jbboehr/laravel-blade-linter

View on GitHub
src/Backend/ExtAst.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;
use function ast\parse_code;

final class ExtAst implements Backend
{
    public function __construct(
        private int $astVersion = 85
    ) {
    }

    /**
     * @param \SplFileInfo $file
     * @param string $code
     * @return list<ErrorRecord>
     */
    public function analyze(\SplFileInfo $file, string $code): array
    {
        try {
            parse_code($code, $this->astVersion);
        } catch (\ParseError $e) {
            return [
                new ErrorRecord(
                    'Parse error: ' . $e->getMessage(),
                    $file->getPathname(),
                    $e->getLine()
                )
            ];
        }

        return [];
    }

    public static function name(): string
    {
        return 'ext-ast';
    }
}