Updated Rector to commit 499fdbc3af1779929444c170a778678e28be957a

499fdbc3af Drop AttributeKey::SCOPE in various classes (#3836)
This commit is contained in:
Tomas Votruba 2023-05-14 10:01:33 +00:00
parent 1c529e1b8f
commit f613b7a98a
16 changed files with 79 additions and 64 deletions

View File

@ -12,6 +12,7 @@ use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch; use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Stmt\Unset_; use PhpParser\Node\Stmt\Unset_;
use PHPStan\Analyser\Scope;
use Rector\Core\Exception\ShouldNotHappenException; use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeManipulator\AssignManipulator; use Rector\Core\NodeManipulator\AssignManipulator;
use Rector\Core\PhpParser\Node\BetterNodeFinder; use Rector\Core\PhpParser\Node\BetterNodeFinder;
@ -63,7 +64,7 @@ final class ReadWritePropertyAnalyzer
/** /**
* @param \PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\StaticPropertyFetch $node * @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); $parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if (!$parentNode instanceof Node) { if (!$parentNode instanceof Node) {
@ -86,12 +87,12 @@ final class ReadWritePropertyAnalyzer
if ($this->assignManipulator->isLeftPartOfAssign($parentNode)) { if ($this->assignManipulator->isLeftPartOfAssign($parentNode)) {
return \false; return \false;
} }
if (!$this->isArrayDimFetchInImpureFunction($parentNode, $node)) { if (!$this->isArrayDimFetchInImpureFunction($parentNode, $node, $scope)) {
return $this->isNotInsideIssetUnset($parentNode); return $this->isNotInsideIssetUnset($parentNode);
} }
return \false; return \false;
} }
private function isArrayDimFetchInImpureFunction(ArrayDimFetch $arrayDimFetch, Node $node) : bool private function isArrayDimFetchInImpureFunction(ArrayDimFetch $arrayDimFetch, Node $node, Scope $scope) : bool
{ {
if ($arrayDimFetch->var === $node) { if ($arrayDimFetch->var === $node) {
$arg = $this->betterNodeFinder->findParentType($arrayDimFetch, Arg::class); $arg = $this->betterNodeFinder->findParentType($arrayDimFetch, Arg::class);
@ -100,7 +101,7 @@ final class ReadWritePropertyAnalyzer
if (!$parentArg instanceof FuncCall) { if (!$parentArg instanceof FuncCall) {
return \false; return \false;
} }
return !$this->pureFunctionDetector->detect($parentArg); return !$this->pureFunctionDetector->detect($parentArg, $scope);
} }
} }
return \false; return \false;

View File

@ -8,14 +8,16 @@ use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\If_;
use PHPStan\Analyser\Scope;
use Rector\Core\Rector\AbstractRector; use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\DeadCode\SideEffect\SideEffectNodeDetector; use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/** /**
* @see \Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\TernaryFalseExpressionToIfRectorTest * @see \Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\TernaryFalseExpressionToIfRectorTest
*/ */
final class TernaryFalseExpressionToIfRector extends AbstractRector final class TernaryFalseExpressionToIfRector extends AbstractScopeAwareRector
{ {
/** /**
* @readonly * @readonly
@ -60,7 +62,7 @@ CODE_SAMPLE
/** /**
* @param Expression $node * @param Expression $node
*/ */
public function refactor(Node $node) : ?Node public function refactorWithScope(Node $node, Scope $scope) : ?Node
{ {
if (!$node->expr instanceof Ternary) { if (!$node->expr instanceof Ternary) {
return null; return null;
@ -69,7 +71,7 @@ CODE_SAMPLE
if (!$ternary->if instanceof Expr) { if (!$ternary->if instanceof Expr) {
return null; return null;
} }
if ($this->sideEffectNodeDetector->detect($ternary->else)) { if ($this->sideEffectNodeDetector->detect($ternary->else, $scope)) {
return null; return null;
} }
return new If_($ternary->cond, ['stmts' => [new Expression($ternary->if)]]); return new If_($ternary->cond, ['stmts' => [new Expression($ternary->if)]]);

View File

@ -15,15 +15,17 @@ use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Namespace_; use PhpParser\Node\Stmt\Namespace_;
use PHPStan\Analyser\Scope;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace; use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\Rector\AbstractRector; use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\DeadCode\SideEffect\SideEffectNodeDetector; use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/** /**
* @see \Rector\Tests\DeadCode\Rector\Assign\RemoveDoubleAssignRector\RemoveDoubleAssignRectorTest * @see \Rector\Tests\DeadCode\Rector\Assign\RemoveDoubleAssignRector\RemoveDoubleAssignRectorTest
*/ */
final class RemoveDoubleAssignRector extends AbstractRector final class RemoveDoubleAssignRector extends AbstractScopeAwareRector
{ {
/** /**
* @readonly * @readonly
@ -52,7 +54,7 @@ CODE_SAMPLE
/** /**
* @param Foreach_|FileWithoutNamespace|If_|Namespace_|ClassMethod|Function_|Closure $node * @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; $stmts = $node->stmts;
if ($stmts === null) { if ($stmts === null) {
@ -86,7 +88,7 @@ CODE_SAMPLE
} }
// detect call expression has side effect // detect call expression has side effect
// no calls on right, could hide e.g. array_pop()|array_shift() // 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; continue;
} }
if (!$stmt->expr->var instanceof Variable && !$stmt->expr->var instanceof PropertyFetch && !$stmt->expr->var instanceof StaticPropertyFetch) { if (!$stmt->expr->var instanceof Variable && !$stmt->expr->var instanceof PropertyFetch && !$stmt->expr->var instanceof StaticPropertyFetch) {

View File

@ -121,7 +121,7 @@ CODE_SAMPLE
if ($this->isUsed($node, $variable, $scope)) { if ($this->isUsed($node, $variable, $scope)) {
return $this->refactorUsedVariable($node, $scope); return $this->refactorUsedVariable($node, $scope);
} }
if ($this->hasCallLikeInAssignExpr($node->expr)) { if ($this->hasCallLikeInAssignExpr($node->expr, $scope)) {
// keep the expr, can have side effect // keep the expr, can have side effect
return $this->cleanCastedExpr($node->expr); return $this->cleanCastedExpr($node->expr);
} }
@ -136,10 +136,10 @@ CODE_SAMPLE
$castedExpr = $expr->expr; $castedExpr = $expr->expr;
return $this->cleanCastedExpr($castedExpr); 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 (bool) $this->betterNodeFinder->findFirst($expr, function (Node $subNode) use($scope) : bool {
return $this->sideEffectNodeDetector->detectCallExpr($subNode); return $this->sideEffectNodeDetector->detectCallExpr($subNode, $scope);
}); });
} }
private function shouldSkip(Assign $assign) : bool private function shouldSkip(Assign $assign) : bool
@ -178,7 +178,7 @@ CODE_SAMPLE
} }
/** @var FuncCall|MethodCall|New_|NullsafeMethodCall|StaticCall $expr */ /** @var FuncCall|MethodCall|New_|NullsafeMethodCall|StaticCall $expr */
$expr = $assign->expr; $expr = $assign->expr;
if (!$this->sideEffectNodeDetector->detectCallExpr($expr)) { if (!$this->sideEffectNodeDetector->detectCallExpr($expr, $scope)) {
return \false; return \false;
} }
return $this->isUsedInAssignExpr($expr, $assign, $scope); return $this->isUsedInAssignExpr($expr, $assign, $scope);

View File

@ -7,9 +7,11 @@ use PhpParser\Node;
use PhpParser\Node\Expr\Variable; use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use Rector\Core\NodeManipulator\PropertyManipulator; use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\PhpParser\NodeFinder\PropertyFetchFinder; use Rector\Core\PhpParser\NodeFinder\PropertyFetchFinder;
use Rector\Core\Rector\AbstractRector; use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\MethodName; use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature; use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Core\ValueObject\Visibility; use Rector\Core\ValueObject\Visibility;
@ -20,7 +22,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/** /**
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector\RemoveUnusedPromotedPropertyRectorTest * @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector\RemoveUnusedPromotedPropertyRectorTest
*/ */
final class RemoveUnusedPromotedPropertyRector extends AbstractRector implements MinPhpVersionInterface final class RemoveUnusedPromotedPropertyRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{ {
/** /**
* @readonly * @readonly
@ -86,7 +88,7 @@ CODE_SAMPLE
/** /**
* @param Class_ $node * @param Class_ $node
*/ */
public function refactor(Node $node) : ?Node public function refactorWithScope(Node $node, Scope $scope) : ?Node
{ {
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT); $constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
if (!$constructClassMethod instanceof ClassMethod) { if (!$constructClassMethod instanceof ClassMethod) {
@ -102,7 +104,7 @@ CODE_SAMPLE
if (!$this->visibilityManipulator->hasVisibility($param, Visibility::PRIVATE)) { if (!$this->visibilityManipulator->hasVisibility($param, Visibility::PRIVATE)) {
continue; continue;
} }
if ($this->propertyManipulator->isPropertyUsedInReadContext($node, $param)) { if ($this->propertyManipulator->isPropertyUsedInReadContext($node, $param, $scope)) {
continue; continue;
} }
$paramName = $this->getName($param); $paramName = $this->getName($param);

View File

@ -7,16 +7,18 @@ use PhpParser\Node;
use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Nop; use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Property; use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface; use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\NodeManipulator\PropertyManipulator; use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\Rector\AbstractRector; use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Removing\NodeManipulator\ComplexNodeRemover; use Rector\Removing\NodeManipulator\ComplexNodeRemover;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/** /**
* @see \Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\RemoveUnusedPrivatePropertyRectorTest * @see \Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\RemoveUnusedPrivatePropertyRectorTest
*/ */
final class RemoveUnusedPrivatePropertyRector extends AbstractRector implements AllowEmptyConfigurableRectorInterface final class RemoveUnusedPrivatePropertyRector extends AbstractScopeAwareRector implements AllowEmptyConfigurableRectorInterface
{ {
/** /**
* @api * @api
@ -76,7 +78,7 @@ CODE_SAMPLE
/** /**
* @param Class_ $node * @param Class_ $node
*/ */
public function refactor(Node $node) : ?Node public function refactorWithScope(Node $node, Scope $scope) : ?Node
{ {
$hasRemoved = \false; $hasRemoved = \false;
foreach ($node->stmts as $key => $property) { foreach ($node->stmts as $key => $property) {
@ -86,12 +88,12 @@ CODE_SAMPLE
if ($this->shouldSkipProperty($property)) { if ($this->shouldSkipProperty($property)) {
continue; continue;
} }
if ($this->propertyManipulator->isPropertyUsedInReadContext($node, $property)) { if ($this->propertyManipulator->isPropertyUsedInReadContext($node, $property, $scope)) {
continue; continue;
} }
// use different variable to avoid re-assign back $hasRemoved to false // use different variable to avoid re-assign back $hasRemoved to false
// when already asssigned to true // when already asssigned to true
$isRemoved = $this->complexNodeRemover->removePropertyAndUsages($node, $property, $this->removeAssignSideEffect); $isRemoved = $this->complexNodeRemover->removePropertyAndUsages($node, $property, $this->removeAssignSideEffect, $scope);
if ($isRemoved) { if ($isRemoved) {
$this->processRemoveSameLineComment($node, $property, $key); $this->processRemoveSameLineComment($node, $property, $key);
$hasRemoved = \true; $hasRemoved = \true;

View File

@ -5,6 +5,7 @@ namespace Rector\DeadCode\SideEffect;
use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name; use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\Native\NativeFunctionReflection; use PHPStan\Reflection\Native\NativeFunctionReflection;
use PHPStan\Reflection\ReflectionProvider; use PHPStan\Reflection\ReflectionProvider;
use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeNameResolver\NodeNameResolver;
@ -240,13 +241,12 @@ final class PureFunctionDetector
$this->nodeNameResolver = $nodeNameResolver; $this->nodeNameResolver = $nodeNameResolver;
$this->reflectionProvider = $reflectionProvider; $this->reflectionProvider = $reflectionProvider;
} }
public function detect(FuncCall $funcCall) : bool public function detect(FuncCall $funcCall, Scope $scope) : bool
{ {
$funcCallName = $this->nodeNameResolver->getName($funcCall); $funcCallName = $this->nodeNameResolver->getName($funcCall);
if ($funcCallName === null) { if ($funcCallName === null) {
return \false; return \false;
} }
$scope = $funcCall->getAttribute(AttributeKey::SCOPE);
$name = new Name($funcCallName); $name = new Name($funcCallName);
$hasFunction = $this->reflectionProvider->hasFunction($name, $scope); $hasFunction = $this->reflectionProvider->hasFunction($name, $scope);
if (!$hasFunction) { if (!$hasFunction) {

View File

@ -19,6 +19,7 @@ use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name; use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\Encapsed; use PhpParser\Node\Scalar\Encapsed;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ConstantType; use PHPStan\Type\ConstantType;
use PHPStan\Type\ObjectType; use PHPStan\Type\ObjectType;
use Rector\NodeTypeResolver\NodeTypeResolver; use Rector\NodeTypeResolver\NodeTypeResolver;
@ -47,7 +48,7 @@ final class SideEffectNodeDetector
$this->nodeTypeResolver = $nodeTypeResolver; $this->nodeTypeResolver = $nodeTypeResolver;
$this->pureFunctionDetector = $pureFunctionDetector; $this->pureFunctionDetector = $pureFunctionDetector;
} }
public function detect(Expr $expr) : bool public function detect(Expr $expr, Scope $scope) : bool
{ {
if ($expr instanceof Assign) { if ($expr instanceof Assign) {
return \true; return \true;
@ -62,7 +63,7 @@ final class SideEffectNodeDetector
return \false; return \false;
} }
if ($expr instanceof FuncCall) { if ($expr instanceof FuncCall) {
return !$this->pureFunctionDetector->detect($expr); return !$this->pureFunctionDetector->detect($expr, $scope);
} }
if ($expr instanceof Variable || $expr instanceof ArrayDimFetch) { if ($expr instanceof Variable || $expr instanceof ArrayDimFetch) {
$variable = $this->resolveVariable($expr); $variable = $this->resolveVariable($expr);
@ -71,7 +72,7 @@ final class SideEffectNodeDetector
} }
return \true; return \true;
} }
public function detectCallExpr(Node $node) : bool public function detectCallExpr(Node $node, Scope $scope) : bool
{ {
if (!$node instanceof Expr) { if (!$node instanceof Expr) {
return \false; return \false;
@ -87,7 +88,7 @@ final class SideEffectNodeDetector
return \true; return \true;
} }
if ($node instanceof FuncCall) { if ($node instanceof FuncCall) {
return !$this->pureFunctionDetector->detect($node); return !$this->pureFunctionDetector->detect($node, $scope);
} }
return \false; return \false;
} }

View File

@ -14,10 +14,12 @@ use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property; use PhpParser\Node\Stmt\Property;
use PhpParser\NodeTraverser; use PhpParser\NodeTraverser;
use PHPStan\Analyser\Scope;
use Rector\Core\NodeAnalyzer\ParamAnalyzer; use Rector\Core\NodeAnalyzer\ParamAnalyzer;
use Rector\Core\NodeManipulator\PropertyFetchAssignManipulator; use Rector\Core\NodeManipulator\PropertyFetchAssignManipulator;
use Rector\Core\NodeManipulator\PropertyManipulator; use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\Rector\AbstractRector; use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\MethodName; use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature; use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Core\ValueObject\Visibility; use Rector\Core\ValueObject\Visibility;
@ -31,7 +33,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
* *
* @see \Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\ReadOnlyPropertyRectorTest * @see \Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\ReadOnlyPropertyRectorTest
*/ */
final class ReadOnlyPropertyRector extends AbstractRector implements MinPhpVersionInterface final class ReadOnlyPropertyRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{ {
/** /**
* @readonly * @readonly
@ -102,12 +104,12 @@ CODE_SAMPLE
/** /**
* @param Property|Param $node * @param Property|Param $node
*/ */
public function refactor(Node $node) : ?Node public function refactorWithScope(Node $node, Scope $scope) : ?Node
{ {
if ($node instanceof Param) { 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 public function provideMinPhpVersion() : int
{ {
@ -124,7 +126,7 @@ CODE_SAMPLE
} }
return $class->isReadonly(); return $class->isReadonly();
} }
private function refactorProperty(Property $property) : ?Property private function refactorProperty(Property $property, Scope $scope) : ?Property
{ {
// 1. is property read-only? // 1. is property read-only?
if ($property->isReadonly()) { if ($property->isReadonly()) {
@ -142,7 +144,7 @@ CODE_SAMPLE
if (!$this->visibilityManipulator->hasVisibility($property, Visibility::PRIVATE)) { if (!$this->visibilityManipulator->hasVisibility($property, Visibility::PRIVATE)) {
return null; return null;
} }
if ($this->propertyManipulator->isPropertyChangeableExceptConstructor($property)) { if ($this->propertyManipulator->isPropertyChangeableExceptConstructor($property, $scope)) {
return null; return null;
} }
if ($this->propertyFetchAssignManipulator->isAssignedMultipleTimesInConstructor($property)) { if ($this->propertyFetchAssignManipulator->isAssignedMultipleTimesInConstructor($property)) {
@ -158,7 +160,7 @@ CODE_SAMPLE
} }
return $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)) { if (!$this->visibilityManipulator->hasVisibility($param, Visibility::PRIVATE)) {
return null; return null;
@ -167,7 +169,7 @@ CODE_SAMPLE
return null; return null;
} }
// promoted property? // promoted property?
if ($this->propertyManipulator->isPropertyChangeableExceptConstructor($param)) { if ($this->propertyManipulator->isPropertyChangeableExceptConstructor($param, $scope)) {
return null; return null;
} }
if ($this->visibilityManipulator->isReadonly($param)) { if ($this->visibilityManipulator->isReadonly($param)) {

View File

@ -8,9 +8,11 @@ use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property; use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty; use PhpParser\Node\Stmt\PropertyProperty;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ObjectType; use PHPStan\Type\ObjectType;
use Rector\Core\NodeManipulator\PropertyManipulator; use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\Rector\AbstractRector; use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Privatization\NodeFactory\ClassConstantFactory; use Rector\Privatization\NodeFactory\ClassConstantFactory;
use Rector\Privatization\NodeReplacer\PropertyFetchWithConstFetchReplacer; use Rector\Privatization\NodeReplacer\PropertyFetchWithConstFetchReplacer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@ -18,7 +20,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/** /**
* @see \Rector\Tests\Privatization\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector\ChangeReadOnlyPropertyWithDefaultValueToConstantRectorTest * @see \Rector\Tests\Privatization\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector\ChangeReadOnlyPropertyWithDefaultValueToConstantRectorTest
*/ */
final class ChangeReadOnlyPropertyWithDefaultValueToConstantRector extends AbstractRector final class ChangeReadOnlyPropertyWithDefaultValueToConstantRector extends AbstractScopeAwareRector
{ {
/** /**
* @readonly * @readonly
@ -93,7 +95,7 @@ CODE_SAMPLE
/** /**
* @param Property $node * @param Property $node
*/ */
public function refactor(Node $node) : ?Node public function refactorWithScope(Node $node, Scope $scope) : ?Node
{ {
if ($this->shouldSkip($node)) { if ($this->shouldSkip($node)) {
return null; return null;
@ -112,7 +114,7 @@ CODE_SAMPLE
return null; return null;
} }
// is property read only? // is property read only?
if ($this->propertyManipulator->isPropertyChangeable($class, $node)) { if ($this->propertyManipulator->isPropertyChangeable($class, $node, $scope)) {
return null; return null;
} }
/** @var Class_ $classLike */ /** @var Class_ $classLike */

View File

@ -15,6 +15,7 @@ use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property; use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer; use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\PhpParser\Comparing\NodeComparator; use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder; use Rector\Core\PhpParser\Node\BetterNodeFinder;
@ -71,12 +72,12 @@ final class ComplexNodeRemover
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer; $this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
$this->nodeComparator = $nodeComparator; $this->nodeComparator = $nodeComparator;
} }
public function removePropertyAndUsages(Class_ $class, Property $property, bool $removeAssignSideEffect) : bool public function removePropertyAndUsages(Class_ $class, Property $property, bool $removeAssignSideEffect, Scope $scope) : bool
{ {
$propertyName = $this->nodeNameResolver->getName($property); $propertyName = $this->nodeNameResolver->getName($property);
$totalPropertyFetch = $this->propertyFetchAnalyzer->countLocalPropertyFetchName($class, $propertyName); $totalPropertyFetch = $this->propertyFetchAnalyzer->countLocalPropertyFetchName($class, $propertyName);
$expressions = []; $expressions = [];
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($class->stmts, function (Node $node) use($removeAssignSideEffect, $propertyName, &$totalPropertyFetch, &$expressions) : ?Node { $this->simpleCallableNodeTraverser->traverseNodesWithCallable($class->stmts, function (Node $node) use($removeAssignSideEffect, $propertyName, &$totalPropertyFetch, &$expressions, $scope) : ?Node {
// here should be checked all expr like stmts that can hold assign, e.f. if, foreach etc. etc. // here should be checked all expr like stmts that can hold assign, e.f. if, foreach etc. etc.
if (!$node instanceof Expression) { if (!$node instanceof Expression) {
return null; return null;
@ -102,7 +103,7 @@ final class ComplexNodeRemover
$currentTotalPropertyFetch = $totalPropertyFetch; $currentTotalPropertyFetch = $totalPropertyFetch;
foreach ($propertyFetches as $propertyFetch) { foreach ($propertyFetches as $propertyFetch) {
if ($this->nodeNameResolver->isName($propertyFetch->name, $propertyName)) { if ($this->nodeNameResolver->isName($propertyFetch->name, $propertyName)) {
if (!$removeAssignSideEffect && $this->sideEffectNodeDetector->detect($assign->expr)) { if (!$removeAssignSideEffect && $this->sideEffectNodeDetector->detect($assign->expr, $scope)) {
return null; return null;
} }
--$totalPropertyFetch; --$totalPropertyFetch;

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api * @api
* @var string * @var string
*/ */
public const PACKAGE_VERSION = '6482c594e4b554a814165d11baeece9498868caa'; public const PACKAGE_VERSION = '499fdbc3af1779929444c170a778678e28be957a';
/** /**
* @api * @api
* @var string * @var string
*/ */
public const RELEASE_DATE = '2023-05-14 09:30:25'; public const RELEASE_DATE = '2023-05-14 16:57:42';
/** /**
* @var int * @var int
*/ */

View File

@ -155,7 +155,7 @@ final class PropertyManipulator
/** /**
* @param \PhpParser\Node\Stmt\Property|\PhpParser\Node\Param $propertyOrPromotedParam * @param \PhpParser\Node\Stmt\Property|\PhpParser\Node\Param $propertyOrPromotedParam
*/ */
public function isPropertyUsedInReadContext(Class_ $class, $propertyOrPromotedParam) : bool public function isPropertyUsedInReadContext(Class_ $class, $propertyOrPromotedParam, Scope $scope) : bool
{ {
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($propertyOrPromotedParam); $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($propertyOrPromotedParam);
if ($this->isAllowedReadOnly($propertyOrPromotedParam, $phpDocInfo)) { if ($this->isAllowedReadOnly($propertyOrPromotedParam, $phpDocInfo)) {
@ -163,7 +163,7 @@ final class PropertyManipulator
} }
$privatePropertyFetches = $this->propertyFetchFinder->findPrivatePropertyFetches($class, $propertyOrPromotedParam); $privatePropertyFetches = $this->propertyFetchFinder->findPrivatePropertyFetches($class, $propertyOrPromotedParam);
foreach ($privatePropertyFetches as $privatePropertyFetch) { foreach ($privatePropertyFetches as $privatePropertyFetch) {
if ($this->readWritePropertyAnalyzer->isRead($privatePropertyFetch)) { if ($this->readWritePropertyAnalyzer->isRead($privatePropertyFetch, $scope)) {
return \true; return \true;
} }
} }
@ -172,11 +172,11 @@ final class PropertyManipulator
if (!$classLike instanceof ClassLike) { if (!$classLike instanceof ClassLike) {
return \false; return \false;
} }
return (bool) $this->betterNodeFinder->findFirst($classLike->stmts, function (Node $node) : bool { return (bool) $this->betterNodeFinder->findFirst($classLike->stmts, function (Node $node) use($scope) : bool {
if (!$node instanceof PropertyFetch) { if (!$node instanceof PropertyFetch) {
return \false; return \false;
} }
if (!$this->readWritePropertyAnalyzer->isRead($node)) { if (!$this->readWritePropertyAnalyzer->isRead($node, $scope)) {
return \false; return \false;
} }
return $node->name instanceof Expr; return $node->name instanceof Expr;
@ -185,7 +185,7 @@ final class PropertyManipulator
/** /**
* @param \PhpParser\Node\Stmt\Property|\PhpParser\Node\Param $propertyOrParam * @param \PhpParser\Node\Stmt\Property|\PhpParser\Node\Param $propertyOrParam
*/ */
public function isPropertyChangeableExceptConstructor($propertyOrParam) : bool public function isPropertyChangeableExceptConstructor($propertyOrParam, Scope $scope) : bool
{ {
$class = $this->betterNodeFinder->findParentType($propertyOrParam, Class_::class); $class = $this->betterNodeFinder->findParentType($propertyOrParam, Class_::class);
// does not have parent type ClassLike? Possibly parent is changed by other rule // does not have parent type ClassLike? Possibly parent is changed by other rule
@ -201,7 +201,7 @@ final class PropertyManipulator
} }
$propertyFetches = $this->propertyFetchFinder->findPrivatePropertyFetches($class, $propertyOrParam); $propertyFetches = $this->propertyFetchFinder->findPrivatePropertyFetches($class, $propertyOrParam);
foreach ($propertyFetches as $propertyFetch) { foreach ($propertyFetches as $propertyFetch) {
if ($this->isChangeableContext($propertyFetch)) { if ($this->isChangeableContext($propertyFetch, $scope)) {
return \true; return \true;
} }
// skip for constructor? it is allowed to set value in constructor method // skip for constructor? it is allowed to set value in constructor method
@ -220,11 +220,11 @@ final class PropertyManipulator
} }
return \false; return \false;
} }
public function isPropertyChangeable(Class_ $class, Property $property) : bool public function isPropertyChangeable(Class_ $class, Property $property, Scope $scope) : bool
{ {
$propertyFetches = $this->propertyFetchFinder->findPrivatePropertyFetches($class, $property); $propertyFetches = $this->propertyFetchFinder->findPrivatePropertyFetches($class, $property);
foreach ($propertyFetches as $propertyFetch) { foreach ($propertyFetches as $propertyFetch) {
if ($this->isChangeableContext($propertyFetch)) { if ($this->isChangeableContext($propertyFetch, $scope)) {
return \true; return \true;
} }
if ($this->assignManipulator->isLeftPartOfAssign($propertyFetch)) { if ($this->assignManipulator->isLeftPartOfAssign($propertyFetch)) {
@ -289,7 +289,7 @@ final class PropertyManipulator
/** /**
* @param \PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\StaticPropertyFetch $propertyFetch * @param \PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\StaticPropertyFetch $propertyFetch
*/ */
private function isChangeableContext($propertyFetch) : bool private function isChangeableContext($propertyFetch, Scope $scope) : bool
{ {
$parentNode = $propertyFetch->getAttribute(AttributeKey::PARENT_NODE); $parentNode = $propertyFetch->getAttribute(AttributeKey::PARENT_NODE);
if (!$parentNode instanceof Node) { if (!$parentNode instanceof Node) {
@ -312,7 +312,7 @@ final class PropertyManipulator
} }
} }
if ($parentNode instanceof ArrayDimFetch) { if ($parentNode instanceof ArrayDimFetch) {
return !$this->readWritePropertyAnalyzer->isRead($propertyFetch); return !$this->readWritePropertyAnalyzer->isRead($propertyFetch, $scope);
} }
return $parentNode instanceof Unset_; return $parentNode instanceof Unset_;
} }

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php'; require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInite690da08c00c0d8835a53c8379f74349::getLoader(); return ComposerAutoloaderInitb9235c9149ee5db18dbd9ae10830c14d::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer // autoload_real.php @generated by Composer
class ComposerAutoloaderInite690da08c00c0d8835a53c8379f74349 class ComposerAutoloaderInitb9235c9149ee5db18dbd9ae10830c14d
{ {
private static $loader; private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInite690da08c00c0d8835a53c8379f74349
return self::$loader; return self::$loader;
} }
spl_autoload_register(array('ComposerAutoloaderInite690da08c00c0d8835a53c8379f74349', 'loadClassLoader'), true, true); spl_autoload_register(array('ComposerAutoloaderInitb9235c9149ee5db18dbd9ae10830c14d', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__)); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInite690da08c00c0d8835a53c8379f74349', 'loadClassLoader')); spl_autoload_unregister(array('ComposerAutoloaderInitb9235c9149ee5db18dbd9ae10830c14d', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php'; require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInite690da08c00c0d8835a53c8379f74349::getInitializer($loader)); call_user_func(\Composer\Autoload\ComposerStaticInitb9235c9149ee5db18dbd9ae10830c14d::getInitializer($loader));
$loader->setClassMapAuthoritative(true); $loader->setClassMapAuthoritative(true);
$loader->register(true); $loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInite690da08c00c0d8835a53c8379f74349::$files; $filesToLoad = \Composer\Autoload\ComposerStaticInitb9235c9149ee5db18dbd9ae10830c14d::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) { $requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload; namespace Composer\Autoload;
class ComposerStaticInite690da08c00c0d8835a53c8379f74349 class ComposerStaticInitb9235c9149ee5db18dbd9ae10830c14d
{ {
public static $files = array ( public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php', 'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3111,9 +3111,9 @@ class ComposerStaticInite690da08c00c0d8835a53c8379f74349
public static function getInitializer(ClassLoader $loader) public static function getInitializer(ClassLoader $loader)
{ {
return \Closure::bind(function () use ($loader) { return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInite690da08c00c0d8835a53c8379f74349::$prefixLengthsPsr4; $loader->prefixLengthsPsr4 = ComposerStaticInitb9235c9149ee5db18dbd9ae10830c14d::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInite690da08c00c0d8835a53c8379f74349::$prefixDirsPsr4; $loader->prefixDirsPsr4 = ComposerStaticInitb9235c9149ee5db18dbd9ae10830c14d::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInite690da08c00c0d8835a53c8379f74349::$classMap; $loader->classMap = ComposerStaticInitb9235c9149ee5db18dbd9ae10830c14d::$classMap;
}, null, ClassLoader::class); }, null, ClassLoader::class);
} }