Skip to content

Commit

Permalink
Updated Rector to commit 91ed251a8c450a776ec4b739c735b4bd6f9bf8d5
Browse files Browse the repository at this point in the history
rectorphp/rector-src@91ed251 [DeadCode] Skip property fetch on cond as may be overridden on RemoveAlwaysTrueIfConditionRector (#5796)
  • Loading branch information
TomasVotruba committed Apr 3, 2024
1 parent a210882 commit 81fd362
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 31 deletions.
34 changes: 5 additions & 29 deletions rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\PropertyFetch;
Expand All @@ -14,25 +15,18 @@
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\If_;
use PhpParser\NodeTraverser;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\Constant\ConstantBooleanType;
use Rector\DeadCode\NodeAnalyzer\SafeLeftTypeBooleanAndOrAnalyzer;
use Rector\NodeAnalyzer\ExprAnalyzer;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector\RemoveAlwaysTrueIfConditionRectorTest
*/
final class RemoveAlwaysTrueIfConditionRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\Reflection\ReflectionResolver
*/
private $reflectionResolver;
/**
* @readonly
* @var \Rector\NodeAnalyzer\ExprAnalyzer
Expand All @@ -48,9 +42,8 @@ final class RemoveAlwaysTrueIfConditionRector extends AbstractRector
* @var \Rector\DeadCode\NodeAnalyzer\SafeLeftTypeBooleanAndOrAnalyzer
*/
private $safeLeftTypeBooleanAndOrAnalyzer;
public function __construct(ReflectionResolver $reflectionResolver, ExprAnalyzer $exprAnalyzer, BetterNodeFinder $betterNodeFinder, SafeLeftTypeBooleanAndOrAnalyzer $safeLeftTypeBooleanAndOrAnalyzer)
public function __construct(ExprAnalyzer $exprAnalyzer, BetterNodeFinder $betterNodeFinder, SafeLeftTypeBooleanAndOrAnalyzer $safeLeftTypeBooleanAndOrAnalyzer)
{
$this->reflectionResolver = $reflectionResolver;
$this->exprAnalyzer = $exprAnalyzer;
$this->betterNodeFinder = $betterNodeFinder;
$this->safeLeftTypeBooleanAndOrAnalyzer = $safeLeftTypeBooleanAndOrAnalyzer;
Expand Down Expand Up @@ -113,7 +106,7 @@ public function refactor(Node $node)
if (!$conditionStaticType->getValue()) {
return null;
}
if ($this->shouldSkipPropertyFetch($node->cond)) {
if ($this->shouldSkipExpr($node->cond)) {
return null;
}
if ($this->shouldSkipFromParam($node->cond)) {
Expand All @@ -139,26 +132,9 @@ private function shouldSkipFromParam(Expr $expr) : bool
}
return \false;
}
private function shouldSkipPropertyFetch(Expr $expr) : bool
private function shouldSkipExpr(Expr $expr) : bool
{
/** @var PropertyFetch[]|StaticPropertyFetch[] $propertyFetches */
$propertyFetches = $this->betterNodeFinder->findInstancesOf($expr, [PropertyFetch::class, StaticPropertyFetch::class]);
foreach ($propertyFetches as $propertyFetch) {
$classReflection = $this->reflectionResolver->resolveClassReflectionSourceObject($propertyFetch);
if (!$classReflection instanceof ClassReflection) {
// cannot get parent Trait_ from Property Fetch
return \true;
}
$propertyName = (string) $this->nodeNameResolver->getName($propertyFetch);
if (!$classReflection->hasNativeProperty($propertyName)) {
continue;
}
$nativeProperty = $classReflection->getNativeProperty($propertyName);
if (!$nativeProperty->hasNativeType()) {
return \true;
}
}
return \false;
return (bool) $this->betterNodeFinder->findInstancesOf($expr, [PropertyFetch::class, StaticPropertyFetch::class, ArrayDimFetch::class]);
}
private function refactorIfWithBooleanAnd(If_ $if) : ?If_
{
Expand Down
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'e5037921c408b717756a3997e8111ae3729aef08';
public const PACKAGE_VERSION = '91ed251a8c450a776ec4b739c735b4bd6f9bf8d5';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-04-03 19:03:00';
public const RELEASE_DATE = '2024-04-03 19:11:18';
/**
* @var int
*/
Expand Down

0 comments on commit 81fd362

Please sign in to comment.