TomasVotruba / bladestan

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

Can't detect undefined methods in {{ \Carbon\Carbon::now()->undefined() }} while it can detect it in <?php echo \Carbon\Carbon::now()->undefined(); ?> #104

Closed johntheteam closed 3 weeks ago

johntheteam commented 4 weeks ago

Create a simple test file of resources/views/test.blade.php

<div>Hello, Blade!</div>
<?php echo \Carbon\Carbon::now()->undefined();?>
{{ \Carbon\Carbon::now()->undefined() }}

test it with the command below:

vendor/bin/phpstan analyze -c phpstan.neon --memory-limit=512M --debug -vvv resources/views/test.blade.php

It can find the error on line 2 but not line 3.

johntheteam commented 4 weeks ago

A full test project:

composer create-project --prefer-dist laravel/laravel blade

cd blade

composer require --dev phpstan/phpstan tomasvotruba/bladestan

vi phpstan.neon

includes:
    - vendor/tomasvotruba/bladestan/config/extension.neon

parameters:
    level: 8
    paths:
        - resources/views
    fileExtensions:
        - blade.php

vi resources/views/test.blade.php

<div>Hello, Blade!</div>
<?php echo \Carbon\Carbon::now()->undefined();?>
{{ \Carbon\Carbon::now()->undefined() }}

vendor/bin/phpstan analyze -c phpstan.neon --memory-limit=512M --debug -vvv resources/views/test.blade.php

AJenbo commented 3 weeks ago

Note that {{ }} doesn't simply compile to echo

AJenbo commented 3 weeks ago

This is incorrect usage, you need to have the template rendered in context to be able to properly analyze them, you cannot analyze Blade files directly.

vendor/bin/phpstan analyze -c phpstan.neon --memory-limit=512M --debug -vvv resources/views/test.blade.php
AJenbo commented 3 weeks ago

Correct usage (read the docs for further details on how to use BladeStan with your project):

phpstan.neon:

includes:
    - vendor/tomasvotruba/bladestan/config/extension.neon

parameters:
    level: 8
    paths:
        - app

app/Http/Controllers/Controller.php

<?php

namespace App\Http\Controllers;

abstract class Controller
{
    public function test() {
        return view('test');
    }
}

vendor/bin/phpstan analyze --error-format=blade

 ------ ------------------------------------------------------------------------------ 
  Line   app/Http/Controllers/Controller.php                                           
 ------ ------------------------------------------------------------------------------ 
  8      Call to an undefined method Carbon\Carbon::undefined().                       
         rendered in: test.blade.php:1                                                 
  8      Call to an undefined method Carbon\Carbon::undefined().                       
         rendered in: test.blade.php:3           
johntheteam commented 3 weeks ago

This is incorrect usage, you need to have the template rendered in context to be able to properly analyze them, you cannot analyze Blade files directly.

Of course you can, you can't analyze Blade files perfectly, but just take Blade files as PHP files, you can at least analyze them in the level of PHP file.

Note that {{ }} doesn't simply compile to echo

You're right, it's compiled to echo e($contentInDoubleBraces);

Correct usage (read the docs for further details on how to use BladeStan with your project):

Where is the docs?

The readme is so simple.

I found other problems:

1, When I specify a wrong Blade file in the code, it just passed.

return view('not-exist');

2, When Blade file is under a Laravel package's sub folder of resources/views, PHPStan reports serious problem.

3, The parameter of the template paths don't recognize * or regular expression or any way to generalize and need to specify one by one, this is hard for serious use.

    bladestan:
        template_paths:
AJenbo commented 3 weeks ago

This is incorrect usage, you need to have the template rendered in context to be able to properly analyze them, you cannot analyze Blade files directly.

Of course you can, you can't analyze Blade files perfectly, but just take Blade files as PHP files, you can at least analyze them in the level of PHP file.

Ok, but that's just being pedantic and unrelated to this project.

Correct usage (read the docs for further details on how to use BladeStan with your project):

Where is the docs?

The readme is so simple.

That's all that is needed, the other steps you did are not mentioned anywhere and contradicts some of them. The readme should help you understand the examples I gave, hope that helps.

I found other problems:

1, When I specify a wrong Blade file in the code, it just passed.

return view('not-exist');

This is a known missing feature.

2, When Blade file is under a Laravel package's sub folder of resources/views, PHPStan reports serious problem.

Please report issues individually, and provide details "serious problem" isn't clear.

3, The parameter of the template paths don't recognize * or regular expression or any way to generalize and need to specify one by one, this is hard for serious use.

    bladestan:
        template_paths:

This sounds more like a feature request, is it something you would be able to implement?

johntheteam commented 2 weeks ago

This sounds more like a feature request, is it something you would be able to implement?

After re-considering the issue, I think this feature isn't needed. The problem is that when there are multiple paths having the same name of blade files, Bladestan will mistook one for another. It's using the template_paths to find the template, which may not be the one used in the context. Therefore it should be a bug.

AJenbo commented 1 week ago

There is already a feature request for that: https://github.com/TomasVotruba/bladestan/issues/41

Please look at existing issues and subscribe to the existing ones rather then making new requests when one already exists.