diff --git a/build/target-repository/docs/rector_rules_overview.md b/build/target-repository/docs/rector_rules_overview.md index 2db4ae5143b..93a9b040379 100644 --- a/build/target-repository/docs/rector_rules_overview.md +++ b/build/target-repository/docs/rector_rules_overview.md @@ -6,7 +6,7 @@ - [Arguments](#arguments) (6) -- [CodeQuality](#codequality) (74) +- [CodeQuality](#codequality) (73) - [CodingStyle](#codingstyle) (34) @@ -1075,34 +1075,6 @@ Move required parameters after optional ones
-### RemoveAlwaysTrueConditionSetInConstructorRector - -If conditions is always true, perform the content right away - -- class: [`Rector\CodeQuality\Rector\FunctionLike\RemoveAlwaysTrueConditionSetInConstructorRector`](../rules/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector.php) - -```diff - final class SomeClass - { - private $value; - - public function __construct(stdClass $value) - { - $this->value = $value; - } - - public function go() - { -- if ($this->value) { -- return 'yes'; -- } -+ return 'yes'; - } - } -``` - -
- ### RemoveSoleValueSprintfRector Remove `sprintf()` wrapper if not needed diff --git a/config/set/code-quality.php b/config/set/code-quality.php index 1371c0dbfb7..6a441dfb5a3 100644 --- a/config/set/code-quality.php +++ b/config/set/code-quality.php @@ -43,7 +43,6 @@ use Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector; use Rector\CodeQuality\Rector\FuncCall\StrvalToTypeCastRector; use Rector\CodeQuality\Rector\FuncCall\UnwrapSprintfOneArgumentRector; -use Rector\CodeQuality\Rector\FunctionLike\RemoveAlwaysTrueConditionSetInConstructorRector; use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector; use Rector\CodeQuality\Rector\Identical\BooleanNotIdenticalToNotIdenticalRector; use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector; @@ -149,7 +148,6 @@ CompleteDynamicPropertiesRector::class, IsAWithStringWithThirdArgumentRector::class, StrlenZeroToIdenticalEmptyStringRector::class, - RemoveAlwaysTrueConditionSetInConstructorRector::class, ThrowWithPreviousExceptionRector::class, RemoveSoleValueSprintfRector::class, ShortenElseIfRector::class, diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/constant_string_with_value.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/constant_string_with_value.php.inc deleted file mode 100644 index f80f4db223c..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/constant_string_with_value.php.inc +++ /dev/null @@ -1,43 +0,0 @@ -value = 'hi'; - } - - public function go() - { - if ($this->value) { - return 'yes'; - } - } -} - -?> ------ -value = 'hi'; - } - - public function go() - { - return 'yes'; - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/empty_stmts.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/empty_stmts.php.inc deleted file mode 100644 index 7064091569a..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/empty_stmts.php.inc +++ /dev/null @@ -1,45 +0,0 @@ -value = $value; - } - - public function go() - { - if ($this->value) { - } - } -} - -?> ------ -value = $value; - } - - public function go() - { - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/fix_static_array.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/fix_static_array.php.inc deleted file mode 100644 index db9609d95d6..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/fix_static_array.php.inc +++ /dev/null @@ -1,45 +0,0 @@ -value = [5]; - } - - public function go() - { - if ($this->value) { - $maybe = 'yes'; - return 'she says ' . $maybe; - } - } -} - -?> ------ -value = [5]; - } - - public function go() - { - $maybe = 'yes'; - return 'she says ' . $maybe; - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/fixture.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/fixture.php.inc deleted file mode 100644 index 3a440e3370b..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,47 +0,0 @@ -value = $value; - } - - public function go() - { - if ($this->value) { - return 'yes'; - } - } -} - -?> ------ -value = $value; - } - - public function go() - { - return 'yes'; - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/multiple_lines.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/multiple_lines.php.inc deleted file mode 100644 index cbe520e4442..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/multiple_lines.php.inc +++ /dev/null @@ -1,60 +0,0 @@ -value = $value; - } - - public function go() - { - if ($this->value) { - $maybe = 'yes'; - return 'she says ' . $maybe; - } - - foreach ([1, 2, 3] as $number) { - if ($number > 100) { - return $number; - } - } - } -} - -?> ------ -value = $value; - } - - public function go() - { - $maybe = 'yes'; - return 'she says ' . $maybe; - foreach ([1, 2, 3] as $number) { - if ($number > 100) { - return $number; - } - } - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/multiple_lines_in_callable.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/multiple_lines_in_callable.php.inc deleted file mode 100644 index 20bc5485832..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/multiple_lines_in_callable.php.inc +++ /dev/null @@ -1,53 +0,0 @@ -value = $value; - } - - public function go() - { - $callable = function () { - if ($this->value) { - $maybe = 1; - return 'she said ' . $maybe; - } - }; - } -} - -?> ------ -value = $value; - } - - public function go() - { - $callable = function () { - $maybe = 1; - return 'she said ' . $maybe; - }; - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/multiple_lines_removed.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/multiple_lines_removed.php.inc deleted file mode 100644 index 6e59bc64ae4..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/multiple_lines_removed.php.inc +++ /dev/null @@ -1,59 +0,0 @@ -value = $value; - } - - public function go() - { - if ($this->value) { - $maybe = 'yes'; - return 'she says ' . $maybe; - } - $callable = function () { - if ($this->value) { - $maybe = 1; - return 'she said ' . $maybe; - } - }; - } -} - -?> ------ -value = $value; - } - - public function go() - { - $maybe = 'yes'; - return 'she says ' . $maybe; - $callable = function () { - $maybe = 1; - return 'she said ' . $maybe; - }; - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/numbers.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/numbers.php.inc deleted file mode 100644 index c280ba653e7..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/numbers.php.inc +++ /dev/null @@ -1,55 +0,0 @@ -value = 15; - $this->smallValue = 0; - } - - public function go() - { - if ($this->value) { - return 'yes'; - } - - if ($this->smallValue) { - return 'no'; - } - } -} - -?> ------ -value = 15; - $this->smallValue = 0; - } - - public function go() - { - return 'yes'; - - if ($this->smallValue) { - return 'no'; - } - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_after_overridden.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_after_overridden.php.inc deleted file mode 100644 index d5e65f6674e..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_after_overridden.php.inc +++ /dev/null @@ -1,26 +0,0 @@ -areListenerClassesLoaded) { - return $this->listenerClassesToEvents; - } - - $this->areListenerClassesLoaded = true; - } -} diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_array.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_array.php.inc deleted file mode 100644 index 0860194ae41..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_array.php.inc +++ /dev/null @@ -1,21 +0,0 @@ -value = $value; - } - - public function go() - { - if ($this->value) { - $maybe = 'yes'; - return 'she says ' . $maybe; - } - } -} diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_changed_value.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_changed_value.php.inc deleted file mode 100644 index b6b667f14d3..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_changed_value.php.inc +++ /dev/null @@ -1,28 +0,0 @@ -value = $value; - } - - public function go() - { - if ($this->value) { - $maybe = 'yes'; - return 'she says ' . $maybe; - } - } - - public function another() - { - $this->value = null; - } -} diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_constant_strings_without_value.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_constant_strings_without_value.php.inc deleted file mode 100644 index d4f821330fa..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_constant_strings_without_value.php.inc +++ /dev/null @@ -1,20 +0,0 @@ -smallValue = ''; - } - - public function go() - { - if ($this->smallValue) { - return 'no'; - } - } -} diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_not_yet_used.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_not_yet_used.php.inc deleted file mode 100644 index 860114803b4..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_not_yet_used.php.inc +++ /dev/null @@ -1,32 +0,0 @@ -statieConfiguration = $statieConfiguration; - } - - public function detect(): string - { - if ($this->detectedTemplating) { - return $this->detectedTemplating; - } - - $twigFileCount = 1; - $latteFileCount = 2; - - $this->detectedTemplating = $twigFileCount > $latteFileCount ? 'twig' : 'latte'; - - return $this->detectedTemplating; - } -} diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_nullable_set.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_nullable_set.php.inc deleted file mode 100644 index ad197b1c856..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_nullable_set.php.inc +++ /dev/null @@ -1,30 +0,0 @@ -callback = $callback; - } - - public function __toString() - { - $contentItems = []; - - if ($this->callback) { - $contentItems['callback'] = 5; - } - - return '...'; - } -} diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_optional_argument_value.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_optional_argument_value.php.inc deleted file mode 100644 index f18a0193226..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_optional_argument_value.php.inc +++ /dev/null @@ -1,24 +0,0 @@ -should = $should; - } - - public function go() - { - if ($this->should) { - $maybe = 'yes'; - return 'she says ' . $maybe; - } - } -} diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_public.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_public.php.inc deleted file mode 100644 index 8e8e7c6c96f..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_public.php.inc +++ /dev/null @@ -1,25 +0,0 @@ -value = 10000; - } - - public function go() - { - if ($this->value) { - return true; - } - - return false; - } -} diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_scalars.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_scalars.php.inc deleted file mode 100644 index e785d61bed5..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_scalars.php.inc +++ /dev/null @@ -1,32 +0,0 @@ -string = $string; - $this->float = $float; - $this->int = $int; - } - - public function go() - { - if ($this->string) { - return 'yes'; - } - - if ($this->float) { - return 'yes'; - } - - if ($this->int) { - return 'yes'; - } - } -} diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_trait.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_trait.php.inc deleted file mode 100644 index f8a8a633a37..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_trait.php.inc +++ /dev/null @@ -1,24 +0,0 @@ -value = $value; - } - - public function go() - { - if ($this->value) { - return 'yes'; - } - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_unknown.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_unknown.php.inc deleted file mode 100644 index 9e8471f7e83..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/skip_unknown.php.inc +++ /dev/null @@ -1,20 +0,0 @@ -yolo = $yolo; - } - - public function go() - { - if ($this->yolo) { - return 'yes'; - } - } -} diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/various_types.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/various_types.php.inc deleted file mode 100644 index b3b87a32bb4..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture/various_types.php.inc +++ /dev/null @@ -1,47 +0,0 @@ -value = 15; - } - - public function go() - { - $this->value = 'hi'; - - if ($this->value) { - return 'yes'; - } - } -} - -?> ------ -value = 15; - } - - public function go() - { - $this->value = 'hi'; - - return 'yes'; - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/RemoveAlwaysTrueConditionSetInConstructorRectorTest.php b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/RemoveAlwaysTrueConditionSetInConstructorRectorTest.php deleted file mode 100644 index 45fe930b32b..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/RemoveAlwaysTrueConditionSetInConstructorRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/config/configured_rule.php deleted file mode 100644 index 3ac570f0a7e..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/config/configured_rule.php +++ /dev/null @@ -1,10 +0,0 @@ -rule(RemoveAlwaysTrueConditionSetInConstructorRector::class); -}; diff --git a/rules/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector.php b/rules/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector.php deleted file mode 100644 index 2aeeb986faa..00000000000 --- a/rules/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector.php +++ /dev/null @@ -1,245 +0,0 @@ -value = $value; - } - - public function go() - { - if ($this->value) { - return 'yes'; - } - } -} -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -final class SomeClass -{ - private $value; - - public function __construct(stdClass $value) - { - $this->value = $value; - } - - public function go() - { - return 'yes'; - } -} -CODE_SAMPLE - ), - ] - ); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [If_::class]; - } - - /** - * @param If_ $node - * @return null|If_|Stmt[] - */ - public function refactor(Node $node): null|If_|array - { - $ifStmt = $this->matchTruableIf($node); - if (! $ifStmt instanceof If_) { - return null; - } - - if ($ifStmt->stmts === []) { - $this->removeNode($ifStmt); - return $ifStmt; - } - - return $ifStmt->stmts; - } - - private function matchTruableIf(If_ $if): If_|null - { - // just one if - if ($if->elseifs !== []) { - return null; - } - - // there is some else - if ($if->else instanceof Else_) { - return null; - } - - // only property fetch, because of constructor set - if (! $if->cond instanceof PropertyFetch) { - return null; - } - - $propertyFetchType = $this->resolvePropertyFetchType($if->cond); - if (! $this->staticTypeAnalyzer->isAlwaysTruableType($propertyFetchType)) { - return null; - } - - return $if; - } - - private function resolvePropertyFetchType(PropertyFetch $propertyFetch): Type - { - $classLike = $this->betterNodeFinder->findParentType($propertyFetch, Class_::class); - if (! $classLike instanceof Class_) { - return new MixedType(); - } - - $propertyName = $this->getName($propertyFetch); - if ($propertyName === null) { - return new MixedType(); - } - - $property = $classLike->getProperty($propertyName); - if (! $property instanceof Property) { - return new MixedType(); - } - - // anything but private can be changed from outer scope - if (! $property->isPrivate()) { - return new MixedType(); - } - - // set in constructor + changed in class - $propertyType = $this->resolvePropertyTypeAfterConstructor($classLike, $propertyName); - - $resolvedTypes = [$propertyType]; - - $defaultValue = $property->props[0]->default; - if ($defaultValue instanceof Expr) { - $resolvedTypes[] = $this->getType($defaultValue); - } - - $resolveAssignedType = $this->resolveAssignedTypeInStmtsByPropertyName($classLike->stmts, $propertyName); - if ($resolveAssignedType instanceof Type) { - $resolvedTypes[] = $resolveAssignedType; - } - - return $this->typeFactory->createMixedPassedOrUnionTypeAndKeepConstant($resolvedTypes); - } - - private function resolvePropertyTypeAfterConstructor(Class_ $class, string $propertyName): Type - { - $propertyTypeFromConstructor = null; - - $constructClassMethod = $class->getMethod(MethodName::CONSTRUCT); - if ($constructClassMethod instanceof ClassMethod) { - $propertyTypeFromConstructor = $this->resolveAssignedTypeInStmtsByPropertyName( - (array) $constructClassMethod->stmts, - $propertyName - ); - } - - if ($propertyTypeFromConstructor instanceof Type) { - return $propertyTypeFromConstructor; - } - - // undefined property is null by default - return new NullType(); - } - - /** - * @param Stmt[] $stmts - */ - private function resolveAssignedTypeInStmtsByPropertyName(array $stmts, string $propertyName): ?Type - { - $resolvedTypes = []; - - $this->traverseNodesWithCallable($stmts, function (Node $node) use ($propertyName, &$resolvedTypes): ?int { - if ($node instanceof ClassMethod && $this->isName($node, MethodName::CONSTRUCT)) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; - } - - if (! $this->isPropertyFetchAssignOfPropertyName($node, $propertyName)) { - return null; - } - - if (! $node instanceof Assign) { - return null; - } - - $resolvedTypes[] = $this->getType($node->expr); - return null; - }); - - if ($resolvedTypes === []) { - return null; - } - - return $this->typeFactory->createMixedPassedOrUnionTypeAndKeepConstant($resolvedTypes); - } - - /** - * E.g. $this->{value} = x - */ - private function isPropertyFetchAssignOfPropertyName(Node $node, string $propertyName): bool - { - if (! $node instanceof Assign) { - return false; - } - - if (! $node->var instanceof PropertyFetch) { - return false; - } - - return $this->isName($node->var, $propertyName); - } -} diff --git a/src/Kernel/RectorKernel.php b/src/Kernel/RectorKernel.php index ca6cbac9acc..5c26410aaa7 100644 --- a/src/Kernel/RectorKernel.php +++ b/src/Kernel/RectorKernel.php @@ -17,7 +17,7 @@ final class RectorKernel /** * @var string */ - private const CACHE_KEY = 'v52'; + private const CACHE_KEY = 'v53'; private ContainerInterface|null $container = null;