canvural / phpstan-blade-rule

PHPStan rule for static analysis of Blade templates
MIT License
56 stars 5 forks source link

Syntax error, unexpected T_ENDIF on line X #4

Open micc83 opened 2 years ago

micc83 commented 2 years ago

Hi, first of all thank you. I've been waiting to be able to run static analysis on my blade templates since a long time.

Unfortunately on my first try I get only a bunch of syntax errors such as:

 ------ ---------------------------------------------------------------
  Line   Domain/Schedulers/Http/Controllers/SchedulePlanController.php 
 ------ ---------------------------------------------------------------
  217    Syntax error, unexpected T_ENDIF on line 217
 ------ ---------------------------------------------------------------

or

 ------ --------------------------------------------------------------
  Line   Domain/Statistics/Http/Controllers/StatsController.php       
 ------ --------------------------------------------------------------
  6      Syntax error, unexpected T_VARIABLE, expecting ';' on line 6
 ------ --------------------------------------------------------------

What I can tell you is that, for example, SchedulePlanController doesn't even have 217 lines so, I guess, its referring to some issue in some of the blade templates rendered by the controller?

My composer.json is something like:

{
    "require": {
        "php": ">=8.0",
        "laravel/framework": "^8.0",
        "laravel/helpers": "^1.2",
        "laravel/legacy-factories": "^1.1",
        "laravel/ui": "^3.0",
    },
    "require-dev": {
        "canvural/phpstan-blade-rule": "^0.1.0",
        "nunomaduro/larastan": "^1.0",
    },
    "autoload": {
        "classmap": [
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/",
            "Domain\\": "app/Domain/",
            "Common\\": "app/Common/",
            "Database\\Seeders\\": "database/seeders/"
        },
        "files": [
            "app/Common/helpers.php"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    }
}

My phpstan.neon file:

includes:
    - ./vendor/nunomaduro/larastan/extension.neon
    - ./vendor/canvural/phpstan-blade-rule/config/extension.neon
    - ./vendor/symplify/template-phpstan-compiler/config/services.neon
    - ./vendor/symplify/astral/config/services.neon

parameters:

    paths:
        - app/Domain
        - app/Common

    templatePaths:
        - resources/views

    # The level 8 is the highest level
    level: 8

    #ignoreErrors:
    #    - '#Unsafe usage of new static#'

    excludePaths:
        - ./*/*/FileToBeExcluded.php

    checkMissingIterableValueType: false

I've also tried to comment larastan extension but nothing.

canvural commented 2 years ago

Hi,

Can you show me the output of composer show symplify/template-phpstan-compiler ? (just the version)

Also, do you override any built-in Blade directive by registering your own?

micc83 commented 2 years ago

Hi, sorry for not replying to you sooner but I changed my laptop and haven't been able to reproduce the error until now.

The version is:

versions : * 10.0.21

About overriding any built-in Blade directive... not that I'm aware of.

Also I confirm that errors are indeed shown under the controller and not the view file. Ex:

 ------ -------------------------------------------------------------------------------------------------------------------------------------- 
  Line   Domain/Reviews/Http/Controllers/ReviewController.php                                                                                  
 ------ -------------------------------------------------------------------------------------------------------------------------------------- 
  36     Argument of an invalid type Illuminate\Contracts\Pagination\LengthAwarePaginator supplied for foreach, only iterables are supported.  

That foreach is indeed inside the reviews/index.blade.php file:

55. @foreach($reviews as $review)

which is rendered as:

53. <?php $__currentLoopData = $reviews; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $review): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

The line number (36) is indeed taken from the line returning the template:

36. return view('reviews.index', [
            'reviews'        => $this->reviews->paginate(),
];
micc83 commented 2 years ago

Hi @canvural, I see that the issue is still in state "waiting for feedback". Is there any extra info needed to debug the issue?

canvural commented 2 years ago

@micc83 There is really not much information here for me to understand what is the issue. I would need a minimal code that reproduces this. You can also try to see what is the compiled template somewhere in here or here.

micc83 commented 2 years ago

Sorry, super late. Here's what I've figured out:

Issue 1: unexpected ')', expecting T_VARIABLE

My template is rendered as follow:

public function create(): View
{
    return view('someview');
}
{{--someview.blade.php--}}
@include('someview._form')
<?php
/** file: someview.blade.php, line: 1 */ (function () use() {

});
 ------ -------------------------------------------------------------- 
  Line   MyController.php                            
 ------ -------------------------------------------------------------- 
  2      Syntax error, unexpected ')', expecting T_VARIABLE on line 2  
 ------ -------------------------------------------------------------- 

So from a fast check I see that the $usePlaceholder variable is used even if $allVariablesList is empty.

Issue 2: unexpected T_VARIABLE, expecting ';'

One more issue with:

 <my-vue-component :enabled="@json(Module::isActive('booking'))"></my-vue-component>
<?php
/** file: someview.blade.php, line: 1 */ echo json_encode(\Common\Settings\Models\Module::isActive('booking'), 15, 512)
 ------ -------------------------------------------------------------- 
  Line   MyController.php                                           
 ------ -------------------------------------------------------------- 
  2      Syntax error, unexpected T_VARIABLE, expecting ';' on line 2
 ------ -------------------------------------------------------------- 

Let me know if I'm on the right path and I'll keep debugging.

canvural commented 2 years ago

Hi @micc83,

The first issue should be fixed in the main branch. Can you try dev-main?

I'll investigate the second issue. Thanks.