prevent getName() on StaticCall or MethodCall

This commit is contained in:
TomasVotruba 2020-02-21 15:54:51 +01:00
parent 299714c5be
commit bc24bb12ff
32 changed files with 90 additions and 39 deletions

View File

@ -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

View File

@ -205,7 +205,7 @@ final class ParsedFunctionLikeNodeCollector
// 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;
}

View File

@ -8,9 +8,11 @@ use Nette\Utils\Strings;
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;
@ -85,6 +87,14 @@ final class NodeNameResolver
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;

View File

@ -103,7 +103,7 @@ PHP
continue;
}
$currentMethodName = $this->getName($methodCall);
$currentMethodName = $this->getName($methodCall->name);
if ($currentMethodName === null) {
continue;
}

View File

@ -106,7 +106,7 @@ PHP
continue;
}
$currentMethodName = $this->getName($methodCall);
$currentMethodName = $this->getName($methodCall->name);
if ($currentMethodName === null) {
continue;
}

View File

@ -7,6 +7,8 @@ namespace Rector\CodeQuality\Rector\For_;
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;
@ -89,7 +91,8 @@ PHP
}
$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);
@ -111,4 +114,14 @@ PHP
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);
}
}

View File

@ -160,7 +160,7 @@ PHP
return false;
}
if (! $this->areNamesEqual($staticCall, $classMethod)) {
if (! $this->areNamesEqual($staticCall->name, $classMethod->name)) {
return false;
}
@ -222,7 +222,8 @@ PHP
}
/** @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;

View File

@ -133,7 +133,7 @@ PHP
private function resolveDefaultValuesFromCall(Node $node): array
{
/** @var string|null $nodeName */
$nodeName = $this->getName($node);
$nodeName = $this->getName($node->name);
if ($nodeName === null) {
return [];
}

View File

@ -6,6 +6,7 @@ namespace Rector\DeadCode\Rector\Stmt;
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;
@ -134,6 +135,14 @@ PHP
return false;
}
if ($node->name instanceof MethodCall) {
return false;
}
if ($node->name instanceof StaticCall) {
return false;
}
return $this->isName($node->name, 'markTestSkipped');
});
}

View File

@ -147,7 +147,7 @@ PHP
return null;
}
$name = $this->getName($node);
$name = $this->getName($node->name);
if ($name === null) {
return null;
}

View File

@ -117,17 +117,17 @@ final class AssertManipulator
*/
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);
@ -153,11 +153,14 @@ final class AssertManipulator
{
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);
@ -240,7 +243,7 @@ final class AssertManipulator
*/
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);
@ -296,7 +299,7 @@ final class AssertManipulator
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;
}

View File

@ -58,12 +58,12 @@ final class RouteInfoFactory
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 = [];

View File

@ -170,6 +170,8 @@ final class NonVariableToVariableOnFunctionCallRector extends AbstractRector
{
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);
}

View File

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

View File

@ -90,7 +90,7 @@ PHP
*/
public function refactor(Node $node): ?Node
{
$methodName = $this->getName($node);
$methodName = $this->getName($node->name);
$className = $this->resolveStaticCallClassName($node);
if ($methodName === null || $className === null) {

View File

@ -59,7 +59,7 @@ PHP
return null;
}
if (! $this->isName($node, 'export')) {
if (! $this->isName($node->name, 'export')) {
return null;
}

View File

@ -107,7 +107,7 @@ final class PhpSpecMocksToPHPUnitMocksRector extends AbstractPhpSpecToPHPUnitRec
throw new ShouldNotHappenException();
}
$mockMethodName = $this->getName($methodCall->var);
$mockMethodName = $this->getName($methodCall->var->name);
if ($mockMethodName === null) {
throw new ShouldNotHappenException();
}

View File

@ -178,7 +178,7 @@ final class PhpSpecPromisesToPHPUnitAssertRector extends AbstractPhpSpecToPHPUni
return new Clone_($this->testedObjectPropertyFetch);
}
$methodName = $this->getName($node);
$methodName = $this->getName($node->name);
if ($methodName === null) {
return null;
}

View File

@ -86,7 +86,9 @@ PHP
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;
}

View File

@ -109,7 +109,7 @@ final class AssertCompareToSpecificMethodRector extends AbstractPHPUnitRector
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];

View File

@ -62,7 +62,7 @@ final class AssertRegExpRector extends AbstractPHPUnitRector
return null;
}
$oldMethodName = $this->getName($node);
$oldMethodName = $this->getName($node->name);
if ($oldMethodName === null) {
return null;
}

View File

@ -75,7 +75,7 @@ final class RenameStaticMethodRector extends AbstractRector
}
foreach ($oldToNewMethods as $oldMethod => $newMethod) {
if (! $this->isName($node, $oldMethod)) {
if (! $this->isName($node->name, $oldMethod)) {
continue;
}

View File

@ -67,7 +67,8 @@ PHP
return null;
}
$name = $this->getName($node);
$name = $this->getName($node->name);
switch ($name) {
case 'setHeader':
return $this->modifySetHeader($node);

View File

@ -105,7 +105,7 @@ PHP
return null;
}
if (! $this->isName($expr, '__construct')) {
if (! $this->isName($expr->name, '__construct')) {
return null;
}

View File

@ -42,7 +42,7 @@ final class ParseFileRector extends AbstractRector
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'parse')) {
if (! $this->isName($node->name, 'parse')) {
return null;
}

View File

@ -133,7 +133,7 @@ final class RegexPatternArgumentManipulator
}
foreach ($methodNamesToArgumentPosition as $methodName => $argumentPosition) {
if (! $this->nodeNameResolver->isName($staticCall, $methodName)) {
if (! $this->nodeNameResolver->isName($staticCall->name, $methodName)) {
continue;
}

View File

@ -48,7 +48,7 @@ final class ChainMethodCallManipulator
$methods = array_reverse($methods);
foreach ($methods as $method) {
$activeMethodName = $this->nodeNameResolver->getName($node);
$activeMethodName = $this->nodeNameResolver->getName($node->name);
if ($activeMethodName !== $method) {
return false;
}

View File

@ -48,7 +48,7 @@ final class IdentifierManipulator
{
$this->ensureNodeHasIdentifier($node);
$oldNodeMethodName = $this->nodeNameResolver->getName($node);
$oldNodeMethodName = $this->resolveOldMethodName($node);
if ($oldNodeMethodName === null) {
return;
}
@ -85,4 +85,14 @@ final class IdentifierManipulator
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;
}
}

View File

@ -51,7 +51,7 @@ final class MethodCallManipulator
$methodCallNamesOnVariable = [];
foreach ($methodCallsOnVariable as $methodCallOnVariable) {
$methodName = $this->nodeNameResolver->getName($methodCallOnVariable);
$methodName = $this->nodeNameResolver->getName($methodCallOnVariable->name);
if ($methodName === null) {
continue;
}

View File

@ -80,7 +80,7 @@ PHP
}
foreach ($positionByMethodName as $methodName => $positions) {
if (! $this->isName($node, $methodName)) {
if (! $this->isName($node->name, $methodName)) {
continue;
}

View File

@ -146,7 +146,7 @@ PHP
return false;
}
return $this->isName($node, $method);
return $this->isName($node->name, $method);
});
}
}

View File

@ -66,7 +66,7 @@ final class StaticCallToFunctionRector extends AbstractRector
}
foreach ($methodNamesToFunctions as $methodName => $function) {
if (! $this->isName($node, $methodName)) {
if (! $this->isName($node->name, $methodName)) {
continue;
}