From 39168875824c59e2261acef5a97191da64b393c2 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 1 Jan 2024 13:26:19 +0000 Subject: [PATCH] Updated Rector to commit 9f937086898498f37d71977895312cbdb7fdbc6a https://github.com/rectorphp/rector-src/commit/9f937086898498f37d71977895312cbdb7fdbc6a [Strict] Handle may be unitialized property on DisallowedEmptyRuleFixerRector (#5409) --- .../UnitializedPropertyAnalyzer.php | 76 +++++++++++++++++++ .../Empty_/DisallowedEmptyRuleFixerRector.php | 22 +++++- src/Application/VersionResolver.php | 4 +- vendor/composer/autoload_classmap.php | 1 + vendor/composer/autoload_static.php | 1 + 5 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 rules/Strict/NodeAnalyzer/UnitializedPropertyAnalyzer.php diff --git a/rules/Strict/NodeAnalyzer/UnitializedPropertyAnalyzer.php b/rules/Strict/NodeAnalyzer/UnitializedPropertyAnalyzer.php new file mode 100644 index 00000000000..3a0ce75f734 --- /dev/null +++ b/rules/Strict/NodeAnalyzer/UnitializedPropertyAnalyzer.php @@ -0,0 +1,76 @@ +astResolver = $astResolver; + $this->nodeTypeResolver = $nodeTypeResolver; + $this->constructorAssignDetector = $constructorAssignDetector; + $this->nodeNameResolver = $nodeNameResolver; + } + public function isUnitialized(Expr $expr) : bool + { + if (!$expr instanceof PropertyFetch && !$expr instanceof StaticPropertyFetch) { + return \false; + } + $varType = $this->nodeTypeResolver->getType($expr->var); + if ($varType instanceof ThisType) { + $varType = $varType->getStaticObjectType(); + } + if (!$varType instanceof TypeWithClassName) { + return \false; + } + $className = $varType->getClassName(); + $classLike = $this->astResolver->resolveClassFromName($className); + if (!$classLike instanceof ClassLike) { + return \false; + } + $propertyName = (string) $this->nodeNameResolver->getName($expr); + $property = $classLike->getProperty($propertyName); + if (!$property instanceof Property) { + return \false; + } + if (\count($property->props) !== 1) { + return \false; + } + if ($property->props[0]->default instanceof Expr) { + return \false; + } + return !$this->constructorAssignDetector->isPropertyAssigned($classLike, $propertyName); + } +} diff --git a/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php b/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php index 65dddb44f3c..7254cae6270 100644 --- a/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php +++ b/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php @@ -7,12 +7,14 @@ use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\BinaryOp\BooleanAnd; +use PhpParser\Node\Expr\BinaryOp\BooleanOr; use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\Empty_; use PhpParser\Node\Expr\Isset_; use PHPStan\Analyser\Scope; use Rector\Core\Contract\Rector\ConfigurableRectorInterface; use Rector\Core\NodeAnalyzer\ExprAnalyzer; +use Rector\Strict\NodeAnalyzer\UnitializedPropertyAnalyzer; use Rector\Strict\NodeFactory\ExactCompareFactory; use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; @@ -32,10 +34,16 @@ final class DisallowedEmptyRuleFixerRector extends AbstractFalsyScalarRuleFixerR * @var \Rector\Core\NodeAnalyzer\ExprAnalyzer */ private $exprAnalyzer; - public function __construct(ExactCompareFactory $exactCompareFactory, ExprAnalyzer $exprAnalyzer) + /** + * @readonly + * @var \Rector\Strict\NodeAnalyzer\UnitializedPropertyAnalyzer + */ + private $unitializedPropertyAnalyzer; + public function __construct(ExactCompareFactory $exactCompareFactory, ExprAnalyzer $exprAnalyzer, UnitializedPropertyAnalyzer $unitializedPropertyAnalyzer) { $this->exactCompareFactory = $exactCompareFactory; $this->exprAnalyzer = $exprAnalyzer; + $this->unitializedPropertyAnalyzer = $unitializedPropertyAnalyzer; } public function getRuleDefinition() : RuleDefinition { @@ -93,7 +101,11 @@ CODE_SAMPLE return null; } $emptyExprType = $scope->getNativeType($empty->expr); - return $this->exactCompareFactory->createNotIdenticalFalsyCompare($emptyExprType, $empty->expr, $this->treatAsNonEmpty); + $result = $this->exactCompareFactory->createNotIdenticalFalsyCompare($emptyExprType, $empty->expr, $this->treatAsNonEmpty); + if ($this->unitializedPropertyAnalyzer->isUnitialized($empty->expr)) { + return new BooleanAnd(new Isset_([$empty->expr]), $result); + } + return $result; } private function refactorEmpty(Empty_ $empty, Scope $scope, bool $treatAsNonEmpty) : ?\PhpParser\Node\Expr { @@ -101,7 +113,11 @@ CODE_SAMPLE return null; } $exprType = $scope->getNativeType($empty->expr); - return $this->exactCompareFactory->createIdenticalFalsyCompare($exprType, $empty->expr, $treatAsNonEmpty); + $result = $this->exactCompareFactory->createIdenticalFalsyCompare($exprType, $empty->expr, $treatAsNonEmpty); + if ($this->unitializedPropertyAnalyzer->isUnitialized($empty->expr)) { + return new BooleanOr(new BooleanNot(new Isset_([$empty->expr])), $result); + } + return $result; } private function createDimFetchBooleanAnd(ArrayDimFetch $arrayDimFetch) : ?BooleanAnd { diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 5e45c00e2d6..b96a3610e8e 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = 'a64a34407038fab9ba3195d8314c1f550a7d1d40'; + public const PACKAGE_VERSION = '9f937086898498f37d71977895312cbdb7fdbc6a'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-01-01 13:23:58'; + public const RELEASE_DATE = '2024-01-01 14:24:13'; /** * @var int */ diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 6381624e36c..b77d83a3427 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -2081,6 +2081,7 @@ return array( 'Rector\\StaticTypeMapper\\ValueObject\\Type\\ShortenedGenericObjectType' => $baseDir . '/packages/StaticTypeMapper/ValueObject/Type/ShortenedGenericObjectType.php', 'Rector\\StaticTypeMapper\\ValueObject\\Type\\ShortenedObjectType' => $baseDir . '/packages/StaticTypeMapper/ValueObject/Type/ShortenedObjectType.php', 'Rector\\StaticTypeMapper\\ValueObject\\Type\\SimpleStaticType' => $baseDir . '/packages/StaticTypeMapper/ValueObject/Type/SimpleStaticType.php', + 'Rector\\Strict\\NodeAnalyzer\\UnitializedPropertyAnalyzer' => $baseDir . '/rules/Strict/NodeAnalyzer/UnitializedPropertyAnalyzer.php', 'Rector\\Strict\\NodeFactory\\ExactCompareFactory' => $baseDir . '/rules/Strict/NodeFactory/ExactCompareFactory.php', 'Rector\\Strict\\Rector\\AbstractFalsyScalarRuleFixerRector' => $baseDir . '/rules/Strict/Rector/AbstractFalsyScalarRuleFixerRector.php', 'Rector\\Strict\\Rector\\BooleanNot\\BooleanInBooleanNotRuleFixerRector' => $baseDir . '/rules/Strict/Rector/BooleanNot/BooleanInBooleanNotRuleFixerRector.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 5383b45eba0..92cf0a348eb 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -2299,6 +2299,7 @@ class ComposerStaticInit3f1c613015ba4b47f7f73b50cb66fb68 'Rector\\StaticTypeMapper\\ValueObject\\Type\\ShortenedGenericObjectType' => __DIR__ . '/../..' . '/packages/StaticTypeMapper/ValueObject/Type/ShortenedGenericObjectType.php', 'Rector\\StaticTypeMapper\\ValueObject\\Type\\ShortenedObjectType' => __DIR__ . '/../..' . '/packages/StaticTypeMapper/ValueObject/Type/ShortenedObjectType.php', 'Rector\\StaticTypeMapper\\ValueObject\\Type\\SimpleStaticType' => __DIR__ . '/../..' . '/packages/StaticTypeMapper/ValueObject/Type/SimpleStaticType.php', + 'Rector\\Strict\\NodeAnalyzer\\UnitializedPropertyAnalyzer' => __DIR__ . '/../..' . '/rules/Strict/NodeAnalyzer/UnitializedPropertyAnalyzer.php', 'Rector\\Strict\\NodeFactory\\ExactCompareFactory' => __DIR__ . '/../..' . '/rules/Strict/NodeFactory/ExactCompareFactory.php', 'Rector\\Strict\\Rector\\AbstractFalsyScalarRuleFixerRector' => __DIR__ . '/../..' . '/rules/Strict/Rector/AbstractFalsyScalarRuleFixerRector.php', 'Rector\\Strict\\Rector\\BooleanNot\\BooleanInBooleanNotRuleFixerRector' => __DIR__ . '/../..' . '/rules/Strict/Rector/BooleanNot/BooleanInBooleanNotRuleFixerRector.php',