remove FirstLevelNodeVisitor

This commit is contained in:
TomasVotruba 2021-03-18 02:23:25 +01:00
parent 00014cbb39
commit 08f9c545a1
6 changed files with 22 additions and 106 deletions

View File

@ -43,7 +43,6 @@ expectedArguments(
\Rector\NodeTypeResolver\Node\AttributeKey::VIRTUAL_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::PARAMETER_POSITION,
\Rector\NodeTypeResolver\Node\AttributeKey::ARGUMENT_POSITION,
\Rector\NodeTypeResolver\Node\AttributeKey::IS_FIRST_LEVEL_STATEMENT,
\Rector\NodeTypeResolver\Node\AttributeKey::FILE_INFO,
);
@ -73,7 +72,6 @@ expectedArguments(
\Rector\NodeTypeResolver\Node\AttributeKey::VIRTUAL_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::PARAMETER_POSITION,
\Rector\NodeTypeResolver\Node\AttributeKey::ARGUMENT_POSITION,
\Rector\NodeTypeResolver\Node\AttributeKey::IS_FIRST_LEVEL_STATEMENT,
\Rector\NodeTypeResolver\Node\AttributeKey::FILE_INFO,
);

View File

@ -1,49 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Caching\Tests\Config;
use Iterator;
use Rector\Caching\Config\FileHashComputer;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\HttpKernel\RectorKernel;
use Symplify\PackageBuilder\Testing\AbstractKernelTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class FileHashComputerTest extends AbstractKernelTestCase
{
/**
* @var FileHashComputer
*/
private $fileHashComputer;
protected function setUp(): void
{
$this->bootKernel(RectorKernel::class);
$this->fileHashComputer = $this->getService(FileHashComputer::class);
}
/**
* @dataProvider provideDataForIdenticalHash()
*/
public function testHashIsIdentical(string $firstConfig, string $secondConfig): void
{
$configAHash = $this->fileHashComputer->compute(new SmartFileInfo($firstConfig));
$configBHash = $this->fileHashComputer->compute(new SmartFileInfo($secondConfig));
$this->assertSame($configAHash, $configBHash);
}
public function provideDataForIdenticalHash(): Iterator
{
yield [__DIR__ . '/Source/config_content_a.yaml', __DIR__ . '/Source/config_content_b.yaml'];
yield [__DIR__ . '/Source/Import/import_a.yaml', __DIR__ . '/Source/Import/import_b.yaml'];
}
public function testInvalidType(): void
{
$this->expectException(ShouldNotHappenException::class);
$this->fileHashComputer->compute(new SmartFileInfo(__DIR__ . '/Source/file.xml'));
}
}

View File

@ -174,11 +174,6 @@ final class AttributeKey
*/
public const ARGUMENT_POSITION = 'argument_position';
/**
* @var string
*/
public const IS_FIRST_LEVEL_STATEMENT = 'is_first_level_statement';
/**
* @var string
*/

View File

@ -12,7 +12,6 @@ use PhpParser\NodeVisitor\NameResolver;
use PhpParser\NodeVisitor\NodeConnectingVisitor;
use Rector\NodeCollector\NodeVisitor\NodeCollectorNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\FileInfoNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\FirstLevelNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\FunctionLikeParamArgPositionNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\FunctionMethodAndClassNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\NamespaceNodeVisitor;
@ -67,11 +66,6 @@ final class NodeScopeAndMetadataDecorator
*/
private $functionLikeParamArgPositionNodeVisitor;
/**
* @var FirstLevelNodeVisitor
*/
private $firstLevelNodeVisitor;
public function __construct(
CloningVisitor $cloningVisitor,
FileInfoNodeVisitor $fileInfoNodeVisitor,
@ -81,8 +75,7 @@ final class NodeScopeAndMetadataDecorator
PHPStanNodeScopeResolver $phpStanNodeScopeResolver,
StatementNodeVisitor $statementNodeVisitor,
NodeConnectingVisitor $nodeConnectingVisitor,
FunctionLikeParamArgPositionNodeVisitor $functionLikeParamArgPositionNodeVisitor,
FirstLevelNodeVisitor $firstLevelNodeVisitor
FunctionLikeParamArgPositionNodeVisitor $functionLikeParamArgPositionNodeVisitor
) {
$this->phpStanNodeScopeResolver = $phpStanNodeScopeResolver;
$this->cloningVisitor = $cloningVisitor;
@ -93,7 +86,6 @@ final class NodeScopeAndMetadataDecorator
$this->nodeCollectorNodeVisitor = $nodeCollectorNodeVisitor;
$this->nodeConnectingVisitor = $nodeConnectingVisitor;
$this->functionLikeParamArgPositionNodeVisitor = $functionLikeParamArgPositionNodeVisitor;
$this->firstLevelNodeVisitor = $firstLevelNodeVisitor;
}
/**
@ -129,7 +121,6 @@ final class NodeScopeAndMetadataDecorator
$nodeTraverser->addVisitor($this->nodeConnectingVisitor);
$nodeTraverser->addVisitor($this->functionMethodAndClassNodeVisitor);
$nodeTraverser->addVisitor($this->namespaceNodeVisitor);
$nodeTraverser->addVisitor($this->firstLevelNodeVisitor);
$nodeTraverser->addVisitor($this->functionLikeParamArgPositionNodeVisitor);
$nodes = $nodeTraverser->traverse($nodes);

View File

@ -1,32 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\NodeTypeResolver\NodeVisitor;
use PhpParser\Node;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\Expression;
use PhpParser\NodeVisitorAbstract;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class FirstLevelNodeVisitor extends NodeVisitorAbstract
{
/**
* @return Node
*/
public function enterNode(Node $node): ?Node
{
if ($node instanceof FunctionLike) {
foreach ((array) $node->getStmts() as $stmt) {
$stmt->setAttribute(AttributeKey::IS_FIRST_LEVEL_STATEMENT, true);
if ($stmt instanceof Expression) {
$stmt->expr->setAttribute(AttributeKey::IS_FIRST_LEVEL_STATEMENT, true);
}
}
}
return null;
}
}

View File

@ -8,6 +8,8 @@ use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\NodeTraverser;
use Rector\Core\ValueObject\MethodName;
use Rector\NodeTypeResolver\Node\AttributeKey;
@ -16,6 +18,11 @@ use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
final class ConstructorAssignDetector
{
/**
* @var string
*/
private const IS_FIRST_LEVEL_STATEMENT = 'first_level_stmt';
/**
* @var PropertyAssignMatcher
*/
@ -36,9 +43,21 @@ final class ConstructorAssignDetector
public function isPropertyAssigned(ClassLike $classLike, string $propertyName): bool
{
$constructClassMethod = $classLike->getMethod(MethodName::CONSTRUCT);
if (! $constructClassMethod instanceof ClassMethod) {
return false;
}
$isAssignedInConstructor = false;
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($classLike->stmts, function (Node $node) use (
foreach ($constructClassMethod->stmts as $methodStmt) {
$methodStmt->setAttribute(self::IS_FIRST_LEVEL_STATEMENT, true);
if ($methodStmt instanceof Expression) {
$methodStmt->expr->setAttribute(self::IS_FIRST_LEVEL_STATEMENT, true);
}
}
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($constructClassMethod->stmts, function (Node $node) use (
$propertyName, &$isAssignedInConstructor
): ?int {
$expr = $this->matchAssignExprToPropertyName($node, $propertyName);
@ -46,15 +65,9 @@ final class ConstructorAssignDetector
return null;
}
// is in constructor?
$methodName = $node->getAttribute(AttributeKey::METHOD_NAME);
if ($methodName !== MethodName::CONSTRUCT) {
return null;
}
/** @var Assign $assign */
$assign = $node;
$isFirstLevelStatement = $assign->getAttribute(AttributeKey::IS_FIRST_LEVEL_STATEMENT);
$isFirstLevelStatement = $assign->getAttribute(self::IS_FIRST_LEVEL_STATEMENT);
// cannot be nested
if ($isFirstLevelStatement !== true) {