mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 00:40:52 +00:00
cleanup
This commit is contained in:
parent
09c13a11fa
commit
5120451ee5
|
@ -62,7 +62,6 @@ final class FileProcessor
|
||||||
|
|
||||||
$newStmts = $this->nodeTraverser->traverse($oldStmts);
|
$newStmts = $this->nodeTraverser->traverse($oldStmts);
|
||||||
|
|
||||||
|
|
||||||
$this->codeStyledPrinter->printToFile($file, $newStmts, $oldStmts, $oldTokens);
|
$this->codeStyledPrinter->printToFile($file, $newStmts, $oldStmts, $oldTokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,11 @@ use Rector\Builder\ConstructorMethodBuilder;
|
||||||
|
|
||||||
final class InjectAnnotationToConstructorNodeVisitor extends NodeVisitorAbstract
|
final class InjectAnnotationToConstructorNodeVisitor extends NodeVisitorAbstract
|
||||||
{
|
{
|
||||||
const ANNOTATION_INJECT = 'inject';
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private const ANNOTATION_INJECT = 'inject';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ConstructorMethodBuilder
|
* @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) {
|
foreach ($classNode->stmts as $classElementStatement) {
|
||||||
if (! $classElementStatement instanceof Property) {
|
if (! $classElementStatement instanceof Property) {
|
||||||
|
@ -76,29 +102,4 @@ final class InjectAnnotationToConstructorNodeVisitor extends NodeVisitorAbstract
|
||||||
|
|
||||||
$propertyNode->setDocComment(new Doc($propertyDocBlock->getContent()));
|
$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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,11 +24,6 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract
|
||||||
*/
|
*/
|
||||||
private $propertyBuilder;
|
private $propertyBuilder;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $className;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ClassPropertyCollector
|
* @var ClassPropertyCollector
|
||||||
*/
|
*/
|
||||||
|
@ -52,17 +47,18 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract
|
||||||
{
|
{
|
||||||
foreach ($nodes as $node) {
|
foreach ($nodes as $node) {
|
||||||
if ($node instanceof Class_) {
|
if ($node instanceof Class_) {
|
||||||
$this->className = (string) $node->name;
|
$this->reconstruct($node, (string) $node->name);
|
||||||
$this->reconstruct($node);
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $nodes;
|
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) {
|
foreach ($propertiesForClass as $propertyType => $propertyName) {
|
||||||
$this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $propertyType, $propertyName);
|
$this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $propertyType, $propertyName);
|
||||||
$this->propertyBuilder->addPropertyToClass($classNode, $propertyType, $propertyName);
|
$this->propertyBuilder->addPropertyToClass($classNode, $propertyType, $propertyName);
|
||||||
|
|
|
@ -20,18 +20,23 @@ final class FileReconstructor
|
||||||
* @var CodeStyledPrinter
|
* @var CodeStyledPrinter
|
||||||
*/
|
*/
|
||||||
private $codeStyledPrinter;
|
private $codeStyledPrinter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Lexer
|
* @var Lexer
|
||||||
*/
|
*/
|
||||||
|
|
||||||
private $lexer;
|
private $lexer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var NodeTraverser
|
* @var NodeTraverser
|
||||||
*/
|
*/
|
||||||
private $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->parser = $parser;
|
||||||
$this->codeStyledPrinter = $codeStyledPrinter;
|
$this->codeStyledPrinter = $codeStyledPrinter;
|
||||||
$this->lexer = $lexer;
|
$this->lexer = $lexer;
|
||||||
|
@ -43,12 +48,8 @@ final class FileReconstructor
|
||||||
{
|
{
|
||||||
$fileContent = file_get_contents($file->getRealPath());
|
$fileContent = file_get_contents($file->getRealPath());
|
||||||
|
|
||||||
/** @var Node[] $nodes */
|
|
||||||
$oldStmts = $this->parser->parse($fileContent);
|
$oldStmts = $this->parser->parse($fileContent);
|
||||||
|
|
||||||
// keep format printer
|
|
||||||
$oldTokens = $this->lexer->getTokens();
|
$oldTokens = $this->lexer->getTokens();
|
||||||
|
|
||||||
$newStmts = $this->nodeTraverser->traverse($oldStmts);
|
$newStmts = $this->nodeTraverser->traverse($oldStmts);
|
||||||
|
|
||||||
return $this->codeStyledPrinter->printToString($newStmts, $oldStmts, $oldTokens);
|
return $this->codeStyledPrinter->printToString($newStmts, $oldStmts, $oldTokens);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user