Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test_with_doctrine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
# do not intall doctrine/orm phpstan, it conflicts with Retor's one
composer install -d orm --no-dev

- run: bin/rector process orm/lib --config ci/config/rector-doctrine.yaml --autoload-file orm/vendor/autoload.php
- run: bin/rector process orm/lib --config ci/config/rector-doctrine.yaml --autoload-file orm/vendor/autoload.php --debug
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ private function addCall(Node $node): void
// one node can be of multiple-class types
$classType = $this->resolveClassType($node);

$methodName = $this->nodeNameResolver->getName($node);
$methodName = $this->nodeNameResolver->getName($node->name);
if ($classType instanceof MixedType) { // anonymous
return;
}
Expand Down
10 changes: 10 additions & 0 deletions packages/node-name-resolver/src/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Trait_;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
use Rector\NodeNameResolver\Regex\RegexPatternDetector;

Expand Down Expand Up @@ -85,6 +87,14 @@ public function isName(Node $node, string $name): bool

public function getName(Node $node): ?string
{
if ($node instanceof MethodCall || $node instanceof StaticCall) {
if ($node->name instanceof MethodCall || $node->name instanceof StaticCall) {
return null;
}

throw new ShouldNotHappenException(sprintf('Pick more specific node than "%s"', get_class($node)));
}

foreach ($this->nodeNameResolvers as $nodeNameResolver) {
if (! is_a($node, $nodeNameResolver->getNode(), true)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private function matchTypeAndMethodName(MethodCall $methodCall): ?array
continue;
}

$currentMethodName = $this->getName($methodCall);
$currentMethodName = $this->getName($methodCall->name);
if ($currentMethodName === null) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private function matchTypeAndMethodName(MethodCall $methodCall): ?array
continue;
}

$currentMethodName = $this->getName($methodCall);
$currentMethodName = $this->getName($methodCall->name);
if ($currentMethodName === null) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\For_;
use PHPStan\Analyser\Scope;
Expand Down Expand Up @@ -89,7 +91,8 @@ public function refactor(Node $node): ?Node
}

$countInCond = $node;
$valueName = $this->getName($node->args[0]->value);

$valueName = $this->resolveValueName($node);
$countedValueName = $this->createCountedValueName($valueName, $for->getAttribute(AttributeKey::SCOPE));

return new Variable($countedValueName);
Expand All @@ -111,4 +114,14 @@ protected function createCountedValueName(?string $valueName, ?Scope $scope): st

return parent::createCountedValueName($countedValueName, $scope);
}

private function resolveValueName(FuncCall $funcCall): ?string
{
$argumentValue = $funcCall->args[0]->value;
if ($argumentValue instanceof MethodCall || $argumentValue instanceof StaticCall) {
return $this->getName($argumentValue->name);
}

return $this->getName($argumentValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private function isParentCallMatching(ClassMethod $classMethod, ?StaticCall $sta
return false;
}

if (! $this->areNamesEqual($staticCall, $classMethod)) {
if (! $this->areNamesEqual($staticCall->name, $classMethod->name)) {
return false;
}

Expand Down Expand Up @@ -222,7 +222,8 @@ private function isParentClassMethodVisibilityOrDefaultOverride(
}

/** @var string $methodName */
$methodName = $this->getName($staticCall);
$methodName = $this->getName($staticCall->name);

$parentClassMethod = $this->functionLikeParsedNodesFinder->findMethod($methodName, $parentClassName);
if ($parentClassMethod !== null && $parentClassMethod->isProtected() && $classMethod->isPublic()) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private function shouldSkip(Node $node): bool
private function resolveDefaultValuesFromCall(Node $node): array
{
/** @var string|null $nodeName */
$nodeName = $this->getName($node);
$nodeName = $this->getName($node->name);
if ($nodeName === null) {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -134,6 +135,14 @@ private function isAfterMarkTestSkippedMethodCall(Node $node): bool
return false;
}

if ($node->name instanceof MethodCall) {
return false;
}

if ($node->name instanceof StaticCall) {
return false;
}

return $this->isName($node->name, 'markTestSkipped');
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private function resolveManagerRegistryCalledMethodNames(Class_ $class): array
return null;
}

$name = $this->getName($node);
$name = $this->getName($node->name);
if ($name === null) {
return null;
}
Expand Down
21 changes: 12 additions & 9 deletions rules/nette-tester-to-phpunit/src/AssertManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,17 @@ public function __construct(
*/
public function processStaticCall(StaticCall $staticCall): Node
{
if ($this->nodeNameResolver->isNames($staticCall, ['truthy', 'falsey'])) {
if ($this->nodeNameResolver->isNames($staticCall->name, ['truthy', 'falsey'])) {
return $this->processTruthyOrFalseyCall($staticCall);
}

if ($this->nodeNameResolver->isNames($staticCall, ['contains', 'notContains'])) {
if ($this->nodeNameResolver->isNames($staticCall->name, ['contains', 'notContains'])) {
$this->processContainsCall($staticCall);
} elseif ($this->nodeNameResolver->isNames($staticCall, ['exception', 'throws'])) {
} elseif ($this->nodeNameResolver->isNames($staticCall->name, ['exception', 'throws'])) {
$this->processExceptionCall($staticCall);
} elseif ($this->nodeNameResolver->isName($staticCall, 'type')) {
} elseif ($this->nodeNameResolver->isName($staticCall->name, 'type')) {
$this->processTypeCall($staticCall);
} elseif ($this->nodeNameResolver->isName($staticCall, 'noError')) {
} elseif ($this->nodeNameResolver->isName($staticCall->name, 'noError')) {
$this->processNoErrorCall($staticCall);
} else {
$this->renameAssertMethod($staticCall);
Expand All @@ -153,11 +153,14 @@ private function processContainsCall(StaticCall $staticCall): void
{
if ($this->stringTypeAnalyzer->isStringOrUnionStringOnlyType($staticCall->args[1]->value)) {
$name = $this->nodeNameResolver->isName(
$staticCall,
$staticCall->name,
'contains'
) ? 'assertStringContainsString' : 'assertStringNotContainsString';
} else {
$name = $this->nodeNameResolver->isName($staticCall, 'contains') ? 'assertContains' : 'assertNotContains';
$name = $this->nodeNameResolver->isName(
$staticCall->name,
'contains'
) ? 'assertContains' : 'assertNotContains';
}

$staticCall->name = new Identifier($name);
Expand Down Expand Up @@ -240,7 +243,7 @@ private function processNoErrorCall(StaticCall $staticCall): void
*/
private function processTruthyOrFalseyCall(StaticCall $staticCall): Expr
{
$method = $this->nodeNameResolver->isName($staticCall, 'truthy') ? 'assertTrue' : 'assertFalse';
$method = $this->nodeNameResolver->isName($staticCall->name, 'truthy') ? 'assertTrue' : 'assertFalse';

if (! $this->sholdBeStaticCall($staticCall)) {
$call = new MethodCall(new Variable('this'), $method);
Expand Down Expand Up @@ -296,7 +299,7 @@ private function refactorExpectExceptionCode(StaticCall $staticCall): void
private function renameAssertMethod(StaticCall $staticCall): void
{
foreach (self::ASSERT_METHODS_REMAP as $oldMethod => $newMethod) {
if (! $this->nodeNameResolver->isName($staticCall, $oldMethod)) {
if (! $this->nodeNameResolver->isName($staticCall->name, $oldMethod)) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions rules/nette-to-symfony/src/Route/RouteInfoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ public function createFromNode(Node $node): ?RouteInfo
return null;
}

if (! $this->nodeNameResolver->isNames($node, ['get', 'head', 'post', 'put', 'patch', 'delete'])) {
if (! $this->nodeNameResolver->isNames($node->name, ['get', 'head', 'post', 'put', 'patch', 'delete'])) {
return null;
}

/** @var string $methodName */
$methodName = $this->nodeNameResolver->getName($node);
$methodName = $this->nodeNameResolver->getName($node->name);
$uppercasedMethodName = strtoupper($methodName);

$methods = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ private function getVariableNameFor(Expr $expr, Scope $scope): string
{
if ($expr instanceof New_ && $expr->class instanceof Name) {
$name = $this->getShortName($expr->class);
} elseif ($expr instanceof MethodCall || $expr instanceof StaticCall) {
$name = $this->getName($expr->name);
} else {
$name = $this->getName($expr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private function processParentPhp4ConstructCall(Node $node): void
}

// it's not a parent PHP 4 constructor call
if (! $this->isName($node, $parentClassName)) {
if (! $this->isName($node->name, $parentClassName)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$methodName = $this->getName($node);
$methodName = $this->getName($node->name);

$className = $this->resolveStaticCallClassName($node);
if ($methodName === null || $className === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $this->isName($node, 'export')) {
if (! $this->isName($node->name, 'export')) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private function processMethodCall(MethodCall $methodCall): ?MethodCall
throw new ShouldNotHappenException();
}

$mockMethodName = $this->getName($methodCall->var);
$mockMethodName = $this->getName($methodCall->var->name);
if ($mockMethodName === null) {
throw new ShouldNotHappenException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function refactor(Node $node): ?Node
return new Clone_($this->testedObjectPropertyFetch);
}

$methodName = $this->getName($node);
$methodName = $this->getName($node->name);
if ($methodName === null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public function refactor(Node $node): ?Node
return null;
}

$node->name = new Identifier(self::OLD_METHODS_NAMES_TO_NEW_NAMES['string'][$this->getName($node)]);
$methodName = $this->getName($node->name);

$node->name = new Identifier(self::OLD_METHODS_NAMES_TO_NEW_NAMES['string'][$methodName]);

return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private function processFuncCallArgumentValue(Node $node, FuncCall $funcCall, Ar
private function renameMethod(Node $node, string $funcName): void
{
/** @var string $oldMethodName */
$oldMethodName = $this->getName($node);
$oldMethodName = $this->getName($node->name);

[$trueMethodName, $falseMethodName] = self::DEFAULT_OLD_TO_NEW_METHODS[$funcName];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function refactor(Node $node): ?Node
return null;
}

$oldMethodName = $this->getName($node);
$oldMethodName = $this->getName($node->name);
if ($oldMethodName === null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function refactor(Node $node): ?Node
}

foreach ($oldToNewMethods as $oldMethod => $newMethod) {
if (! $this->isName($node, $oldMethod)) {
if (! $this->isName($node->name, $oldMethod)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public function refactor(Node $node): ?Node
return null;
}

$name = $this->getName($node);
$name = $this->getName($node->name);

switch ($name) {
case 'setHeader':
return $this->modifySetHeader($node);
Expand Down
2 changes: 1 addition & 1 deletion rules/symfony/src/Rector/Class_/MakeCommandLazyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private function matchCommandNameNodeInConstruct(Expr $expr): ?Node
return null;
}

if (! $this->isName($expr, '__construct')) {
if (! $this->isName($expr->name, '__construct')) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion rules/symfony/src/Rector/Yaml/ParseFileRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'parse')) {
if (! $this->isName($node->name, 'parse')) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Php/Regex/RegexPatternArgumentManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private function processStaticCall(StaticCall $staticCall): array
}

foreach ($methodNamesToArgumentPosition as $methodName => $argumentPosition) {
if (! $this->nodeNameResolver->isName($staticCall, $methodName)) {
if (! $this->nodeNameResolver->isName($staticCall->name, $methodName)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function isTypeAndChainCalls(Node $node, Type $type, array $methods): boo
$methods = array_reverse($methods);

foreach ($methods as $method) {
$activeMethodName = $this->nodeNameResolver->getName($node);
$activeMethodName = $this->nodeNameResolver->getName($node->name);
if ($activeMethodName !== $method) {
return false;
}
Expand Down
12 changes: 11 additions & 1 deletion src/PhpParser/Node/Manipulator/IdentifierManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function renameNodeWithMap(Node $node, array $renameMethodMap): void
{
$this->ensureNodeHasIdentifier($node);

$oldNodeMethodName = $this->nodeNameResolver->getName($node);
$oldNodeMethodName = $this->resolveOldMethodName($node);
if ($oldNodeMethodName === null) {
return;
}
Expand Down Expand Up @@ -85,4 +85,14 @@ private function ensureNodeHasIdentifier(Node $node): void
implode('", "', self::NODE_CLASSES_WITH_IDENTIFIER)
));
}

private function resolveOldMethodName(Node $node)
{
if ($node instanceof StaticCall || $node instanceof MethodCall) {
$oldNodeMethodName = $this->nodeNameResolver->getName($node->name);
} else {
$oldNodeMethodName = $this->nodeNameResolver->getName($node);
}
return $oldNodeMethodName;
}
}
2 changes: 1 addition & 1 deletion src/PhpParser/Node/Manipulator/MethodCallManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function findMethodCallNamesOnVariable(Variable $variable): array

$methodCallNamesOnVariable = [];
foreach ($methodCallsOnVariable as $methodCallOnVariable) {
$methodName = $this->nodeNameResolver->getName($methodCallOnVariable);
$methodName = $this->nodeNameResolver->getName($methodCallOnVariable->name);
if ($methodName === null) {
continue;
}
Expand Down
Loading