This commit is contained in:
TomasVotruba 2017-07-20 19:10:06 +02:00
parent 09c13a11fa
commit 5120451ee5
4 changed files with 42 additions and 45 deletions

View File

@ -62,7 +62,6 @@ final class FileProcessor
$newStmts = $this->nodeTraverser->traverse($oldStmts);
$this->codeStyledPrinter->printToFile($file, $newStmts, $oldStmts, $oldTokens);
}

View File

@ -12,7 +12,11 @@ use Rector\Builder\ConstructorMethodBuilder;
final class InjectAnnotationToConstructorNodeVisitor extends NodeVisitorAbstract
{
const ANNOTATION_INJECT = 'inject';
/**
* @var string
*/
private const ANNOTATION_INJECT = 'inject';
/**
* @var ConstructorMethodBuilder
*/
@ -29,9 +33,31 @@ final class InjectAnnotationToConstructorNodeVisitor extends NodeVisitorAbstract
}
/**
* @param Class_ $classNode
* Called when entering a node.
*
* Return value semantics:
* * null
* => $node stays as-is
* * NodeTraverser::DONT_TRAVERSE_CHILDREN
* => Children of $node are not traversed. $node stays as-is
* * NodeTraverser::STOP_TRAVERSAL
* => Traversal is aborted. $node stays as-is
* * otherwise
* => $node is set to the return value
*
* @return null|int|Node Replacement node (or special return value)
*/
public function reconstruct(Node $classNode): void
public function enterNode(Node $node)
{
if ($node instanceof Class_) {
$this->reconstruct($node);
return $node;
}
return null;
}
private function reconstruct(Class_ $classNode): void
{
foreach ($classNode->stmts as $classElementStatement) {
if (! $classElementStatement instanceof Property) {
@ -76,29 +102,4 @@ final class InjectAnnotationToConstructorNodeVisitor extends NodeVisitorAbstract
$propertyNode->setDocComment(new Doc($propertyDocBlock->getContent()));
}
/**
* Called when entering a node.
*
* Return value semantics:
* * null
* => $node stays as-is
* * NodeTraverser::DONT_TRAVERSE_CHILDREN
* => Children of $node are not traversed. $node stays as-is
* * NodeTraverser::STOP_TRAVERSAL
* => Traversal is aborted. $node stays as-is
* * otherwise
* => $node is set to the return value
*
* @return null|int|Node Replacement node (or special return value)
*/
public function enterNode(Node $node)
{
if ($node instanceof Class_) {
$this->reconstruct($node);
return $node;
}
return null;
}
}

View File

@ -24,11 +24,6 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract
*/
private $propertyBuilder;
/**
* @var string
*/
private $className;
/**
* @var ClassPropertyCollector
*/
@ -52,17 +47,18 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract
{
foreach ($nodes as $node) {
if ($node instanceof Class_) {
$this->className = (string) $node->name;
$this->reconstruct($node);
$this->reconstruct($node, (string) $node->name);
break;
}
}
return $nodes;
}
private function reconstruct(Class_ $classNode): void
private function reconstruct(Class_ $classNode, string $className): void
{
$propertiesForClass = $this->newClassPropertyCollector->getPropertiesforClass($this->className);
$propertiesForClass = $this->newClassPropertyCollector->getPropertiesforClass($className);
foreach ($propertiesForClass as $propertyType => $propertyName) {
$this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $propertyType, $propertyName);
$this->propertyBuilder->addPropertyToClass($classNode, $propertyType, $propertyName);

View File

@ -20,18 +20,23 @@ final class FileReconstructor
* @var CodeStyledPrinter
*/
private $codeStyledPrinter;
/**
* @var Lexer
*/
private $lexer;
/**
* @var NodeTraverser
*/
private $nodeTraverser;
public function __construct(Parser $parser, CodeStyledPrinter $codeStyledPrinter, Lexer $lexer, NodeTraverser $nodeTraverser)
{
public function __construct(
Parser $parser,
CodeStyledPrinter $codeStyledPrinter,
Lexer $lexer,
NodeTraverser $nodeTraverser
) {
$this->parser = $parser;
$this->codeStyledPrinter = $codeStyledPrinter;
$this->lexer = $lexer;
@ -43,12 +48,8 @@ final class FileReconstructor
{
$fileContent = file_get_contents($file->getRealPath());
/** @var Node[] $nodes */
$oldStmts = $this->parser->parse($fileContent);
// keep format printer
$oldTokens = $this->lexer->getTokens();
$newStmts = $this->nodeTraverser->traverse($oldStmts);
return $this->codeStyledPrinter->printToString($newStmts, $oldStmts, $oldTokens);