Skip to content

Commit

Permalink
Updated Rector to commit 499fdbc3af1779929444c170a778678e28be957a
Browse files Browse the repository at this point in the history
rectorphp/rector-src@499fdbc Drop AttributeKey::SCOPE in various classes (#3836)
  • Loading branch information
TomasVotruba committed May 14, 2023
1 parent 1c529e1 commit f613b7a
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 64 deletions.
9 changes: 5 additions & 4 deletions packages/ReadWrite/NodeAnalyzer/ReadWritePropertyAnalyzer.php
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Stmt\Unset_;
use PHPStan\Analyser\Scope;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeManipulator\AssignManipulator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function __construct(AssignManipulator $assignManipulator, \Rector\ReadWr
/**
* @param \PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\StaticPropertyFetch $node
*/
public function isRead($node) : bool
public function isRead($node, Scope $scope) : bool
{
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if (!$parentNode instanceof Node) {
Expand All @@ -86,12 +87,12 @@ public function isRead($node) : bool
if ($this->assignManipulator->isLeftPartOfAssign($parentNode)) {
return \false;
}
if (!$this->isArrayDimFetchInImpureFunction($parentNode, $node)) {
if (!$this->isArrayDimFetchInImpureFunction($parentNode, $node, $scope)) {
return $this->isNotInsideIssetUnset($parentNode);
}
return \false;
}
private function isArrayDimFetchInImpureFunction(ArrayDimFetch $arrayDimFetch, Node $node) : bool
private function isArrayDimFetchInImpureFunction(ArrayDimFetch $arrayDimFetch, Node $node, Scope $scope) : bool
{
if ($arrayDimFetch->var === $node) {
$arg = $this->betterNodeFinder->findParentType($arrayDimFetch, Arg::class);
Expand All @@ -100,7 +101,7 @@ private function isArrayDimFetchInImpureFunction(ArrayDimFetch $arrayDimFetch, N
if (!$parentArg instanceof FuncCall) {
return \false;
}
return !$this->pureFunctionDetector->detect($parentArg);
return !$this->pureFunctionDetector->detect($parentArg, $scope);
}
}
return \false;
Expand Down
Expand Up @@ -8,14 +8,16 @@
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PHPStan\Analyser\Scope;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\TernaryFalseExpressionToIfRectorTest
*/
final class TernaryFalseExpressionToIfRector extends AbstractRector
final class TernaryFalseExpressionToIfRector extends AbstractScopeAwareRector
{
/**
* @readonly
Expand Down Expand Up @@ -60,7 +62,7 @@ public function getNodeTypes() : array
/**
* @param Expression $node
*/
public function refactor(Node $node) : ?Node
public function refactorWithScope(Node $node, Scope $scope) : ?Node
{
if (!$node->expr instanceof Ternary) {
return null;
Expand All @@ -69,7 +71,7 @@ public function refactor(Node $node) : ?Node
if (!$ternary->if instanceof Expr) {
return null;
}
if ($this->sideEffectNodeDetector->detect($ternary->else)) {
if ($this->sideEffectNodeDetector->detect($ternary->else, $scope)) {
return null;
}
return new If_($ternary->cond, ['stmts' => [new Expression($ternary->if)]]);
Expand Down
8 changes: 5 additions & 3 deletions rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php
Expand Up @@ -15,15 +15,17 @@
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Namespace_;
use PHPStan\Analyser\Scope;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\Assign\RemoveDoubleAssignRector\RemoveDoubleAssignRectorTest
*/
final class RemoveDoubleAssignRector extends AbstractRector
final class RemoveDoubleAssignRector extends AbstractScopeAwareRector
{
/**
* @readonly
Expand Down Expand Up @@ -52,7 +54,7 @@ public function getNodeTypes() : array
/**
* @param Foreach_|FileWithoutNamespace|If_|Namespace_|ClassMethod|Function_|Closure $node
*/
public function refactor(Node $node) : ?Node
public function refactorWithScope(Node $node, Scope $scope) : ?Node
{
$stmts = $node->stmts;
if ($stmts === null) {
Expand Down Expand Up @@ -86,7 +88,7 @@ public function refactor(Node $node) : ?Node
}
// detect call expression has side effect
// no calls on right, could hide e.g. array_pop()|array_shift()
if ($this->sideEffectNodeDetector->detectCallExpr($stmt->expr->expr)) {
if ($this->sideEffectNodeDetector->detectCallExpr($stmt->expr->expr, $scope)) {
continue;
}
if (!$stmt->expr->var instanceof Variable && !$stmt->expr->var instanceof PropertyFetch && !$stmt->expr->var instanceof StaticPropertyFetch) {
Expand Down
Expand Up @@ -121,7 +121,7 @@ public function refactorWithScope(Node $node, Scope $scope) : ?Node
if ($this->isUsed($node, $variable, $scope)) {
return $this->refactorUsedVariable($node, $scope);
}
if ($this->hasCallLikeInAssignExpr($node->expr)) {
if ($this->hasCallLikeInAssignExpr($node->expr, $scope)) {
// keep the expr, can have side effect
return $this->cleanCastedExpr($node->expr);
}
Expand All @@ -136,10 +136,10 @@ private function cleanCastedExpr(Expr $expr) : Expr
$castedExpr = $expr->expr;
return $this->cleanCastedExpr($castedExpr);
}
private function hasCallLikeInAssignExpr(Expr $expr) : bool
private function hasCallLikeInAssignExpr(Expr $expr, Scope $scope) : bool
{
return (bool) $this->betterNodeFinder->findFirst($expr, function (Node $subNode) : bool {
return $this->sideEffectNodeDetector->detectCallExpr($subNode);
return (bool) $this->betterNodeFinder->findFirst($expr, function (Node $subNode) use($scope) : bool {
return $this->sideEffectNodeDetector->detectCallExpr($subNode, $scope);
});
}
private function shouldSkip(Assign $assign) : bool
Expand Down Expand Up @@ -178,7 +178,7 @@ private function isUsed(Assign $assign, Variable $variable, Scope $scope) : bool
}
/** @var FuncCall|MethodCall|New_|NullsafeMethodCall|StaticCall $expr */
$expr = $assign->expr;
if (!$this->sideEffectNodeDetector->detectCallExpr($expr)) {
if (!$this->sideEffectNodeDetector->detectCallExpr($expr, $scope)) {
return \false;
}
return $this->isUsedInAssignExpr($expr, $assign, $scope);
Expand Down
Expand Up @@ -7,9 +7,11 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\PhpParser\NodeFinder\PropertyFetchFinder;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Core\ValueObject\Visibility;
Expand All @@ -20,7 +22,7 @@
/**
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector\RemoveUnusedPromotedPropertyRectorTest
*/
final class RemoveUnusedPromotedPropertyRector extends AbstractRector implements MinPhpVersionInterface
final class RemoveUnusedPromotedPropertyRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
/**
* @readonly
Expand Down Expand Up @@ -86,7 +88,7 @@ public function getNodeTypes() : array
/**
* @param Class_ $node
*/
public function refactor(Node $node) : ?Node
public function refactorWithScope(Node $node, Scope $scope) : ?Node
{
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
if (!$constructClassMethod instanceof ClassMethod) {
Expand All @@ -102,7 +104,7 @@ public function refactor(Node $node) : ?Node
if (!$this->visibilityManipulator->hasVisibility($param, Visibility::PRIVATE)) {
continue;
}
if ($this->propertyManipulator->isPropertyUsedInReadContext($node, $param)) {
if ($this->propertyManipulator->isPropertyUsedInReadContext($node, $param, $scope)) {
continue;
}
$paramName = $this->getName($param);
Expand Down
Expand Up @@ -7,16 +7,18 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Removing\NodeManipulator\ComplexNodeRemover;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\RemoveUnusedPrivatePropertyRectorTest
*/
final class RemoveUnusedPrivatePropertyRector extends AbstractRector implements AllowEmptyConfigurableRectorInterface
final class RemoveUnusedPrivatePropertyRector extends AbstractScopeAwareRector implements AllowEmptyConfigurableRectorInterface
{
/**
* @api
Expand Down Expand Up @@ -76,7 +78,7 @@ public function getNodeTypes() : array
/**
* @param Class_ $node
*/
public function refactor(Node $node) : ?Node
public function refactorWithScope(Node $node, Scope $scope) : ?Node
{
$hasRemoved = \false;
foreach ($node->stmts as $key => $property) {
Expand All @@ -86,12 +88,12 @@ public function refactor(Node $node) : ?Node
if ($this->shouldSkipProperty($property)) {
continue;
}
if ($this->propertyManipulator->isPropertyUsedInReadContext($node, $property)) {
if ($this->propertyManipulator->isPropertyUsedInReadContext($node, $property, $scope)) {
continue;
}
// use different variable to avoid re-assign back $hasRemoved to false
// when already asssigned to true
$isRemoved = $this->complexNodeRemover->removePropertyAndUsages($node, $property, $this->removeAssignSideEffect);
$isRemoved = $this->complexNodeRemover->removePropertyAndUsages($node, $property, $this->removeAssignSideEffect, $scope);
if ($isRemoved) {
$this->processRemoveSameLineComment($node, $property, $key);
$hasRemoved = \true;
Expand Down
4 changes: 2 additions & 2 deletions rules/DeadCode/SideEffect/PureFunctionDetector.php
Expand Up @@ -5,6 +5,7 @@

use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\Native\NativeFunctionReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\NodeNameResolver\NodeNameResolver;
Expand Down Expand Up @@ -240,13 +241,12 @@ public function __construct(NodeNameResolver $nodeNameResolver, ReflectionProvid
$this->nodeNameResolver = $nodeNameResolver;
$this->reflectionProvider = $reflectionProvider;
}
public function detect(FuncCall $funcCall) : bool
public function detect(FuncCall $funcCall, Scope $scope) : bool
{
$funcCallName = $this->nodeNameResolver->getName($funcCall);
if ($funcCallName === null) {
return \false;
}
$scope = $funcCall->getAttribute(AttributeKey::SCOPE);
$name = new Name($funcCallName);
$hasFunction = $this->reflectionProvider->hasFunction($name, $scope);
if (!$hasFunction) {
Expand Down
9 changes: 5 additions & 4 deletions rules/DeadCode/SideEffect/SideEffectNodeDetector.php
Expand Up @@ -19,6 +19,7 @@
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\Encapsed;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ConstantType;
use PHPStan\Type\ObjectType;
use Rector\NodeTypeResolver\NodeTypeResolver;
Expand Down Expand Up @@ -47,7 +48,7 @@ public function __construct(NodeTypeResolver $nodeTypeResolver, \Rector\DeadCode
$this->nodeTypeResolver = $nodeTypeResolver;
$this->pureFunctionDetector = $pureFunctionDetector;
}
public function detect(Expr $expr) : bool
public function detect(Expr $expr, Scope $scope) : bool
{
if ($expr instanceof Assign) {
return \true;
Expand All @@ -62,7 +63,7 @@ public function detect(Expr $expr) : bool
return \false;
}
if ($expr instanceof FuncCall) {
return !$this->pureFunctionDetector->detect($expr);
return !$this->pureFunctionDetector->detect($expr, $scope);
}
if ($expr instanceof Variable || $expr instanceof ArrayDimFetch) {
$variable = $this->resolveVariable($expr);
Expand All @@ -71,7 +72,7 @@ public function detect(Expr $expr) : bool
}
return \true;
}
public function detectCallExpr(Node $node) : bool
public function detectCallExpr(Node $node, Scope $scope) : bool
{
if (!$node instanceof Expr) {
return \false;
Expand All @@ -87,7 +88,7 @@ public function detectCallExpr(Node $node) : bool
return \true;
}
if ($node instanceof FuncCall) {
return !$this->pureFunctionDetector->detect($node);
return !$this->pureFunctionDetector->detect($node, $scope);
}
return \false;
}
Expand Down
18 changes: 10 additions & 8 deletions rules/Php81/Rector/Property/ReadOnlyPropertyRector.php
Expand Up @@ -14,10 +14,12 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PhpParser\NodeTraverser;
use PHPStan\Analyser\Scope;
use Rector\Core\NodeAnalyzer\ParamAnalyzer;
use Rector\Core\NodeManipulator\PropertyFetchAssignManipulator;
use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Core\ValueObject\Visibility;
Expand All @@ -31,7 +33,7 @@
*
* @see \Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\ReadOnlyPropertyRectorTest
*/
final class ReadOnlyPropertyRector extends AbstractRector implements MinPhpVersionInterface
final class ReadOnlyPropertyRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
/**
* @readonly
Expand Down Expand Up @@ -102,12 +104,12 @@ public function getNodeTypes() : array
/**
* @param Property|Param $node
*/
public function refactor(Node $node) : ?Node
public function refactorWithScope(Node $node, Scope $scope) : ?Node
{
if ($node instanceof Param) {
return $this->refactorParam($node);
return $this->refactorParam($node, $scope);
}
return $this->refactorProperty($node);
return $this->refactorProperty($node, $scope);
}
public function provideMinPhpVersion() : int
{
Expand All @@ -124,7 +126,7 @@ private function shouldSkipInReadonlyClass($node) : bool
}
return $class->isReadonly();
}
private function refactorProperty(Property $property) : ?Property
private function refactorProperty(Property $property, Scope $scope) : ?Property
{
// 1. is property read-only?
if ($property->isReadonly()) {
Expand All @@ -142,7 +144,7 @@ private function refactorProperty(Property $property) : ?Property
if (!$this->visibilityManipulator->hasVisibility($property, Visibility::PRIVATE)) {
return null;
}
if ($this->propertyManipulator->isPropertyChangeableExceptConstructor($property)) {
if ($this->propertyManipulator->isPropertyChangeableExceptConstructor($property, $scope)) {
return null;
}
if ($this->propertyFetchAssignManipulator->isAssignedMultipleTimesInConstructor($property)) {
Expand All @@ -158,7 +160,7 @@ private function refactorProperty(Property $property) : ?Property
}
return $property;
}
private function refactorParam(Param $param) : ?\PhpParser\Node\Param
private function refactorParam(Param $param, Scope $scope) : ?\PhpParser\Node\Param
{
if (!$this->visibilityManipulator->hasVisibility($param, Visibility::PRIVATE)) {
return null;
Expand All @@ -167,7 +169,7 @@ private function refactorParam(Param $param) : ?\PhpParser\Node\Param
return null;
}
// promoted property?
if ($this->propertyManipulator->isPropertyChangeableExceptConstructor($param)) {
if ($this->propertyManipulator->isPropertyChangeableExceptConstructor($param, $scope)) {
return null;
}
if ($this->visibilityManipulator->isReadonly($param)) {
Expand Down

0 comments on commit f613b7a

Please sign in to comment.