efabrica-team / phpstan-latte

PHPStan extension to check compiled latte templates
MIT License
43 stars 4 forks source link

Custom formRender and false positive errors such as `renderPair` #101

Closed jakubvojacek closed 1 year ago

jakubvojacek commented 1 year ago

Hello

we use custom formRender (extending Nette\Forms\Rendering implementing Nette\Forms\FormRenderer)

It has methods such as renderPair, renderControls, ...

We use it directly in lattes sometimes, eg

{$form->getRenderer()->renderControls($form->getGroup('General'), true) |noescape}

or

{$form->getRenderer()->renderPair($formContainer->getComponent('channels_audio_pid')) |noescape}

which results in phpstan complaining about

  133    Call to an undefined method Nette\Forms\FormRenderer::renderPair().                                                   
  136    Call to an undefined method Nette\Forms\FormRenderer::renderPair().                                                   
  139    Call to an undefined method Nette\Forms\FormRenderer::renderPair().                                                   
  144    Call to an undefined method Nette\Forms\FormRenderer::renderPair().                                                   
  147    Call to an undefined method Nette\Forms\FormRenderer::renderPair().                                                   
  150    Call to an undefined method Nette\Forms\FormRenderer::renderPair().  
....

I can add these errors into my phpstan ignore list. But I was wondering if this should be done at the phpstan-latte level? Since Nette\Forms\RenderingDefaultFormRenderer which is used by default implements all these methods?

Thanks

MartinMystikJonas commented 1 year ago

It should be enough to typehint that getRenderer() in your app returns more specific type that has these methods. Either directly in your code (BaseForm) or in PHPStan stubs. Something like this:

/**
 * @method \Nette\Forms\RenderingDefaultFormRenderer getRenderer()
 */
class Form {
jakubvojacek commented 1 year ago

yes,

/**
 * @method \Nette\Forms\Rendering\DefaultFormRenderer getRenderer()
 */

worked like a charm. Thanks a lot :+1:

jakubvojacek commented 1 year ago

This stopped working, I guess it was caused by https://github.com/efabrica-team/phpstan-latte/commit/e73d5f281b8b77f0b7739965be447253b0d73084 (since this commit was related to forms, not sure tho).

Anyhow, now the dev-main reports again

  40     Call to an undefined method Nette\Forms\FormRenderer::renderPair().                                                                                                                                                                  
  43     Call to an undefined method Nette\Forms\FormRenderer::renderPair().                                                                                                                                                                  
  46     Call to an undefined method Nette\Forms\FormRenderer::renderPair().                                                                                                                                                                  
  51     Call to an undefined method Nette\Forms\FormRenderer::renderPair().                                                                                                                                                                  
  54     Call to an undefined method Nette\Forms\FormRenderer::renderPair().                                                                                                                                                                  
  57     Call to an undefined method Nette\Forms\FormRenderer::renderPair().                                                                                                                                                                  
  61     Call to an undefined method Nette\Forms\FormRenderer::renderPair().     

even when I used the phpdocs

/** @method CSMSFormRender getRenderer() */
class CSMSForm extends \Nette\Application\UI\Form
{
...
MartinMystikJonas commented 1 year ago

@lulco I think error is maybe here https://github.com/efabrica-team/phpstan-latte/blob/314ee555a0cb268ec86b172542f11cc33f6fa541/src/Compiler/LatteToPhpCompiler.php#L163 it should extend given form class not basic nette form.

lulco commented 1 year ago

Yes, we don't collect Form type now. Adding to list in https://github.com/efabrica-team/phpstan-latte/issues/106

lulco commented 1 year ago

@jakubvojacek @MartinMystikJonas

Fixed in https://github.com/efabrica-team/phpstan-latte/pull/115

Some problems can be found:

MartinMystikJonas commented 1 year ago

Can we generate PHPStan stubs and pass them to analyser when analysisng templates?

lulco commented 1 year ago

Not sure... Method FileAnalyser::analyseFile() has second parameter array $analysedFiles. I was trying to add some files there several times but always failed.

jakubvojacek commented 1 year ago

I just tested this branch on my codebase and it was much better, no more errors :+1:

lulco commented 1 year ago

@jakubvojacek ok thanks, @MartinMystikJonas please CR and we can merge