make test pass

This commit is contained in:
TomasVotruba 2017-07-20 17:23:10 +02:00
parent 82e8858158
commit 01a5fd8b41
6 changed files with 44 additions and 38 deletions

View File

@ -2,7 +2,12 @@
namespace Rector\NodeTraverser; namespace Rector\NodeTraverser;
final class PriorityAwareNodeTraverser use PhpParser\NodeTraverser;
/**
* Allow to add priorites to node vistiors
*/
final class PriorityAwareNodeTraverser extends NodeTraverser
{ {
// ... // might be needed, so ready here
} }

View File

@ -60,16 +60,6 @@ final class GetterToPropertyNodeVisitor extends NodeVisitorAbstract
return null; return null;
} }
/**
* @param Assign|MethodCall $assignOrMethodCallNode
*/
public function reconstruct(Node $assignOrMethodCallNode): void
{
if ($assignOrMethodCallNode instanceof Assign) {
$this->processAssignment($assignOrMethodCallNode);
}
}
private function isCandidate(Node $node): bool private function isCandidate(Node $node): bool
{ {
// $var = $this->get('some_service'); // $var = $this->get('some_service');
@ -92,6 +82,20 @@ final class GetterToPropertyNodeVisitor extends NodeVisitorAbstract
return false; return false;
} }
/**
* @param Assign|MethodCall $assignOrMethodCallNode
*/
private function reconstruct(Node $assignOrMethodCallNode): void
{
if ($assignOrMethodCallNode instanceof Assign) {
$this->processAssignment($assignOrMethodCallNode);
}
if ($assignOrMethodCallNode instanceof MethodCall) {
$this->processMethodCall($assignOrMethodCallNode);
}
}
private function processAssignment(Assign $assignNode): void private function processAssignment(Assign $assignNode): void
{ {
$refactoredMethodCall = $this->processMethodCallNode($assignNode->expr); $refactoredMethodCall = $this->processMethodCallNode($assignNode->expr);
@ -100,6 +104,14 @@ final class GetterToPropertyNodeVisitor extends NodeVisitorAbstract
} }
} }
private function processMethodCall(MethodCall $methodCallNode): void
{
$refactoredMethodCall = $this->processMethodCallNode($methodCallNode->var);
if ($refactoredMethodCall) {
$methodCallNode->var = $refactoredMethodCall;
}
}
/** /**
* Is "$this->get('string')" statements? * Is "$this->get('string')" statements?
*/ */

View File

@ -2,12 +2,10 @@
namespace Rector\NodeVisitor\DependencyInjection\NamedServicesToConstructor; namespace Rector\NodeVisitor\DependencyInjection\NamedServicesToConstructor;
use PhpParser\Node; use PhpParser\Node;
use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_; use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\ClassMethod;
@ -18,6 +16,12 @@ use Rector\Builder\Naming\NameResolver;
use Rector\Builder\PropertyBuilder; use Rector\Builder\PropertyBuilder;
use Rector\Tests\NodeVisitor\DependencyInjection\NamedServicesToConstructorReconstructor\Source\LocalKernel; use Rector\Tests\NodeVisitor\DependencyInjection\NamedServicesToConstructorReconstructor\Source\LocalKernel;
/**
* Add property to class...
* Add property to constructor...
*
* How to dettect that particular class?
*/
final class NamedServicesToConstructorNodeVisitor extends NodeVisitorAbstract final class NamedServicesToConstructorNodeVisitor extends NodeVisitorAbstract
{ {
/** /**
@ -161,11 +165,13 @@ final class NamedServicesToConstructorNodeVisitor extends NodeVisitorAbstract
$this->propertyBuilder->addPropertyToClass($classNode, $serviceType, $propertyName); $this->propertyBuilder->addPropertyToClass($classNode, $serviceType, $propertyName);
// creates "$this->propertyName" // creates "$this->propertyName"
return new PropertyFetch( // return new PropertyFetch(
new Variable('this', [ // new Variable('this', [
'name' => $propertyName // 'name' => $propertyName
]), $propertyName // ]), $propertyName
); // );
return null;
} }

View File

@ -5,7 +5,6 @@ namespace Rector\Testing\Application;
use PhpParser\Lexer; use PhpParser\Lexer;
use PhpParser\Node; use PhpParser\Node;
use PhpParser\NodeTraverser; use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use PhpParser\Parser; use PhpParser\Parser;
use Rector\Printer\CodeStyledPrinter; use Rector\Printer\CodeStyledPrinter;
use SplFileInfo; use SplFileInfo;
@ -40,7 +39,7 @@ 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 processFileWithNodeVisitor(SplFileInfo $file, NodeVisitor $nodeVisitor): string public function processFile(SplFileInfo $file): string
{ {
$fileContent = file_get_contents($file->getRealPath()); $fileContent = file_get_contents($file->getRealPath());
@ -50,7 +49,6 @@ final class FileReconstructor
// keep format printer // keep format printer
$oldTokens = $this->lexer->getTokens(); $oldTokens = $this->lexer->getTokens();
$this->nodeTraverser->addVisitor($nodeVisitor);
$newStmts = $this->nodeTraverser->traverse($oldStmts); $newStmts = $this->nodeTraverser->traverse($oldStmts);
return $this->codeStyledPrinter->printToString($oldStmts, $newStmts, $oldTokens); return $this->codeStyledPrinter->printToString($oldStmts, $newStmts, $oldTokens);

View File

@ -29,17 +29,8 @@ abstract class AbstractReconstructorTestCase extends TestCase
protected function doTestFileMatchesExpectedContent(string $file, string $reconstructedFile): void protected function doTestFileMatchesExpectedContent(string $file, string $reconstructedFile): void
{ {
$reconstructedFileContent = $this->fileReconstructor->processFileWithNodeVisitor( $reconstructedFileContent = $this->fileReconstructor->processFile(new SplFileInfo($file));
new SplFileInfo($file), $this->getNodeVisitor()
);
$this->assertStringEqualsFile($reconstructedFile, $reconstructedFileContent); $this->assertStringEqualsFile($reconstructedFile, $reconstructedFileContent);
} }
abstract protected function getNodeVisitorClass(): string;
private function getNodeVisitor(): NodeVisitor
{
return $this->container->get($this->getNodeVisitorClass());
}
} }

View File

@ -2,7 +2,6 @@
namespace Rector\Tests\NodeVisitor\DependencyInjection\InjectAnnotationToConstructorReconstructor; namespace Rector\Tests\NodeVisitor\DependencyInjection\InjectAnnotationToConstructorReconstructor;
use Rector\NodeVisitor\DependencyInjection\InjectAnnotationToConstructorNodeVisitor;
use Rector\Testing\PHPUnit\AbstractReconstructorTestCase; use Rector\Testing\PHPUnit\AbstractReconstructorTestCase;
final class Test extends AbstractReconstructorTestCase final class Test extends AbstractReconstructorTestCase
@ -14,10 +13,5 @@ final class Test extends AbstractReconstructorTestCase
__DIR__ . '/correct/correct.php.inc' __DIR__ . '/correct/correct.php.inc'
); );
} }
protected function getNodeVisitorClass(): string
{
return InjectAnnotationToConstructorNodeVisitor::class;
}
} }