mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 05:48:21 +01:00
Fixes from PHPStan
This commit is contained in:
parent
5a963a6508
commit
32617ecbac
@ -25,7 +25,7 @@ final class PropertyFetchTypeResolverTest extends AbstractNodeTypeResolverTest
|
||||
$propertyFetchNodes = $this->getNodesForFileOfType($file, PropertyFetch::class);
|
||||
$propertyFetchNode = $propertyFetchNodes[$nodePosition];
|
||||
|
||||
$this->assertSame($propertyName, $propertyFetchNode->name->toString());
|
||||
$this->assertSame($propertyName, (string) $propertyFetchNode->name);
|
||||
$this->assertSame($expectedTypes, $this->nodeTypeResolver->resolve($propertyFetchNode));
|
||||
}
|
||||
|
||||
|
23
phpstan.neon
23
phpstan.neon
@ -19,37 +19,30 @@ parameters:
|
||||
# known values
|
||||
- '#Argument of an invalid type array<PhpParser\\Node>\|null supplied for foreach, only iterables are supported#' # 1
|
||||
- '#Access to an undefined property PHPStan\\PhpDocParser\\Ast\\Node::\$name#' # 2
|
||||
- '#Calling method getName\(\) on possibly null value of type Rector\\Builder\\Class_\\VariableInfo\|null#' # 1
|
||||
|
||||
- '#Method Rector\\Node\\NodeFactory::create(Null|False)Constant\(\) should return PhpParser\\Node\\Expr\\ConstFetch but returns PhpParser\\Node\\Expr#' # 1
|
||||
- '#Method Rector\\Node\\NodeFactory::createNullConstant\(\) should return PhpParser\\Node\\Expr\\ConstFetch but returns PhpParser\\Node\\Expr#' # 1
|
||||
- '#Method Rector\\Node\\NodeFactory::createNamespace\(\) should return PhpParser\\Node\\Stmt\\Namespace_ but returns PhpParser\\Node#' # 1
|
||||
- '#Calling method getText\(\) on possibly null value of type PhpParser\\Comment\\Doc\|null#' # 3
|
||||
- '#Instanceof between PhpParser\\Node\\Expr\|string and PhpParser\\Node\\Identifier will always evaluate to false#' # 8
|
||||
- '#Instanceof between PhpParser\\Node\\Expr\|string and PhpParser\\Node\\(Identifier|Name) will always evaluate to false#' # 9
|
||||
- '#Method Rector\\NodeTraverserQueue\\BetterNodeFinder::findFirstAncestorInstanceOf\(\) should return PhpParser\\Node\|null but returns object#' # 1
|
||||
- '#Access to an undefined property PhpParser\\Node\\Stmt\\ClassLike::\$namespacedName#' # 2
|
||||
- '#Access to an undefined property PhpParser\\Node\\Stmt\\Class_::\$namespacedName#' # 1
|
||||
- '#Access to an undefined property PhpParser\\Node\\Stmt\\(Class_|ClassLike)::\$namespacedName#' # 3
|
||||
- '#Property Rector\\NodeTypeResolver\\NodeVisitor\\NamespaceResolver::\$useNodes \(array<PhpParser\\Node\\Stmt\\Use_>\) does not accept array<PhpParser\\Node>#' # 1
|
||||
- '#Parameter \#1 \$classMethodNode of method Rector\\NodeTypeResolver\\TypesExtractor\\ConstructorPropertyTypesExtractor::extractPropertiesFromConstructorMethodNode\(\) expects PhpParser\\Node\\Stmt\\ClassMethod, PhpParser\\Node given#' # 1
|
||||
- '#Instanceof between PhpParser\\Node\\Expr\|string and PhpParser\\Node\\Name will always evaluate to false#' # 1
|
||||
- '#Parameter \#1 \$node of method Rector\\NodeTypeResolver\\NodeTypeResolver::resolve\(\) expects PhpParser\\Node, PhpParser\\Node\\Expr|string given#' # 3
|
||||
- '#Method Rector\\NodeTraverser\\RectorNodeTraverser::getRectors\(\) should return array<Rector\\Contract\\Rector\\RectorInterface> but returns array<PhpParser\\NodeVisitor>#' # 1
|
||||
|
||||
# known value of Name of MethodCall
|
||||
- '#Cannot call method toString\(\) on PhpParser\\Node\\Expr\|string#' # 4
|
||||
|
||||
# buggy
|
||||
- '#Access to an undefined property PhpParser\\Node::\$args#' # 8
|
||||
- '#Access to an undefined property PhpParser\\Node\\Expr::\$value#' # 2
|
||||
- '#Parameter \#1 \$classLikeNode of method Rector\\NodeAnalyzer\\ClassLikeAnalyzer::resolveExtendsTypes\(\) expects PhpParser\\Node\\Stmt\\Class_\|PhpParser\\Node\\Stmt\\Interface_, PhpParser\\Node\\Stmt\\ClassLike given#' # 1
|
||||
- '#Parameter \#1 \$functionLikeNode of method Rector\\NodeTypeResolver\\TypeContext::getFunctionReflection\(\) expects PhpParser\\Node\\Expr\\Closure\|PhpParser\\Node\\Stmt\\ClassMethod\|PhpParser\\Node\\Stmt\\Function_, PhpParser\\Node\\FunctionLike given#' # 1
|
||||
- '#Access to an undefined property PhpParser\\Node\\Param::\$var#' # 4
|
||||
- '#Call to an undefined method PhpParser\\BuilderFactory::(classConstFetch|methodCall|staticCall)\(\)#' # 8
|
||||
- '#Call to an undefined method PhpParser\\BuilderFactory::args\(\)#' # 1
|
||||
- '#Call to an undefined method PhpParser\\BuilderFactory::(args|classConstFetch|methodCall|staticCall)\(\)#' # 9
|
||||
- "#Casting to string something that's already string#" # 4
|
||||
- '#Call to an undefined method PhpParser\\Node\\Stmt\\UseUse::getAlias\(\)#' # 2
|
||||
- '#Call to an undefined method PhpParser\\PrettyPrinter\\Standard::printFormatPreserving\(\)#' # 1
|
||||
- '#Access to an undefined property PhpParser\\Node\\Expr::\$var#' # 1
|
||||
- '#Access to an undefined property PhpParser\\Node\\Expr::\$name#' # 2
|
||||
- '#Call to an undefined method PhpParser\\Node\\Expr|PhpParser\\Node\\Name::toCodeString\(\).#' # 9
|
||||
- '#Access to an undefined property PhpParser\\Node\\Expr::\$(name|var)#' # 2
|
||||
- '#Call to an undefined method PhpParser\\Node\\Expr\\BinaryOp::getOperatorSigil\(\).#' # 1
|
||||
- '#Call to an undefined method PhpParser\\Node\\Expr\\ArrayItem::getComments\(\).#' # 2
|
||||
|
||||
excludes_analyse:
|
||||
# test files
|
||||
|
@ -36,20 +36,18 @@ final class ChainMethodCallAnalyzer
|
||||
// node chaining is in reverse order than code
|
||||
$methods = array_reverse($methods);
|
||||
|
||||
$currentMethodCall = $node;
|
||||
|
||||
foreach ($methods as $method) {
|
||||
if ((string) $currentMethodCall->name !== $method) {
|
||||
if ((string) $node->name !== $method) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$currentMethodCall = $currentMethodCall->var;
|
||||
if ($currentMethodCall instanceof MethodCall) {
|
||||
$node = $node->var;
|
||||
if ($node instanceof MethodCall) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$variableTypes = $this->nodeTypeResolver->resolve($currentMethodCall);
|
||||
$variableTypes = $this->nodeTypeResolver->resolve($node);
|
||||
|
||||
return in_array($type, $variableTypes, true);
|
||||
}
|
||||
|
@ -33,16 +33,21 @@ final class MethodArgumentAnalyzer
|
||||
return false;
|
||||
}
|
||||
|
||||
return $node->args[$position - 1]->value instanceof String_;
|
||||
/** @var MethodCall $methodCallNode */
|
||||
$methodCallNode = $node;
|
||||
|
||||
return $methodCallNode->args[$position - 1]->value instanceof String_;
|
||||
}
|
||||
|
||||
public function isMethodNthArgumentNull(Node $methodCallNode, int $position): bool
|
||||
public function isMethodNthArgumentNull(Node $node, int $position): bool
|
||||
{
|
||||
if (! $this->hasMethodNthArgument($methodCallNode, $position)) {
|
||||
if (! $this->hasMethodNthArgument($node, $position)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var MethodCall $methodCallNode */
|
||||
$methodCallNode = $node;
|
||||
|
||||
$value = $methodCallNode->args[$position - 1]->value;
|
||||
if (! $value instanceof ConstFetch) {
|
||||
return false;
|
||||
|
@ -22,7 +22,7 @@ final class ReplaceVariableByPropertyFetchRector extends AbstractRector
|
||||
private $variablesToPropertyFetchCollection;
|
||||
|
||||
/**
|
||||
* @var VariableInfo|null
|
||||
* @var VariableInfo
|
||||
*/
|
||||
private $activeVariableInfo;
|
||||
|
||||
@ -52,7 +52,7 @@ final class SomeController
|
||||
* @var ProductRepository
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
|
||||
public function __construct(ProductRepository $productRepository)
|
||||
{
|
||||
$this->productRepository = $productRepository;
|
||||
@ -61,7 +61,7 @@ final class SomeController
|
||||
public function default()
|
||||
{
|
||||
$products = $productRepository->fetchAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
@ -72,7 +72,7 @@ final class SomeController
|
||||
* @var ProductRepository
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
|
||||
public function __construct(ProductRepository $productRepository)
|
||||
{
|
||||
$this->productRepository = $productRepository;
|
||||
@ -81,7 +81,7 @@ final class SomeController
|
||||
public function default()
|
||||
{
|
||||
$products = $this->productRepository->fetchAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
@ -91,8 +91,6 @@ CODE_SAMPLE
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
$this->activeVariableInfo = null;
|
||||
|
||||
if (! $node instanceof Variable) {
|
||||
return false;
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ final class ReplaceParentRepositoryCallsByRepositoryPropertyRector extends Abstr
|
||||
<?php
|
||||
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
|
||||
|
||||
class SomeRepository extends EntityRepository
|
||||
{
|
||||
public function someMethod()
|
||||
@ -78,7 +78,7 @@ SAMPLE
|
||||
<?php
|
||||
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
|
||||
|
||||
class SomeRepository extends EntityRepository
|
||||
{
|
||||
public function someMethod()
|
||||
|
@ -115,14 +115,16 @@ CODE_SAMPLE
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var MethodCall $node */
|
||||
if (count($node->args) !== 1) {
|
||||
/** @var MethodCall $methodCallNode */
|
||||
$methodCallNode = $node;
|
||||
|
||||
if (count($methodCallNode->args) !== 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($node->args[0]->value instanceof String_) {
|
||||
if ($methodCallNode->args[0]->value instanceof String_) {
|
||||
/** @var String_ $string */
|
||||
$string = $node->args[0]->value;
|
||||
$string = $methodCallNode->args[0]->value;
|
||||
|
||||
// is alias
|
||||
if (Strings::contains($string->value, ':')) {
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace Rector\Rector\Contrib\Doctrine;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use Rector\Node\NodeFactory;
|
||||
use Rector\NodeAnalyzer\MethodArgumentAnalyzer;
|
||||
use Rector\NodeAnalyzer\MethodCallAnalyzer;
|
||||
@ -64,16 +65,22 @@ final class AliasToClassRector extends AbstractRector
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->isAliasWithConfiguredEntity($node->args[0]->value->value);
|
||||
/** @var MethodCall $methodCall */
|
||||
$methodCall = $node;
|
||||
|
||||
return $this->isAliasWithConfiguredEntity($methodCall->args[0]->value->value);
|
||||
}
|
||||
|
||||
public function refactor(Node $node): ?Node
|
||||
/**
|
||||
* @param MethodCall $methodCall
|
||||
*/
|
||||
public function refactor(Node $methodCall): ?Node
|
||||
{
|
||||
$node->args[0]->value = $this->nodeFactory->createClassConstantReference(
|
||||
$this->convertAliasToFqn($node->args[0]->value->value)
|
||||
$methodCall->args[0]->value = $this->nodeFactory->createClassConstantReference(
|
||||
$this->convertAliasToFqn($methodCall->args[0]->value->value)
|
||||
);
|
||||
|
||||
return $node;
|
||||
return $methodCall;
|
||||
}
|
||||
|
||||
private function isAlias(string $name): bool
|
||||
|
@ -96,7 +96,7 @@ final class AssertCompareToSpecificMethodRector extends AbstractPHPUnitRector
|
||||
return false;
|
||||
}
|
||||
|
||||
$methodName = $secondArgumentValue->name->toString();
|
||||
$methodName = (string) $secondArgumentValue->name;
|
||||
$this->activeFuncCallName = $methodName;
|
||||
|
||||
return isset($this->defaultOldToNewMethods[$methodName]);
|
||||
|
@ -111,7 +111,7 @@ final class AssertIssetToSpecificMethodRector extends AbstractPHPUnitRector
|
||||
unset($oldArgs[0]);
|
||||
|
||||
$node->args = array_merge($this->nodeFactory->createArgs([
|
||||
$this->nodeFactory->createString($propertyFetchNode->name->toString()),
|
||||
$this->nodeFactory->createString((string) $propertyFetchNode->name),
|
||||
$propertyFetchNode->var,
|
||||
]), $oldArgs);
|
||||
}
|
||||
|
@ -95,7 +95,7 @@ final class AssertPropertyExistsRector extends AbstractPHPUnitRector
|
||||
return false;
|
||||
}
|
||||
|
||||
$methodName = $firstArgumentValue->name->toString();
|
||||
$methodName = (string) $firstArgumentValue->name;
|
||||
|
||||
return $methodName === 'property_exists';
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ final class AssertRegExpRector extends AbstractPHPUnitRector
|
||||
return false;
|
||||
}
|
||||
|
||||
$methodName = $secondArgumentValue->name->toString();
|
||||
$methodName = (string) $secondArgumentValue->name;
|
||||
|
||||
return $methodName === 'preg_match';
|
||||
}
|
||||
|
@ -105,7 +105,7 @@ final class AssertTrueFalseInternalTypeToSpecificMethodRector extends AbstractPH
|
||||
return false;
|
||||
}
|
||||
|
||||
$methodName = $firstArgumentValue->name->toString();
|
||||
$methodName = (string) $firstArgumentValue->name;
|
||||
|
||||
return isset($this->oldMethodsToTypes[$methodName]);
|
||||
}
|
||||
@ -126,7 +126,7 @@ final class AssertTrueFalseInternalTypeToSpecificMethodRector extends AbstractPH
|
||||
/** @var FuncCall $isFunctionNode */
|
||||
$isFunctionNode = $methodCallNode->args[0]->value;
|
||||
$argument = $isFunctionNode->args[0]->value;
|
||||
$isFunctionName = $isFunctionNode->name->toString();
|
||||
$isFunctionName = (string) $isFunctionNode->name;
|
||||
|
||||
$oldArguments = $methodCallNode->args;
|
||||
unset($oldArguments[0]);
|
||||
|
@ -154,7 +154,7 @@ final class AssertTrueFalseToSpecificMethodRector extends AbstractPHPUnitRector
|
||||
{
|
||||
$funcCallOrEmptyNode = $methodCallNode->args[0]->value;
|
||||
if ($funcCallOrEmptyNode instanceof FuncCall) {
|
||||
$funcCallOrEmptyNodeName = $funcCallOrEmptyNode->name->toString();
|
||||
$funcCallOrEmptyNodeName = (string) $funcCallOrEmptyNode->name;
|
||||
$funcCallOrEmptyNodeArgs = $funcCallOrEmptyNode->args;
|
||||
$oldArguments = $methodCallNode->args;
|
||||
unset($oldArguments[0]);
|
||||
|
@ -58,17 +58,18 @@ final class OptionNameRector extends AbstractRector
|
||||
}
|
||||
|
||||
$arrayParentNode = $arrayItemParentNode->getAttribute(Attribute::PARENT_NODE);
|
||||
$argParentNode = $arrayParentNode->getAttribute(Attribute::PARENT_NODE);
|
||||
|
||||
/** @var MethodCall $argParentNode */
|
||||
$methodCallNode = $argParentNode->getAttribute(Attribute::PARENT_NODE);
|
||||
$argParentNode = $arrayParentNode->getAttribute(Attribute::PARENT_NODE);
|
||||
|
||||
/** @var MethodCall|Node $methodCallNode */
|
||||
$methodCallNode = $argParentNode->getAttribute(Attribute::PARENT_NODE);
|
||||
|
||||
if (! $methodCallNode instanceof MethodCall) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $methodCallNode->name->toString() === 'add';
|
||||
return (string) $methodCallNode->name === 'add';
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -52,13 +52,13 @@ final class FluentReplaceRector extends AbstractRector
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function otherFunction()
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$someClass = new SomeClass();
|
||||
$someClass->someFunction()
|
||||
->otherFunction();
|
||||
@ -70,12 +70,12 @@ CODE_SAMPLE
|
||||
public function someFunction()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function otherFunction()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$someClass = new SomeClass();
|
||||
$someClass->someFunction();
|
||||
$someClass->otherFunction();
|
||||
@ -152,7 +152,7 @@ CODE_SAMPLE
|
||||
{
|
||||
$nextMethodCallNode = $this->methodCallNodeFactory->createWithVariableAndMethodName(
|
||||
$innerMethodCallNode->var,
|
||||
$outerMethodCallNode->name->toString()
|
||||
(string) $outerMethodCallNode->name
|
||||
);
|
||||
|
||||
$this->addNodeAfterNode($nextMethodCallNode, $innerMethodCallNode);
|
||||
|
Loading…
x
Reference in New Issue
Block a user