Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ClassSyncerNodeTraverser #3140

Merged
merged 5 commits into from
Apr 4, 2020
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 @@ -211,7 +211,6 @@ public function __construct(\Rector\DoctrineAnnotationGenerated\ConstantPreservi
if (extension_loaded('Zend OPcache') && ini_get('opcache.save_comments') == 0) {
throw \Doctrine\Common\Annotations\AnnotationException::optimizerPlusSaveComments();
}
\Doctrine\Common\Annotations\AnnotationRegistry::registerFile(__DIR__ . '/../../../vendor/doctrine/annotations/lib/Doctrine/Common/Annotations' . '/Annotation/IgnoreAnnotation.php');
$this->parser = $parser ?: new \Doctrine\Common\Annotations\DocParser();
$this->preParser = new \Rector\DoctrineAnnotationGenerated\ConstantPreservingDocParser();
$this->preParser->setImports(self::$globalImports);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,6 @@ private function collectAnnotationMetadata($name)
self::$metadataParser->setIgnoreNotImportedAnnotations(true);
self::$metadataParser->setIgnoredAnnotationNames($this->ignoredAnnotationNames);
self::$metadataParser->setImports(['enum' => 'Doctrine\Common\Annotations\Annotation\Enum', 'target' => 'Doctrine\Common\Annotations\Annotation\Target', 'attribute' => 'Doctrine\Common\Annotations\Annotation\Attribute', 'attributes' => 'Doctrine\Common\Annotations\Annotation\Attributes']);
\Doctrine\Common\Annotations\AnnotationRegistry::registerFile(__DIR__ . '/../../../vendor/doctrine/annotations/lib/Doctrine/Common/Annotations' . '/Annotation/Enum.php');
\Doctrine\Common\Annotations\AnnotationRegistry::registerFile(__DIR__ . '/../../../vendor/doctrine/annotations/lib/Doctrine/Common/Annotations' . '/Annotation/Target.php');
\Doctrine\Common\Annotations\AnnotationRegistry::registerFile(__DIR__ . '/../../../vendor/doctrine/annotations/lib/Doctrine/Common/Annotations' . '/Annotation/Attribute.php');
\Doctrine\Common\Annotations\AnnotationRegistry::registerFile(__DIR__ . '/../../../vendor/doctrine/annotations/lib/Doctrine/Common/Annotations' . '/Annotation/Attributes.php');
}
$class = new \ReflectionClass($name);
$docComment = $class->getDocComment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,9 @@ public function refactor(Node $node): ?Node
return null;
}

private function isAppUses($staticCall): bool
private function isAppUses(StaticCall $staticCall): bool
{
if (! $this->isName($staticCall->class, 'App')) {
return false;
}

return $this->isName($staticCall->name, 'uses');
return $this->isStaticCallNamed($staticCall, 'App', 'uses');
}

private function createFullyQualifiedNameFromAppUsesStaticCall(StaticCall $staticCall): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,7 @@ private function isStaticNamedConstructor(ClassMethod $classMethod): bool
return false;
}

if ($this->isName($node->expr->class, 'self')) {
return true;
}
return $this->isName($node->expr->class, 'static');
return $this->isNames($node->expr->class, ['self', 'static']);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveDeadRecursiveClassMethodRector\Fixture;

class StaticCall
class YesAnotherStaticCall
{
public static function run()
{
Expand All @@ -16,7 +16,7 @@ class StaticCall

namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveDeadRecursiveClassMethodRector\Fixture;

class StaticCall
class YesAnotherStaticCall
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -119,18 +118,10 @@ private function hasUuidInitAlreadyAdded(Class_ $class, string $uuidPropertyName
return false;
}

if (! $node->expr instanceof StaticCall) {
if (! $this->isStaticCallNamed($node->expr, 'Ramsey\Uuid\Uuid', 'uuid4')) {
return false;
}

$staticCall = $node->expr;
if (! $this->isObjectType($staticCall->class, 'Ramsey\Uuid\Uuid')) {
return false;
}

if (! $this->isName($staticCall->name, 'uuid4')) {
return false;
}
return $this->isName($node->var, $uuidPropertyName);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ public function refactor(Node $node): ?Node
return null;
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,15 +357,7 @@ private function getParentSetUpStaticCallPosition(ClassMethod $setupClassMethod)
$methodStmt = $methodStmt->expr;
}

if (! $methodStmt instanceof StaticCall) {
continue;
}

if (! $this->isName($methodStmt->class, 'parent')) {
continue;
}

if (! $this->isName($methodStmt->name, 'setUp')) {
if (! $this->isStaticCallNamed($methodStmt, 'parent', 'setUp')) {
continue;
}

Expand Down
34 changes: 30 additions & 4 deletions src/Rector/AbstractRector/NameResolverTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
Expand Down Expand Up @@ -79,6 +80,19 @@ protected function isFuncCallName(Node $node, string $name): bool
return $this->isName($node, $name);
}

protected function isStaticCallNamed(Node $node, string $className, string $methodName): bool
{
if (! $node instanceof StaticCall) {
return false;
}

if (! $this->isName($node->class, $className)) {
return false;
}

return $this->isName($node->name, $methodName);
}

protected function isMethodCall(Node $node, string $variableName, string $methodName): bool
{
if (! $node instanceof MethodCall) {
Expand All @@ -89,7 +103,7 @@ protected function isMethodCall(Node $node, string $variableName, string $method
return false;
}

return (bool) $this->isName($node->name, $methodName);
return $this->isName($node->name, $methodName);
}

protected function isVariableName(?Node $node, string $name): bool
Expand All @@ -101,11 +115,23 @@ protected function isVariableName(?Node $node, string $name): bool
return $this->isName($node, $name);
}

protected function isInClassNamed(Node $node, string $name): bool
protected function isInClassNamed(Node $node, string $desiredClassName): bool
{
$className = $node->getAttribute(AttributeKey::CLASS_NAME);
return $node->getAttribute(AttributeKey::CLASS_NAME) === $desiredClassName;
}

/**
* @param string[] $desiredClassNames
*/
protected function isInClassesNamed(Node $node, array $desiredClassNames): bool
{
foreach ($desiredClassNames as $desiredClassName) {
if ($this->isInClassNamed($node, $desiredClassName)) {
return true;
}
}

return $className === $name;
return false;
}

/**
Expand Down
14 changes: 1 addition & 13 deletions src/Rector/ClassMethod/AddMethodParentCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,7 @@ private function hasParentCallOfMethod(ClassMethod $classMethod, string $method)
return (bool) $this->betterNodeFinder->findFirst((array) $classMethod->stmts, function (Node $node) use (
$method
): bool {
if (! $node instanceof StaticCall) {
return false;
}

if (! $node->class instanceof Name) {
return false;
}

if (! $this->isName($node->class, 'parent')) {
return false;
}

return $this->isName($node->name, $method);
return $this->isStaticCallNamed($node, 'parent', $method);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Rector\Utils\DoctrineAnnotationParserSyncer;

use PhpParser\Node;
use PhpParser\NodeTraverser;
use Rector\PostRector\Application\PostFileProcessor;
use Rector\Utils\DoctrineAnnotationParserSyncer\Contract\Rector\ClassSyncerRectorInterface;

final class ClassSyncerNodeTraverser extends NodeTraverser
{
/**
* @var PostFileProcessor
*/
private $postFileProcessor;

/**
* @param ClassSyncerRectorInterface[] $classSyncerRectors
*/
public function __construct(array $classSyncerRectors, PostFileProcessor $postFileProcessor)
{
foreach ($classSyncerRectors as $classSyncerRector) {
$this->addVisitor($classSyncerRector);
}

$this->postFileProcessor = $postFileProcessor;
}

/**
* @param Node[] $nodes
* @return Node[]
*/
public function traverse(array $nodes): array
{
$traversedNodes = parent::traverse($nodes);
return $this->postFileProcessor->traverse($traversedNodes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Rector\Utils\DoctrineAnnotationParserSyncer\Contract\Rector;

interface ClassSyncerRectorInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected function hasContentChanged(array $nodes): bool
{
$finalContent = $this->printNodesToString($nodes);

// nothing to validate agains
// nothing to validate against
if (! file_exists($this->getTargetFilePath())) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,24 @@

namespace Rector\Utils\DoctrineAnnotationParserSyncer\FileSyncer;

use PhpParser\NodeTraverser;
use Rector\Utils\DoctrineAnnotationParserSyncer\Rector\Assign\AssignNewDocParserRector;
use Rector\Utils\DoctrineAnnotationParserSyncer\Rector\ClassMethod\ChangeOriginalTypeToCustomRector;
use Rector\Utils\DoctrineAnnotationParserSyncer\Rector\Dir\ReplaceDirWithRealPathRector;
use Rector\Utils\DoctrineAnnotationParserSyncer\Rector\Namespace_\RenameAnnotationReaderClassRector;
use Rector\Utils\DoctrineAnnotationParserSyncer\ClassSyncerNodeTraverser;

final class AnnotationReaderClassSyncer extends AbstractClassSyncer
{
/**
* @var NodeTraverser
* @var ClassSyncerNodeTraverser
*/
private $nodeTraverser;

public function __construct(
RenameAnnotationReaderClassRector $renameAnnotationReaderClassRector,
ChangeOriginalTypeToCustomRector $changeOriginalTypeToCustomRector,
ReplaceDirWithRealPathRector $replaceDirWithRealPathRector,
AssignNewDocParserRector $assignNewDocParserRector
) {
$this->nodeTraverser = new NodeTraverser();
$this->nodeTraverser->addVisitor($renameAnnotationReaderClassRector);
$this->nodeTraverser->addVisitor($changeOriginalTypeToCustomRector);
$this->nodeTraverser->addVisitor($replaceDirWithRealPathRector);
$this->nodeTraverser->addVisitor($assignNewDocParserRector);
private $classSyncerNodeTraverser;

public function __construct(ClassSyncerNodeTraverser $classSyncerNodeTraverser)
{
$this->classSyncerNodeTraverser = $classSyncerNodeTraverser;
}

public function sync(bool $isDryRun): bool
{
$nodes = $this->getFileNodes();
$changedNodes = $this->nodeTraverser->traverse($nodes);
$changedNodes = $this->classSyncerNodeTraverser->traverse($nodes);

if ($isDryRun) {
return ! $this->hasContentChanged($nodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,24 @@

namespace Rector\Utils\DoctrineAnnotationParserSyncer\FileSyncer;

use PhpParser\NodeTraverser;
use Rector\Utils\DoctrineAnnotationParserSyncer\Rector\ClassMethod\RemoveValueChangeFromConstantClassMethodRector;
use Rector\Utils\DoctrineAnnotationParserSyncer\Rector\Dir\ReplaceDirWithRealPathRector;
use Rector\Utils\DoctrineAnnotationParserSyncer\Rector\Namespace_\RenameDocParserClassRector;
use Rector\Utils\DoctrineAnnotationParserSyncer\ClassSyncerNodeTraverser;

final class DocParserClassSyncer extends AbstractClassSyncer
{
/**
* @var NodeTraverser
* @var ClassSyncerNodeTraverser
*/
private $nodeTraverser;

public function __construct(
RenameDocParserClassRector $renameDocParserClassRector,
RemoveValueChangeFromConstantClassMethodRector $removeValueChangeFromConstantClassMethodRector,
ReplaceDirWithRealPathRector $replaceDirWithRealPathRector
) {
$this->nodeTraverser = new NodeTraverser();
$this->nodeTraverser->addVisitor($renameDocParserClassRector);
$this->nodeTraverser->addVisitor($removeValueChangeFromConstantClassMethodRector);
$this->nodeTraverser->addVisitor($replaceDirWithRealPathRector);
private $classSyncerNodeTraverser;

public function __construct(ClassSyncerNodeTraverser $classSyncerNodeTraverser)
{
$this->classSyncerNodeTraverser = $classSyncerNodeTraverser;
}

public function sync(bool $isDryRun): bool
{
$nodes = $this->getFileNodes();
$changedNodes = $this->nodeTraverser->traverse($nodes);
$changedNodes = $this->classSyncerNodeTraverser->traverse($nodes);

if ($isDryRun) {
return ! $this->hasContentChanged($nodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\DoctrineAnnotationGenerated\ConstantPreservingDocParser;
use Rector\Utils\DoctrineAnnotationParserSyncer\Contract\Rector\ClassSyncerRectorInterface;

final class AssignNewDocParserRector extends AbstractRector
final class AssignNewDocParserRector extends AbstractRector implements ClassSyncerRectorInterface
{
/**
* @return class-string[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\DoctrineAnnotationGenerated\ConstantPreservingDocParser;
use Rector\Utils\DoctrineAnnotationParserSyncer\Contract\Rector\ClassSyncerRectorInterface;

final class ChangeOriginalTypeToCustomRector extends AbstractRector
final class ChangeOriginalTypeToCustomRector extends AbstractRector implements ClassSyncerRectorInterface
{
/**
* @return class-string[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\Utils\DoctrineAnnotationParserSyncer\Contract\Rector\ClassSyncerRectorInterface;

final class RemoveValueChangeFromConstantClassMethodRector extends AbstractRector
final class RemoveValueChangeFromConstantClassMethodRector extends AbstractRector implements ClassSyncerRectorInterface
{
/**
* @return class-string[]
Expand Down
Loading