Skip to content

Commit

Permalink
[DX] Move from CLASS_METHOD node attribute to parent finder (#1167)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 6, 2021
1 parent c3b6efe commit 824df97
Show file tree
Hide file tree
Showing 24 changed files with 66 additions and 62 deletions.
2 changes: 0 additions & 2 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
0,
\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE,
\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NAME,
\Rector\NodeTypeResolver\Node\AttributeKey::METHOD_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::NEXT_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::PREVIOUS_NODE,
Expand All @@ -73,7 +72,6 @@
0,
\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE,
\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NAME,
\Rector\NodeTypeResolver\Node\AttributeKey::METHOD_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::NEXT_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::PREVIOUS_NODE,
Expand Down
5 changes: 4 additions & 1 deletion packages/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class AttributeKey

/**
* @deprecated Use
* @see BetterNodeFinder to find your parent nodes.
* @see BetterNodeFinder::findParentType($node, ClassLike::class) to find your parent nodes.
*
* @var string
*/
Expand All @@ -52,6 +52,9 @@ final class AttributeKey
public const METHOD_NAME = 'methodName';

/**
* @deprecated Use
* @see BetterNodeFinder::findParentType($node, ClassMethod::class) to find your parent nodes.
*
* @var string
*/
public const METHOD_NODE = 'methodNode';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Contract\NodeTypeResolverInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -36,7 +37,8 @@ final class ParamTypeResolver implements NodeTypeResolverInterface
public function __construct(
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private NodeNameResolver $nodeNameResolver,
private PhpDocInfoFactory $phpDocInfoFactory
private PhpDocInfoFactory $phpDocInfoFactory,
private BetterNodeFinder $betterNodeFinder,
) {
}

Expand Down Expand Up @@ -90,7 +92,7 @@ private function resolveFromParamType(Param $param): Type

private function resolveFromFirstVariableUse(Param $param): Type
{
$classMethod = $param->getAttribute(AttributeKey::METHOD_NODE);
$classMethod = $this->betterNodeFinder->findParentType($param, ClassMethod::class);
if (! $classMethod instanceof ClassMethod) {
return new MixedType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
*/
final class VariableTypeResolver implements NodeTypeResolverInterface
{
/**
* @var string[]
*/
private const PARENT_NODE_ATTRIBUTES = [AttributeKey::PARENT_NODE, AttributeKey::METHOD_NODE];

public function __construct(
private NodeNameResolver $nodeNameResolver,
private PhpDocInfoFactory $phpDocInfoFactory
Expand Down Expand Up @@ -76,31 +71,21 @@ private function resolveTypesFromScope(Variable $variable, string $variableName)

private function resolveNodeScope(Variable $variable): ?Scope
{
/** @var Scope|null $nodeScope */
$nodeScope = $variable->getAttribute(AttributeKey::SCOPE);
if ($nodeScope !== null) {
return $nodeScope;
$scope = $variable->getAttribute(AttributeKey::SCOPE);
if ($scope instanceof Scope) {
return $scope;
}

return $this->resolveFromParentNodes($variable);
}

private function resolveFromParentNodes(Variable $variable): ?Scope
{
foreach (self::PARENT_NODE_ATTRIBUTES as $parentNodeAttribute) {
$parentNode = $variable->getAttribute($parentNodeAttribute);
if (! $parentNode instanceof Node) {
continue;
}

$parentNodeScope = $parentNode->getAttribute(AttributeKey::SCOPE);
if (! $parentNodeScope instanceof Scope) {
continue;
}

return $parentNodeScope;
$parent = $variable->getAttribute(AttributeKey::PARENT_NODE);
if (! $parent instanceof Node) {
return null;
}

return null;
return $parent->getAttribute(AttributeKey::SCOPE);
}
}
10 changes: 7 additions & 3 deletions rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
Expand Down Expand Up @@ -120,9 +121,12 @@ private function isSelfReferencing(Assign $assign): bool

private function areInSameClassMethod(Assign $assign, Expression $previousExpression): bool
{
return $this->nodeComparator->areNodesEqual(
$assign->getAttribute(AttributeKey::METHOD_NODE),
$previousExpression->getAttribute(AttributeKey::METHOD_NODE)
$assignClassMethod = $this->betterNodeFinder->findParentType($assign, ClassMethod::class);
$previousExpressionClassMethod = $this->betterNodeFinder->findParentType(
$previousExpression,
ClassMethod::class
);

return $this->nodeComparator->areNodesEqual($assignClassMethod, $previousExpressionClassMethod);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use Rector\Core\Php\ReservedKeywordAnalyzer;
Expand Down Expand Up @@ -114,7 +115,7 @@ public function refactor(Node $node): ?Node

private function shouldSkip(Assign $assign): bool
{
$classMethod = $assign->getAttribute(AttributeKey::METHOD_NODE);
$classMethod = $this->betterNodeFinder->findParentType($assign, ClassMethod::class);
if (! $classMethod instanceof FunctionLike) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function refactor(Node $node): ?Node
return null;
}

$classMethod = $node->getAttribute(AttributeKey::METHOD_NODE);
$classMethod = $this->betterNodeFinder->findParentType($node, ClassMethod::class);
if (! $classMethod instanceof ClassMethod) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@

use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class ControllerClassMethodAnalyzer
{
public function __construct(
private BetterNodeFinder $betterNodeFinder
) {
}

public function isInControllerActionMethod(Variable $variable): bool
{
/** @var string|null $className */
Expand All @@ -22,7 +28,7 @@ public function isInControllerActionMethod(Variable $variable): bool
return false;
}

$classMethod = $variable->getAttribute(AttributeKey::METHOD_NODE);
$classMethod = $this->betterNodeFinder->findParentType($variable, ClassMethod::class);
if (! $classMethod instanceof ClassMethod) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\Type\MixedType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeManipulator\ClassInsertManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
Expand Down Expand Up @@ -219,8 +220,12 @@ private function createPropertiesFromParams(array $params): array

private function decoratePropertyWithParamDocInfo(Param $param, Property $property): void
{
$constructor = $param->getAttribute(AttributeKey::METHOD_NODE);
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($constructor);
$constructorClassMethod = $this->betterNodeFinder->findParentType($param, ClassMethod::class);
if (! $constructorClassMethod instanceof ClassMethod) {
throw new ShouldNotHappenException();
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNode($constructorClassMethod);
if (! $phpDocInfo instanceof PhpDocInfo) {
return;
}
Expand Down
6 changes: 4 additions & 2 deletions rules/PhpSpecToPHPUnit/PhpSpecMockCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
Expand All @@ -33,7 +34,8 @@ final class PhpSpecMockCollector

public function __construct(
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private NodeNameResolver $nodeNameResolver
private NodeNameResolver $nodeNameResolver,
private BetterNodeFinder $betterNodeFinder,
) {
}

Expand Down Expand Up @@ -101,7 +103,7 @@ private function addMockFromParam(Param $param): void
/** @var string $class */
$class = $param->getAttribute(AttributeKey::CLASS_NAME);

$classMethod = $param->getAttribute(AttributeKey::METHOD_NODE);
$classMethod = $this->betterNodeFinder->findParentType($param, ClassMethod::class);
if (! $classMethod instanceof ClassMethod) {
throw new ShouldNotHappenException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private function createCreateMockCall(Param $param, Name $name): ?Expression

$variable = $this->getName($param->var);

$classMethod = $param->getAttribute(AttributeKey::METHOD_NODE);
$classMethod = $this->betterNodeFinder->findParentType($param, ClassMethod::class);
if (! $classMethod instanceof ClassMethod) {
throw new ShouldNotHappenException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function refactor(Node $node): ?Node
}

foreach ($readOnlyVariableAssigns as $readOnlyVariableAssign) {
$classMethod = $readOnlyVariableAssign->getAttribute(AttributeKey::METHOD_NODE);
$classMethod = $this->betterNodeFinder->findParentType($readOnlyVariableAssign, ClassMethod::class);
if (! $classMethod instanceof ClassMethod) {
throw new ShouldNotHappenException();
}
Expand Down
4 changes: 2 additions & 2 deletions rules/Removing/NodeManipulator/ComplexNodeRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function shouldSkipPropertyForClassMethod(
StaticPropertyFetch | PropertyFetch $expr,
array $classMethodNamesToSkip
): bool {
$classMethodNode = $expr->getAttribute(AttributeKey::METHOD_NODE);
$classMethodNode = $this->betterNodeFinder->findParentType($expr, ClassMethod::class);
if (! $classMethodNode instanceof ClassMethod) {
return false;
}
Expand All @@ -111,7 +111,7 @@ private function resolveAssign(PropertyFetch | StaticPropertyFetch $expr): ?Assi

private function removeConstructorDependency(Assign $assign): void
{
$classMethod = $assign->getAttribute(AttributeKey::METHOD_NODE);
$classMethod = $this->betterNodeFinder->findParentType($assign, ClassMethod::class);
if (! $classMethod instanceof ClassMethod) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private function refactorStaticCall(StaticCall $staticCall): ?MethodCall

private function isInStaticClassMethod(StaticCall $staticCall): bool
{
$locationClassMethod = $staticCall->getAttribute(AttributeKey::METHOD_NODE);
$locationClassMethod = $this->betterNodeFinder->findParentType($staticCall, ClassMethod::class);
if (! $locationClassMethod instanceof ClassMethod) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ObjectType;
use Rector\Core\Configuration\Option;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\Naming\Naming\PropertyNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -92,7 +92,11 @@ public function refactor(Node $node): ?Node

$propertyName = $this->propertyNaming->fqnToVariableName($staticObjectType);

$classMethod = $node->getAttribute(AttributeKey::METHOD_NODE);
$classMethod = $this->betterNodeFinder->findParentType($node, ClassMethod::class);
if (! $classMethod instanceof ClassMethod) {
return null;
}

if ($this->nodeNameResolver->isName($classMethod, MethodName::CONSTRUCT)) {
$propertyFetch = new Variable($propertyName);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Rector\Core\Rector\AbstractRector;
use Rector\Naming\Naming\PropertyNaming;
use Rector\Naming\ValueObject\ExpectedName;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\TypeAnalyzer\ArrayTypeAnalyzer;
use Rector\PostRector\Collector\PropertyToAddCollector;
use Rector\PostRector\ValueObject\PropertyMetadata;
Expand Down Expand Up @@ -161,10 +160,9 @@ public function configure(array $configuration): void

private function shouldSkipFuncCall(FuncCall $funcCall): bool
{
// we can inject only in injectable class method context
// we can inject only in injectable class method context
/** @var ClassMethod|null $classMethod */
$classMethod = $funcCall->getAttribute(AttributeKey::METHOD_NODE);
$classMethod = $this->betterNodeFinder->findParentType($funcCall, ClassMethod::class);
if (! $classMethod instanceof ClassMethod) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Transform\NodeAnalyzer\FuncCallStaticCallToMethodCallAnalyzer;
use Rector\Transform\ValueObject\FuncCallToMethodCall;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
Expand Down Expand Up @@ -98,7 +97,7 @@ public function refactor(Node $node): ?Node
return null;
}

$classMethod = $node->getAttribute(AttributeKey::METHOD_NODE);
$classMethod = $this->betterNodeFinder->findParentType($node, ClassMethod::class);
if (! $classMethod instanceof ClassMethod) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Transform\NodeAnalyzer\FuncCallStaticCallToMethodCallAnalyzer;
use Rector\Transform\ValueObject\StaticCallToMethodCall;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
Expand Down Expand Up @@ -112,7 +111,7 @@ public function refactor(Node $node): ?Node
return null;
}

$classMethod = $node->getAttribute(AttributeKey::METHOD_NODE);
$classMethod = $this->betterNodeFinder->findParentType($node, ClassMethod::class);
if (! $classMethod instanceof ClassMethod) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function inferParam(Param $param): Type
}

// what is return type?
$classMethod = $node->getAttribute(AttributeKey::METHOD_NODE);
$classMethod = $this->betterNodeFinder->findParentType($node, ClassMethod::class);
if (! $classMethod instanceof ClassMethod) {
return null;
}
Expand Down
Loading

0 comments on commit 824df97

Please sign in to comment.