Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using --analysis with Drupal gives a lot of issues due to loose array types #277

Closed
googletorp opened this issue May 29, 2022 · 4 comments
Closed

Comments

@googletorp
Copy link

PHPStan will on analysis (level 6) give error if arrays are not correctly types, see more here: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type. This makes a lot of sense on a generic PHP application. You would like to get code like this:

/**
 * Some example function
 * 
 * @param MyClass[] $list
 ...
 * /

instead of seeing code like this

/**
 * Some example function
 * 
 * @param array $list
 ...
 * /

This will make the analysis much better as it's possible to figure out what kind of data is inside the loop. The thing is that Drupal uses complex array structures for render arrays and Form API arrays, meaning that you will need to:

 @param mixed[] $form

In order to explicitly say to PHPStan that we don't really know what the array contains. When extending a lot of core classes in custom code you want to analyze you will get these errors all over and they are not fixable if you use {@inheritDox}.

This is not a bug in drupal-check, but I think it would make a lot of sense to ignore this error when using analysis.

@googletorp
Copy link
Author

Hey @mglaman, please let me know what you think about this, the code is pretty simple for this so made a PR in the hopes that you agree with me.
Thanks for all the work maintaining this.

@rimi-itk
Copy link

Thanks for #278, @googletorp.

However, it may be a better solution to make it possible include some configuration file, e.g. phpstan.neon (in the project root), when running drupal-check (cf. https://github.com/mglaman/drupal-check#rollback-update-to-phpstan-level-2-for-deprecation-analysis). Any thoughts on this, @mglaman?

@mglaman
Copy link
Owner

mglaman commented Jul 26, 2022

However, it may be a better solution to make it possible include some configuration

Please, just use PHPStan directly then. This package is meant to be an opinionated quick scan. Annotating array types can be annoying, but it helps fix buggy code.

@mglaman mglaman closed this as not planned Won't fix, can't repro, duplicate, stale Jul 26, 2022
@googletorp
Copy link
Author

@mglaman I agree in general that annotating array types is a great thing, but with Drupal use of render and form arrays, you get a bunch or warnings every time you extend core, which seems to defeat the purpose of writing clean code, if the framework you are building upon doesn't follow the same standards.

Can you offer some guidance on how to use this feature without getting overwhelmed by errors coming from Drupal Core?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants