Skip to content

Commit

Permalink
Drop AttributeKey::SCOPE in various classes (#3836)
Browse files Browse the repository at this point in the history
* Drop AttributeKey::SCOPE in various classes

* fix build
  • Loading branch information
staabm committed May 14, 2023
1 parent 6482c59 commit 499fdbc
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 52 deletions.
9 changes: 5 additions & 4 deletions packages/ReadWrite/NodeAnalyzer/ReadWritePropertyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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 All @@ -38,7 +39,7 @@ public function __construct(
) {
}

public function isRead(PropertyFetch | StaticPropertyFetch $node): bool
public function isRead(PropertyFetch | StaticPropertyFetch $node, Scope $scope): bool
{
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof Node) {
Expand Down Expand Up @@ -67,14 +68,14 @@ public function isRead(PropertyFetch | StaticPropertyFetch $node): bool
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 @@ -84,7 +85,7 @@ private function isArrayDimFetchInImpureFunction(ArrayDimFetch $arrayDimFetch, N
return false;
}

return ! $this->pureFunctionDetector->detect($parentArg);
return ! $this->pureFunctionDetector->detect($parentArg, $scope);
}
}

Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ parameters:
path: packages/Config/RectorConfig.php

# stmts refactoring
- '#Cognitive complexity for "Rector\\DeadCode\\Rector\\Assign\\RemoveDoubleAssignRector\:\:refactor\(\)" is \d+, keep it under 10#'
- '#Cognitive complexity for "Rector\\DeadCode\\Rector\\Assign\\RemoveDoubleAssignRector\:\:refactorWithScope\(\)" is \d+, keep it under 10#'

# empty parent construct
- '#Rector\\Core\\PhpParser\\NodeTraverser\\FileWithoutNamespaceNodeTraverser\:\:__construct\(\) does not call parent constructor from PhpParser\\NodeTraverser#'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
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
{
public function __construct(
private readonly SideEffectNodeDetector $sideEffectNodeDetector
Expand Down Expand Up @@ -65,7 +67,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 @@ -76,7 +78,7 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->sideEffectNodeDetector->detect($ternary->else)) {
if ($this->sideEffectNodeDetector->detect($ternary->else, $scope)) {
return null;
}

Expand Down
8 changes: 5 additions & 3 deletions rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
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
{
public function __construct(
private readonly SideEffectNodeDetector $sideEffectNodeDetector,
Expand Down Expand Up @@ -65,7 +67,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 @@ -107,7 +109,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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
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 @@ -123,11 +123,11 @@ private function cleanCastedExpr(Expr $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,
fn (Node $subNode): bool => $this->sideEffectNodeDetector->detectCallExpr($subNode)
fn (Node $subNode): bool => $this->sideEffectNodeDetector->detectCallExpr($subNode, $scope)
);
}

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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,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 @@ -22,7 +24,7 @@
/**
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector\RemoveUnusedPromotedPropertyRectorTest
*/
final class RemoveUnusedPromotedPropertyRector extends AbstractRector implements MinPhpVersionInterface
final class RemoveUnusedPromotedPropertyRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
public function __construct(
private readonly PropertyFetchFinder $propertyFetchFinder,
Expand Down Expand Up @@ -81,7 +83,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 @@ -101,7 +103,7 @@ public function refactor(Node $node): ?Node
continue;
}

if ($this->propertyManipulator->isPropertyUsedInReadContext($node, $param)) {
if ($this->propertyManipulator->isPropertyUsedInReadContext($node, $param, $scope)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
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 @@ -83,7 +85,7 @@ public function getNodeTypes(): array
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
$hasRemoved = false;

Expand All @@ -96,7 +98,7 @@ public function refactor(Node $node): ?Node
continue;
}

if ($this->propertyManipulator->isPropertyUsedInReadContext($node, $property)) {
if ($this->propertyManipulator->isPropertyUsedInReadContext($node, $property, $scope)) {
continue;
}

Expand All @@ -105,7 +107,8 @@ public function refactor(Node $node): ?Node
$isRemoved = $this->complexNodeRemover->removePropertyAndUsages(
$node,
$property,
$this->removeAssignSideEffect
$this->removeAssignSideEffect,
$scope
);

if ($isRemoved) {
Expand Down
4 changes: 2 additions & 2 deletions rules/DeadCode/SideEffect/PureFunctionDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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 @@ -124,14 +125,13 @@ public function __construct(
) {
}

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);
Expand Down
9 changes: 5 additions & 4 deletions rules/DeadCode/SideEffect/SideEffectNodeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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(
) {
}

public function detect(Expr $expr): bool
public function detect(Expr $expr, Scope $scope): bool
{
if ($expr instanceof Assign) {
return true;
Expand All @@ -65,7 +66,7 @@ public function detect(Expr $expr): bool
}

if ($expr instanceof FuncCall) {
return ! $this->pureFunctionDetector->detect($expr);
return ! $this->pureFunctionDetector->detect($expr, $scope);
}

if ($expr instanceof Variable || $expr instanceof ArrayDimFetch) {
Expand All @@ -77,7 +78,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 @@ -97,7 +98,7 @@ public function detectCallExpr(Node $node): bool
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,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 @@ -33,7 +35,7 @@
*
* @see \Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\ReadOnlyPropertyRectorTest
*/
final class ReadOnlyPropertyRector extends AbstractRector implements MinPhpVersionInterface
final class ReadOnlyPropertyRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
public function __construct(
private readonly PropertyManipulator $propertyManipulator,
Expand Down Expand Up @@ -92,13 +94,13 @@ 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 @@ -116,7 +118,7 @@ private function shouldSkipInReadonlyClass(Property|Param $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 @@ -139,7 +141,7 @@ private function refactorProperty(Property $property): ?Property
return null;
}

if ($this->propertyManipulator->isPropertyChangeableExceptConstructor($property)) {
if ($this->propertyManipulator->isPropertyChangeableExceptConstructor($property, $scope)) {
return null;
}

Expand All @@ -161,7 +163,7 @@ private function refactorProperty(Property $property): ?Property
return $property;
}

private function refactorParam(Param $param): Param | null
private function refactorParam(Param $param, Scope $scope): Param | null
{
if (! $this->visibilityManipulator->hasVisibility($param, Visibility::PRIVATE)) {
return null;
Expand All @@ -172,7 +174,7 @@ private function refactorParam(Param $param): Param | null
}

// promoted property?
if ($this->propertyManipulator->isPropertyChangeableExceptConstructor($param)) {
if ($this->propertyManipulator->isPropertyChangeableExceptConstructor($param, $scope)) {
return null;
}

Expand Down
Loading

0 comments on commit 499fdbc

Please sign in to comment.