efabrica-team / phpstan-latte

PHPStan extension to check compiled latte templates

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Links to actions with `never` return type throws "Unreachable statement"

spaze opened this issue · comments

I have a template that contains this:

	<li><a n:href="Sign:out">Odhlásit</a></li>

The SignPresenter has actionOut method that has never specified as its return type because it will redirect (and call `exit), see source.

The template is compiled to

    <li><a href="';
    /* line 63 */
    /** @var MichalSpacekCz\Admin\Presenters\SignPresenter $adminSignPresenter */
    $adminSignPresenter->actionOut();
    echo '">Odhlásit</a></li>  // <-- Line 425 added by the issue reporter
</ul>

Which will cause "Unreachable statement - code above always terminates." (on level 4+):

 ------ ---------- --------------------------------------------------------------------------------------------------------------- 
  Line   Compiled   Admin/Presenters/templates/Homepage/default.latte rendered from                                                
         line       MichalSpacekCz\Admin\Presenters\HomepagePresenter::default                                                     
                    See compiled template:                                                                                         
                    /tmp/phpstan-latte/app/Admin/Presenters/templates/Homepage/default.latte.af440d9b3e33680491f50c2047124ae6.php  
 ------ ---------- --------------------------------------------------------------------------------------------------------------- 
  63     425        Unreachable statement - code above always terminates.                                                          
 ------ ---------- --------------------------------------------------------------------------------------------------------------- 

Hmm nice catch :) we used link transformation just to check parameters of presenter action / render methods.

But it is not real method call at those places where link is used. We have to think how to transform it to coreect code

Maybe make it a "conditional call" by wraping it in condition. Something like:

if ($__followLink) {actionXyz();} 

Therefore we woukd avoid always terminates because it is not always called. But check for params would be still valid.

Even better we could implement custom rule for checking link calls directly without transforms. It would be useful for PHP side of app as well.

Maybe make it a "conditional call" by wraping it in condition. Something like:

if ($__followLink) {actionXyz();} 

Yes, this should work

Therefore we woukd avoid always terminates because it is not always called. But check for params would be still valid.

Even better we could implement custom rule for checking link calls directly without transforms. It would be useful for PHP side of app as well.

I agree, but this should be part od nette extension then

oh, this is not working
https://phpstan.org/r/0f7e8cc0-9c23-4395-b64c-e3679bdd56ab

PHPStan is too smart for this hack :)
as soon as the condition is true, it is going to unreachable statement so PHPStan knows that all other conditions have to be false to not reach unreachable statement...

Any other idea? :) maybe call some boolean function always before

ok, I used mt_rand() === 0 in condition. Because mt_rand is impure function, it always returns different value