Open
Description
When enabling PHPStan's checkUninitializedProperties
, using @required
to autowire a public property, or through a public method will still mark the property as uninitialized, although Symfony's autowiring will make sure it isn't.
This is on;
phpstan/phpstan 1.10.13
phpstan/phpstan-symfony 1.3.1
Metadata
Metadata
Assignees
Labels
No labels
Activity
ondrejmirtes commentedon Apr 18, 2023
Please contribute an extension as per https://phpstan.org/developing-extensions/always-read-written-properties. Thanks.
raalderink commentedon Apr 18, 2023
Okay, cool. This helps implement the case for public properties with
@required
or#[Required]
. There is another strategy to autowire: through public methods with this annotation or attribute.I've found
\PHPStan\Node\ClassPropertiesNode
, which calls\PHPStan\Rules\Properties\ReadWritePropertiesExtension::isInitialized
.ClassPropertiesNode
has all information needed to implement this, ingetPropertyUsages
. ThePropertyWrite
information would allow access to all setting methods, which could be checked for the annotation/attribute. This information, nor theNode
/Scope
, are made available to the extension, meaning that there is no way to access the setting methods.How should I approach this?
ondrejmirtes commentedon Apr 18, 2023
Please show a piece of code you’re talking about - both analysed and the extension you’re writing.
raalderink commentedon Apr 18, 2023
This is an example piece of code to analyse;
This is the extension thus far;
The top part,
if ($property->isPublic()) {
, is functional (although I'm sure there's ways to improve it). Theelse
part is where the issue lies. The$one
in the analysed code will pass correctly, whereas$two
will not, but should be assetTwo
will make sure it is initialized.ondrejmirtes commentedon Apr 18, 2023
Yeah, this isn't great. I worry there would be performance implications by repeating the work of ClassStatementsGatherer there.
But there's something more promising to do - there's a setting
additionalConstructors
that 's used for example for PHPUnit'sTestCase::setUp()
.This setting is currently statically defined in the config, but we could create a new extension that would decide about "additional constructors" programatically.
Here's the relevant code: https://github.com/phpstan/phpstan-src/blob/1.11.x/src/Reflection/ConstructorsHelper.php
This is very similar thing we previously did for
%stubFiles%
: phpstan/phpstan-src@2ba9332ondrejmirtes commentedon Apr 19, 2023
You can continue here now :) https://github.com/phpstan/phpstan/releases/tag/1.10.14
Update Composer dev tooling dependencies (#116)
Set properties autowired with @required as initialized
Set properties autowired with @required as initialized
raalderink commentedon Apr 21, 2023
I've created a new PR for this package now, which should cover this. However, I'm running into a couple things I'm not quite understanding how to solve at this moment. As you can see in the builds, I'm getting these errors;
I'm not sure how to fix these. I'm also seeing a couple of similar ones in the baseline, so would baselining these be OK?
Then there's this error;
Seems to me like this one should be available, but I must be missing something here.