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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwarePhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\FileSystemRector\Parser\FileInfoParser;
use Rector\NodeContainer\ParsedNodesByType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
use Rector\Rector\AbstractPHPUnitRector;
Expand All @@ -31,11 +30,6 @@
*/
final class AddDoesNotPerformAssertionToNonAssertingTestRector extends AbstractPHPUnitRector
{
/**
* @var ParsedNodesByType
*/
private $parsedNodesByType;

/**
* @var FileInfoParser
*/
Expand All @@ -58,12 +52,10 @@ final class AddDoesNotPerformAssertionToNonAssertingTestRector extends AbstractP

public function __construct(
DocBlockManipulator $docBlockManipulator,
ParsedNodesByType $parsedNodesByType,
FileInfoParser $fileInfoParser,
ClassMethodReflectionFactory $classMethodReflectionFactory
) {
$this->docBlockManipulator = $docBlockManipulator;
$this->parsedNodesByType = $parsedNodesByType;
$this->fileInfoParser = $fileInfoParser;
$this->classMethodReflectionFactory = $classMethodReflectionFactory;
}
Expand Down Expand Up @@ -234,12 +226,12 @@ private function hasNestedAssertCall(ClassMethod $classMethod): bool
private function findClassMethod(Node $node): ?ClassMethod
{
if ($node instanceof MethodCall) {
$classMethod = $this->parsedNodesByType->findClassMethodByMethodCall($node);
$classMethod = $this->functionLikeParsedNodesFinder->findClassMethodByMethodCall($node);
if ($classMethod !== null) {
return $classMethod;
}
} elseif ($node instanceof StaticCall) {
$classMethod = $this->parsedNodesByType->findClassMethodByStaticCall($node);
$classMethod = $this->functionLikeParsedNodesFinder->findClassMethodByStaticCall($node);
if ($classMethod !== null) {
return $classMethod;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use PhpParser\Node\Stmt\Return_;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocNode\Sensio\SensioTemplateTagValueNode;
use Rector\NodeContainer\ParsedNodesByType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
Expand All @@ -36,18 +35,9 @@ final class TemplateAnnotationRector extends AbstractRector
*/
private $templateGuesser;

/**
* @var ParsedNodesByType
*/
private $parsedNodesByType;

public function __construct(
TemplateGuesser $templateGuesser,
ParsedNodesByType $parsedNodesByType,
int $version = 3
) {
public function __construct(TemplateGuesser $templateGuesser, int $version = 3)
{
$this->templateGuesser = $templateGuesser;
$this->parsedNodesByType = $parsedNodesByType;
$this->version = $version;
}

Expand Down Expand Up @@ -242,7 +232,7 @@ private function refactorClassMethod(

if ($returnNode !== null && $returnNode->expr instanceof MethodCall) {
// go inside called method
$innerClassMethod = $this->parsedNodesByType->findClassMethodByMethodCall($returnNode->expr);
$innerClassMethod = $this->functionLikeParsedNodesFinder->findClassMethodByMethodCall($returnNode->expr);
if ($innerClassMethod !== null) {
$this->refactorClassMethod($innerClassMethod, $sensioTemplateTagValueNode);

Expand Down
77 changes: 77 additions & 0 deletions src/NodeContainer/FunctionLikeParsedNodesFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Rector\NodeContainer;

use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\TypeUtils;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpParser\Node\Resolver\NameResolver;

final class FunctionLikeParsedNodesFinder
{
/**
* @var ParsedNodesByType
*/
private $parsedNodesByType;

/**
* @var NameResolver
*/
private $nameResolver;

/**
* @var NodeTypeResolver
*/
private $nodeTypeResolver;

public function __construct(
ParsedNodesByType $parsedNodesByType,
NameResolver $nameResolver,
NodeTypeResolver $nodeTypeResolver
) {
$this->parsedNodesByType = $parsedNodesByType;
$this->nameResolver = $nameResolver;
$this->nodeTypeResolver = $nodeTypeResolver;
}

public function findClassMethodByMethodCall(MethodCall $methodCall): ?ClassMethod
{
/** @var string|null $className */
$className = $methodCall->getAttribute(AttributeKey::CLASS_NAME);
if ($className === null) {
return null;
}

$methodName = $this->nameResolver->getName($methodCall->name);
if ($methodName === null) {
return null;
}

return $this->parsedNodesByType->findMethod($methodName, $className);
}

public function findClassMethodByStaticCall(StaticCall $staticCall): ?ClassMethod
{
$methodName = $this->nameResolver->getName($staticCall->name);
if ($methodName === null) {
return null;
}

$objectType = $this->nodeTypeResolver->resolve($staticCall->class);

$classNames = TypeUtils::getDirectClassNames($objectType);
foreach ($classNames as $className) {
$foundMethod = $this->parsedNodesByType->findMethod($methodName, $className);
if ($foundMethod !== null) {
return $foundMethod;
}
}

return null;
}
}
37 changes: 0 additions & 37 deletions src/NodeContainer/ParsedNodesByType.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use Rector\Exception\NotImplementedException;
use Rector\Exception\ShouldNotHappenException;
Expand Down Expand Up @@ -195,42 +194,6 @@ public function findFunction(string $name): ?Function_
return $this->simpleParsedNodesByType[Function_::class][$name] ?? null;
}

public function findClassMethodByMethodCall(MethodCall $methodCall): ?ClassMethod
{
/** @var string|null $className */
$className = $methodCall->getAttribute(AttributeKey::CLASS_NAME);
if ($className === null) {
return null;
}

$methodName = $this->nameResolver->getName($methodCall->name);
if ($methodName === null) {
return null;
}

return $this->findMethod($methodName, $className);
}

public function findClassMethodByStaticCall(StaticCall $staticCall): ?ClassMethod
{
$methodName = $this->nameResolver->getName($staticCall->name);
if ($methodName === null) {
return null;
}

$objectType = $this->nodeTypeResolver->resolve($staticCall->class);

$classNames = TypeUtils::getDirectClassNames($objectType);
foreach ($classNames as $className) {
$foundMethod = $this->findMethod($methodName, $className);
if ($foundMethod !== null) {
return $foundMethod;
}
}

return null;
}

public function findMethod(string $methodName, string $className): ?ClassMethod
{
if (isset($this->methodsByType[$className][$methodName])) {
Expand Down
9 changes: 8 additions & 1 deletion src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Rector\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector;
use Rector\Exclusion\ExclusionManager;
use Rector\NodeContainer\ClassLikeParsedNodesFinder;
use Rector\NodeContainer\FunctionLikeParsedNodesFinder;
use Rector\NodeTypeResolver\FileSystem\CurrentFileInfoProvider;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
Expand Down Expand Up @@ -86,6 +87,10 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn
* @var ClassLikeParsedNodesFinder
*/
protected $classLikeParsedNodesFinder;
/**
* @var FunctionLikeParsedNodesFinder
*/
protected $functionLikeParsedNodesFinder;

/**
* Run once in the every end of one processed file
Expand All @@ -107,7 +112,8 @@ public function autowireAbstractRectorDependencies(
PhpDocInfoPrinter $phpDocInfoPrinter,
DocBlockManipulator $docBlockManipulator,
StaticTypeMapper $staticTypeMapper,
ClassLikeParsedNodesFinder $classLikeParsedNodesFinder
ClassLikeParsedNodesFinder $classLikeParsedNodesFinder,
FunctionLikeParsedNodesFinder $functionLikeParsedNodesFinder
): void {
$this->symfonyStyle = $symfonyStyle;
$this->phpVersionProvider = $phpVersionProvider;
Expand All @@ -119,6 +125,7 @@ public function autowireAbstractRectorDependencies(
$this->docBlockManipulator = $docBlockManipulator;
$this->staticTypeMapper = $staticTypeMapper;
$this->classLikeParsedNodesFinder = $classLikeParsedNodesFinder;
$this->functionLikeParsedNodesFinder = $functionLikeParsedNodesFinder;
}

/**
Expand Down