jbboehr/laravel-blade-linter

View on GitHub
src/Backend/Evaluate.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php
declare(strict_types=1);

namespace Bdelespierre\LaravelBladeLinter\Backend;

use Bdelespierre\LaravelBladeLinter\Backend;
use Bdelespierre\LaravelBladeLinter\ErrorRecord;

final class Evaluate implements Backend
{
    /**
     * @param \SplFileInfo $file
     * @param string $code
     * @return list<ErrorRecord>
     */
    public function analyze(\SplFileInfo $file, string $code): array
    {
        $code = 'function() { ?>' . $code . '<?php };';

        try {
            eval($code);
        } catch (\ParseError $e) {
            return [
                new ErrorRecord(
                    'Parse error: ' . $e->getMessage(),
                    $file->getPathname(),
                    $e->getLine()
                )
            ];
        }

        return [];
    }

    public static function name(): string
    {
        return 'eval';
    }
}