This commit is contained in:
TomasVotruba 2017-07-19 00:42:50 +02:00
parent ebe7698bae
commit 8f87dd58d6
8 changed files with 12 additions and 73 deletions

View File

@ -1,39 +0,0 @@
<?php declare(strict_types=1);
namespace Rector\Analyzer;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
final class ClassAnalyzer
{
public function isControllerClassNode(Node $node): bool
{
if (! $node instanceof Class_) {
return false;
}
if ($node->extends instanceof Name) {
return Strings::endsWith($node->extends->getLast(), 'Controller');
}
return false;
}
public function isContainerAwareClassNode(Node $node): bool
{
if (! $node instanceof Class_) {
return false;
}
foreach ($node->implements as $nameNode) {
if (Strings::endsWith($nameNode->getLast(), 'ContainerAwareInterface')) {
return true;
}
}
return false;
}
}

View File

@ -57,10 +57,9 @@ final class ReconstructCommand extends Command
} }
/** /**
* @param string[] $directories
* @return SplFileInfo[] array * @return SplFileInfo[] array
*/ */
private function findPhpFilesInDirectories(array $directories): array private function findPhpFilesInDirectories(string ...$directories): array
{ {
$finder = Finder::find('*.php') $finder = Finder::find('*.php')
->in($directories); ->in($directories);

View File

@ -99,6 +99,6 @@ final class InjectAnnotationToConstructorNodeVisitor extends NodeVisitorAbstract
return $node; return $node;
} }
return NodeTraverser::DONT_TRAVERSE_CHILDREN; return null;
} }
} }

View File

@ -13,7 +13,6 @@ use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeTraverser; use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract; use PhpParser\NodeVisitorAbstract;
use Rector\Analyzer\ClassAnalyzer;
use Rector\Builder\ConstructorMethodBuilder; use Rector\Builder\ConstructorMethodBuilder;
use Rector\Builder\Naming\NameResolver; use Rector\Builder\Naming\NameResolver;
use Rector\Builder\PropertyBuilder; use Rector\Builder\PropertyBuilder;
@ -38,36 +37,19 @@ final class NamedServicesToConstructorNodeVisitor extends NodeVisitorAbstract
*/ */
private $nameResolver; private $nameResolver;
/**
* @var ClassAnalyzer
*/
private $classAnalyzer;
public function __construct( public function __construct(
ConstructorMethodBuilder $constructorMethodBuilder, ConstructorMethodBuilder $constructorMethodBuilder,
PropertyBuilder $propertyBuilder, PropertyBuilder $propertyBuilder,
NameResolver $nameResolver, NameResolver $nameResolver
ClassAnalyzer $classAnalyzer
) { ) {
$this->constructorMethodBuilder = $constructorMethodBuilder; $this->constructorMethodBuilder = $constructorMethodBuilder;
$this->propertyBuilder = $propertyBuilder; $this->propertyBuilder = $propertyBuilder;
$this->nameResolver = $nameResolver; $this->nameResolver = $nameResolver;
$this->classAnalyzer = $classAnalyzer;
} }
private function isCandidate(Node $node): bool private function isCandidate(Node $node): bool
{ {
// OR? Maybe listen on MethodCall... $this-> +get('...') return $node instanceof Class_;
if ($this->classAnalyzer->isControllerClassNode($node)) {
return true;
}
if ($this->classAnalyzer->isContainerAwareClassNode($node)) {
return true;
}
return false;
} }
/** /**

View File

@ -40,15 +40,13 @@ final class FileReconstructor
} }
# ref: https://github.com/nikic/PHP-Parser/issues/344#issuecomment-298162516 # ref: https://github.com/nikic/PHP-Parser/issues/344#issuecomment-298162516
public function processFileWithReconstructor(SplFileInfo $file, NodeVisitor $nodeVisitor): string public function processFileWithNodeVisitor(SplFileInfo $file, NodeVisitor $nodeVisitor): string
{ {
$fileContent = file_get_contents($file->getRealPath()); $fileContent = file_get_contents($file->getRealPath());
/** @var Node[] $nodes */ /** @var Node[] $nodes */
$oldStmts = $this->parser->parse($fileContent); $oldStmts = $this->parser->parse($fileContent);
// before recontruct event?
// keep format printer // keep format printer
$oldTokens = $this->lexer->getTokens(); $oldTokens = $this->lexer->getTokens();

View File

@ -5,7 +5,6 @@ namespace Rector\Testing\PHPUnit;
use PhpParser\NodeVisitor; use PhpParser\NodeVisitor;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Rector\Contract\Dispatcher\ReconstructorInterface;
use Rector\DependencyInjection\ContainerFactory; use Rector\DependencyInjection\ContainerFactory;
use Rector\Testing\Application\FileReconstructor; use Rector\Testing\Application\FileReconstructor;
use SplFileInfo; use SplFileInfo;
@ -30,17 +29,17 @@ abstract class AbstractReconstructorTestCase extends TestCase
protected function doTestFileMatchesExpectedContent(string $file, string $reconstructedFile): void protected function doTestFileMatchesExpectedContent(string $file, string $reconstructedFile): void
{ {
$reconstructedFileContent = $this->fileReconstructor->processFileWithReconstructor( $reconstructedFileContent = $this->fileReconstructor->processFileWithNodeVisitor(
new SplFileInfo($file), $this->getReconstructor() new SplFileInfo($file), $this->getNodeVisitor()
); );
$this->assertStringEqualsFile($reconstructedFile, $reconstructedFileContent); $this->assertStringEqualsFile($reconstructedFile, $reconstructedFileContent);
} }
abstract protected function getReconstructorClass(): string; abstract protected function getNodeVisitorClass(): string;
private function getReconstructor(): NodeVisitor private function getNodeVisitor(): NodeVisitor
{ {
return $this->container->get($this->getReconstructorClass()); return $this->container->get($this->getNodeVisitorClass());
} }
} }

View File

@ -15,7 +15,7 @@ final class Test extends AbstractReconstructorTestCase
); );
} }
protected function getReconstructorClass(): string protected function getNodeVisitorClass(): string
{ {
return InjectAnnotationToConstructorNodeVisitor::class; return InjectAnnotationToConstructorNodeVisitor::class;
} }

View File

@ -15,7 +15,7 @@ final class Test extends AbstractReconstructorTestCase
// $this->doTestFileMatchesExpectedContent(__DIR__ . '/wrong/wrong3.php.inc', __DIR__ . '/correct/correct3.php.inc'); // $this->doTestFileMatchesExpectedContent(__DIR__ . '/wrong/wrong3.php.inc', __DIR__ . '/correct/correct3.php.inc');
} }
protected function getReconstructorClass(): string protected function getNodeVisitorClass(): string
{ {
return NamedServicesToConstructorNodeVisitor::class; return NamedServicesToConstructorNodeVisitor::class;
} }