mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 05:48:21 +01:00
Updated Rector to commit 9f937086898498f37d71977895312cbdb7fdbc6a
9f93708689
[Strict] Handle may be unitialized property on DisallowedEmptyRuleFixerRector (#5409)
This commit is contained in:
parent
aff2607559
commit
3916887582
76
rules/Strict/NodeAnalyzer/UnitializedPropertyAnalyzer.php
Normal file
76
rules/Strict/NodeAnalyzer/UnitializedPropertyAnalyzer.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Strict\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Expr\StaticPropertyFetch;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PHPStan\Type\ThisType;
|
||||
use PHPStan\Type\TypeWithClassName;
|
||||
use Rector\Core\PhpParser\AstResolver;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\NodeTypeResolver;
|
||||
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
|
||||
final class UnitializedPropertyAnalyzer
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\PhpParser\AstResolver
|
||||
*/
|
||||
private $astResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeTypeResolver\NodeTypeResolver
|
||||
*/
|
||||
private $nodeTypeResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector
|
||||
*/
|
||||
private $constructorAssignDetector;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeNameResolver\NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
public function __construct(AstResolver $astResolver, NodeTypeResolver $nodeTypeResolver, ConstructorAssignDetector $constructorAssignDetector, NodeNameResolver $nodeNameResolver)
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
@ -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
|
||||
{
|
||||
|
@ -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
|
||||
*/
|
||||
|
1
vendor/composer/autoload_classmap.php
vendored
1
vendor/composer/autoload_classmap.php
vendored
@ -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',
|
||||
|
1
vendor/composer/autoload_static.php
vendored
1
vendor/composer/autoload_static.php
vendored
@ -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',
|
||||
|
Loading…
x
Reference in New Issue
Block a user