Stillat / blade-parser

A library that makes it simple to parse, analyze, and manipulate Blade templates. Designed with tool makers in mind ❤️
https://stillat.com/blade-parser
MIT License
125 stars 2 forks source link

Error on PHP statements without a closing semicolon (Anticipated PHP compilation error) #9

Closed pascalbaljet closed 1 year ago

pascalbaljet commented 1 year ago

Blade supports PHP code without a closing semicolon:

@php $counter = 1 @endphp

The validator returns this error: Anticipated PHP compilation error: [syntax error, unexpected end of file]

See also: https://github.com/laravel/framework/blob/0ec445970da249578296796ee0aeefc03813bc23/src/Illuminate/View/Compilers/Concerns/CompilesRawPhp.php#L16

JohnathonKoster commented 1 year ago

The linked line refers to the compilation behavior of

@php ($counter = 1)

The following will also produce the same resulting PHP as the library:

<?php

use Illuminate\Support\Facades\Blade;

$result = Blade::compileString('@php $counter = 1 @endphp');

Example of behavior parity:

https://github.com/laravel/framework/blob/0ec445970da249578296796ee0aeefc03813bc23/tests/View/Blade/BladeVerbatimTest.php#L82

JohnathonKoster commented 1 year ago

nvm (shouldn't review when so tired) - spotted the issue

JohnathonKoster commented 1 year ago

Corrected in 1.0.3 🙂

pascalbaljet commented 1 year ago

Nice! I've found another case that leads to a false positive:

@php
    if ($someVar) {
        echo 'Yes';
    } else {
        echo 'No';
    }
@endphp
JohnathonKoster commented 1 year ago

Nice! I've found another case that leads to a false positive:

@php
    if ($someVar) {
        echo 'Yes';
    } else {
        echo 'No';
    }
@endphp

Is this triggering the Anticipated PHP compilation error: [syntax error, unexpected end of file] for you in 1.0.3? If so, are there any other @php directives in the template?

I'm have trouble reproducing on the latest version with that template by itself. Thanks!