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

PHP 8.2 - Phpstan - Does not scan laravel ide helper model #8566

Closed
karinarastsinskagia opened this issue Dec 20, 2022 · 12 comments
Closed

PHP 8.2 - Phpstan - Does not scan laravel ide helper model #8566

karinarastsinskagia opened this issue Dec 20, 2022 · 12 comments

Comments

@karinarastsinskagia
Copy link

Bug report

Probably similar to #8550, on PHP 8.2, when a dynamic property is documented in laravel ide helper model with @Property annotation, an Access to an undefined property is thrown. It seems that phpstan does not scan ide_helper_models script and does not recognize these properties.
If I put all these @Property annotations to the respective class directly, the problem disappears. However, this does not solve the real issue.

Also, I noticed that if I customize my ruleset-phpstan.neon file and add the following parameter everything works fine.
phpVersion: 80100 # PHP 8.1

So, phpstan is not compatible with laravel ide helper?

Code snippet that reproduces the problem

Expected output

No errors!

Did PHPStan help you today? Did it make you happy in any way?

@mergeable
Copy link

mergeable bot commented Dec 20, 2022

This bug report is missing a link to reproduction at phpstan.org/try.

It will most likely be closed after manual review.

@LastDragon-ru
Copy link

You probably need to add generated file(s) into stubFiles on your phpstan.neon. But I would recommend to use https://github.com/nunomaduro/larastan instead of it + add @property into Model classes (ide helper can do it automatically)

@ondrejmirtes
Copy link
Member

@karinarastsinskagia How does the generated file look like? It needs to have #[\AllowDynamicProperties] above the class to work on PHP 8.2.

@karinarastsinskagia
Copy link
Author

You probably need to add generated file(s) into stubFiles on your phpstan.neon. But I would recommend to use https://github.com/nunomaduro/larastan instead of it + add @property into Model classes (ide helper can do it automatically)

I already use nunomaduro/larastan and all @Property have been added into ide_helper_models.php. Into each Model class has been declared the following

/** @mixin IdeHelper<Model>*/ in order to access all these annotations.

Please find below how my phpstan.neon file looks like

includes:
- ./vendor/nunomaduro/larastan/extension.neon

parameters:

paths:
    - src
    - tests
level: 6

tmpDir: build/phpstan
reportStaticMethodSignatures: true
checkModelProperties: true
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
checkOctaneCompatibility: true

parallel:
    jobSize: 20
    maximumNumberOfProcesses: 32
    minimumNumberOfJobsPerProcess: 2

scanFiles:
	- _ide_helper.php
	- _ide_helper_models.php

@ondrejmirtes
Copy link
Member

Classes are allowed to have dynamic properties with #[\AllowDynamicProperties], or if they have __get and __set methods.

@karinarastsinskagia
Copy link
Author

karinarastsinskagia commented Dec 21, 2022

I have already tried to use #[\AllowDynamicProperties] but nothing changed. It seems that phpstan does not scan ide_helper_models.php for @Property annotations which have been declared for Model classes.

Has anyone dealt with a similar case?

@ondrejmirtes
Copy link
Member

Please create a small reproducing repository that shows the problem.

@karinarastsinskagia
Copy link
Author

karinarastsinskagia commented Jan 5, 2023

Please create a small reproducing repository that shows the problem.

Please find below an example of this issue

https://phpstan.org/r/a99c90c0-488e-4105-8955-451ce8a77c36

As I said earlier, I am using laravel ide helper in order to generate _ide_helper_models.php which includes all @Property annotations instead of adding @Property to my models files.

@ondrejmirtes
Copy link
Member

@karinarastsinskagia
Copy link
Author

My suggestion works: https://phpstan.org/r/cbc4611b-f6d6-4dc3-8bba-bb5f01dc867c

Is there any other solution ? Is it possible some of the phpstan.neon settings to affect the #[\AllowDynamicProperties] ?
Also, since the model's properties are included in IdeHelperAttribute (which is defined in model) why they are not accessible without using #[\AllowDynamicProperties] ?

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Jan 5, 2023

Your code can't work on PHP 8.2 (throws deprecation warnings) unless you allow dynamic properties with #[\AllowDynamicProperties], or implement @property using __get and __set magic methods.

When I try to run your original code on PHP 8.2, it throws deprecation warnings, which matches what PHPStan reports: https://3v4l.org/FbLAg

@github-actions
Copy link

github-actions bot commented Feb 9, 2023

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants