TomasVotruba / bladestan

PHPStan analysis for Blade templates
https://tomasvotruba.com/blog/introducing-bladestan-phpstan-analysis-of-blade-templates/
MIT License
290 stars 14 forks source link

Syntax error, unexpected T_ELSE #117

Closed MKodde closed 2 weeks ago

MKodde commented 2 weeks ago

Description

Larastan does not seem to support Bladestan, at least when installing Bladestan, my analysis starts throwing a seemingly random, and false error

 ------ ----------------------------------------------------------------- 
  Line   app/Http/Controllers/Redacted/RedactedController.php     
 ------ ----------------------------------------------------------------- 
  27     Syntax error, unexpected T_ELSE                                  
         ✏️  app/Http/Controllers/Redacted/RedactedController.php  
 ------ ----------------------------------------------------------------- 

On line 27 of this class there is nothing wrong, not even an IF statement is found there, that line is still in the list of use statements for that controller..

Disabling Bladestan makes the error go away.

As can be read in the external link below. I found a PR discussion that larastan excludes blade templates by default. So there's that also. But this issue seems unrelated to that.

Running a more verbose phpstan run shows that Bladestan seems to be loaded by PHPStan

Extension installer:
composer/pcre: 3.3.1
larastan/larastan: v2.9.8
nesbot/carbon: 3.8.0
pestphp/pest: v3.2.3
tomasvotruba/bladestan: 0.5.0

Included configs from Composer packages:
tomasvotruba/bladestan (config/template-compiler/services.neon): 0.5.0
tomasvotruba/bladestan (config/template-compiler/config.neon): 0.5.0

However, it is not listed as a loaded plugins:

Reading /var/www/html/vendor/composer/installed.json
Loading plugin Pest\Plugin\Manager (from pestphp/pest-plugin)
Loading plugin PHPCSStandards\Composer\Plugin\Installers\PHPCodeSniffer\Plugin (from dealerdirect/phpcodesniffer-composer-installer)
Loading plugin PHPStan\ExtensionInstaller\Plugin (from phpstan/extension-installer)
Running 2.7.9 (2024-09-04 14:43:28) with PHP 8.3.11 on Linux / 6.8.0-45-generic

Version details

Laravel version: v11.24.1
Larastan version: v2.9.8
PHPStan version: v1.12.3
Bladestan: v0.5.0

External references

Found this in a Larastan PR

AJenbo commented 2 weeks ago

Bladestan and Larasan are both PHPStan plugins. There are no reason they should conflict and I use both together on multiple project.

I don't see any indication from the errors or what else you have included to indicate that this is caused by them conflicting. You also apparently didn't test if it still happens if you have Bladestan but not Larastan.

The Larastan PR you linked to is also relevant.

It's much more likely that this is caused by you having inline PHP in your blade file that Bladestan isn't able to transform in to an analyzable format. Can you please provide a sample of the code that can trigger this issue.

AJenbo commented 2 weeks ago

If you are not able to give me access to the project where it fails to compile the template then I will need you to narrow down the issue and provide me with a template that can trigger it.

For figuring out what is causing the issue I would suggest that you start by just analyzing RedactedController. Then remove view calls until you have just 1 in the file and the issue is still triggered.

Next edit the blade file until, start by removing any @include or component that isn't causing the issue.

At that point you should be able to see what files trigger the issue, if preferred you can continue to reduce the offending code til you have just the minimum for triggering the issue and then share that so that I can replicate the issue.

AJenbo commented 2 weeks ago

Most likely I think your issue is caused by having non-terminated PHP statements in your temples, this isn't currently supported by Bladestan, see this issue for details: https://github.com/TomasVotruba/bladestan/issues/57

Something like would result in what you have reported:

@php
echo ''
@endphp
@if(1)
@endif

If this isn't what you issue turns out to be please provide further details and I will be happy to reopen the issue and investigate further.

MKodde commented 1 week ago

Thanks for your pointers @AJenbo sadly my project is private so I can not give you direct access. I will however try to narrow down the point where the error is triggered from. My hope was that the PHPstan report would show which template causes the trouble. I'll try to report if I find out what seems to be causing the trouble.

Edit 2024-09-30 12:59 Got the sucker! It turned out to be an unexpected @else stament in a @ifHtmx

AJenbo commented 1 week ago

Glad to hear you figured it out!

Bladestan still has a few rough corners but we are working on making it better all the time, PRs are very welcome 🙂