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
*/
private function findPhpFilesInDirectories(array $directories): array
private function findPhpFilesInDirectories(string ...$directories): array
{
$finder = Finder::find('*.php')
->in($directories);

View File

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

View File

@ -40,15 +40,13 @@ final class FileReconstructor
}
# 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());
/** @var Node[] $nodes */
$oldStmts = $this->parser->parse($fileContent);
// before recontruct event?
// keep format printer
$oldTokens = $this->lexer->getTokens();

View File

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

View File

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