mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 13:28:18 +01:00
[NamedServicesToContrutor] make tests pass
This commit is contained in:
parent
80314eb924
commit
09c13a11fa
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Rector\Application;
|
||||
|
||||
use PhpParser\Lexer;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\Parser;
|
||||
use Rector\Printer\CodeStyledPrinter;
|
||||
@ -24,11 +25,17 @@ final class FileProcessor
|
||||
*/
|
||||
private $nodeTraverser;
|
||||
|
||||
public function __construct(Parser $parser, CodeStyledPrinter $codeStyledPrinter, NodeTraverser $nodeTraverser)
|
||||
/**
|
||||
* @var Lexer
|
||||
*/
|
||||
private $lexer;
|
||||
|
||||
public function __construct(Parser $parser, CodeStyledPrinter $codeStyledPrinter, Lexer $lexer, NodeTraverser $nodeTraverser)
|
||||
{
|
||||
$this->parser = $parser;
|
||||
$this->codeStyledPrinter = $codeStyledPrinter;
|
||||
$this->nodeTraverser = $nodeTraverser;
|
||||
$this->lexer = $lexer;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -44,18 +51,19 @@ final class FileProcessor
|
||||
public function processFile(SplFileInfo $file): void
|
||||
{
|
||||
$fileContent = file_get_contents($file->getRealPath());
|
||||
$nodes = $this->parser->parse($fileContent);
|
||||
if ($nodes === null) {
|
||||
$oldStmts = $this->parser->parse($fileContent);
|
||||
if ($oldStmts === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$originalNodes = $this->cloneArrayOfObjects($nodes);
|
||||
$oldStmts = $this->cloneArrayOfObjects($oldStmts);
|
||||
|
||||
$oldTokens = $this->lexer->getTokens();
|
||||
|
||||
$newStmts = $this->nodeTraverser->traverse($oldStmts);
|
||||
|
||||
|
||||
$this->nodeTraverser->traverse($nodes);
|
||||
|
||||
|
||||
$this->codeStyledPrinter->printToFile($file, $originalNodes, $nodes);
|
||||
$this->codeStyledPrinter->printToFile($file, $newStmts, $oldStmts, $oldTokens);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Buillder\Class_;
|
||||
namespace Rector\Builder\Class_;
|
||||
|
||||
final class ClassPropertyCollector
|
||||
{
|
||||
@ -21,6 +21,6 @@ final class ClassPropertyCollector
|
||||
*/
|
||||
public function getPropertiesforClass(string $class): array
|
||||
{
|
||||
return $this->classProperties[$class] ?: [];
|
||||
return $this->classProperties[$class] ?? [];
|
||||
}
|
||||
}
|
||||
|
@ -2,11 +2,12 @@
|
||||
|
||||
namespace Rector\NodeVisitor\DependencyInjection\NamedServicesToConstructor;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
use Rector\Builder\Class_\ClassPropertyCollector;
|
||||
use Rector\Builder\ConstructorMethodBuilder;
|
||||
use Rector\Builder\PropertyBuilder;
|
||||
use Rector\Buillder\Class_\ClassPropertyCollector;
|
||||
|
||||
/**
|
||||
* Add new propertis to class and to contructor.
|
||||
@ -43,6 +44,10 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract
|
||||
$this->newClassPropertyCollector = $newClassPropertyCollector;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
* @return Node[]
|
||||
*/
|
||||
public function afterTraverse(array $nodes): array
|
||||
{
|
||||
foreach ($nodes as $node) {
|
||||
@ -58,7 +63,6 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract
|
||||
private function reconstruct(Class_ $classNode): void
|
||||
{
|
||||
$propertiesForClass = $this->newClassPropertyCollector->getPropertiesforClass($this->className);
|
||||
|
||||
foreach ($propertiesForClass as $propertyType => $propertyName) {
|
||||
$this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $propertyType, $propertyName);
|
||||
$this->propertyBuilder->addPropertyToClass($classNode, $propertyType, $propertyName);
|
||||
|
@ -11,7 +11,7 @@ use PhpParser\Node\Scalar\String_;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
use Rector\Builder\Kernel\ServiceFromKernelResolver;
|
||||
use Rector\Builder\Naming\NameResolver;
|
||||
use Rector\Buillder\Class_\ClassPropertyCollector;
|
||||
use Rector\Builder\Class_\ClassPropertyCollector;
|
||||
use Rector\Tests\NodeVisitor\DependencyInjection\NamedServicesToConstructorReconstructor\Source\LocalKernel;
|
||||
|
||||
/**
|
||||
@ -85,6 +85,7 @@ final class GetterToPropertyNodeVisitor extends NodeVisitorAbstract
|
||||
{
|
||||
if ($this->isCandidate($node)) {
|
||||
$this->reconstruct($node);
|
||||
return $node;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -5,6 +5,10 @@ namespace Rector\Parser;
|
||||
use PhpParser\Lexer;
|
||||
use PhpParser\Lexer\Emulative;
|
||||
|
||||
/**
|
||||
* This Lexer allows Format-perserving AST Transformations
|
||||
* @see https://github.com/nikic/PHP-Parser/issues/344#issuecomment-298162516
|
||||
*/
|
||||
final class LexerFactory
|
||||
{
|
||||
public function create(): Lexer
|
||||
|
@ -17,18 +17,18 @@ final class CodeStyledPrinter
|
||||
$this->prettyPrinter = $prettyPrinter;
|
||||
}
|
||||
|
||||
public function printToFile(SplFileInfo $file, array $originalNodes, array $newNodes): void
|
||||
public function printToFile(SplFileInfo $file, array $newStmts, array $oldStmts, array $oldTokens): void
|
||||
{
|
||||
if ($originalNodes === $newNodes) {
|
||||
if ($oldStmts === $newStmts) {
|
||||
return;
|
||||
}
|
||||
|
||||
file_put_contents($file->getRealPath(), $this->printToString($newNodes));
|
||||
file_put_contents($file->getRealPath(), $this->printToString($newStmts, $oldStmts, $oldTokens));
|
||||
// @todo: run ecs with minimal set to code look nice
|
||||
}
|
||||
|
||||
public function printToString(array $oldStmts, array $newStmts, array $oldTokens): string
|
||||
public function printToString(array $newStmts, array $oldStmts, array $oldTokens): string
|
||||
{
|
||||
return $this->prettyPrinter->printFormatPreserving($oldStmts, $newStmts, $oldTokens);
|
||||
return $this->prettyPrinter->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
|
||||
}
|
||||
}
|
||||
|
@ -51,6 +51,6 @@ final class FileReconstructor
|
||||
|
||||
$newStmts = $this->nodeTraverser->traverse($oldStmts);
|
||||
|
||||
return $this->codeStyledPrinter->printToString($oldStmts, $newStmts, $oldTokens);
|
||||
return $this->codeStyledPrinter->printToString($newStmts, $oldStmts, $oldTokens);
|
||||
}
|
||||
}
|
||||
|
@ -8,8 +8,19 @@ final class Test extends AbstractReconstructorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFileMatchesExpectedContent(__DIR__ . '/wrong/wrong.php.inc', __DIR__ . '/correct/correct.php.inc');
|
||||
$this->doTestFileMatchesExpectedContent(__DIR__ . '/wrong/wrong2.php.inc', __DIR__ . '/correct/correct2.php.inc');
|
||||
// $this->doTestFileMatchesExpectedContent(__DIR__ . '/wrong/wrong3.php.inc', __DIR__ . '/correct/correct3.php.inc');
|
||||
$this->doTestFileMatchesExpectedContent(
|
||||
__DIR__ . '/wrong/wrong.php.inc',
|
||||
__DIR__ . '/correct/correct.php.inc'
|
||||
);
|
||||
|
||||
$this->doTestFileMatchesExpectedContent(
|
||||
__DIR__ . '/wrong/wrong2.php.inc',
|
||||
__DIR__ . '/correct/correct2.php.inc'
|
||||
);
|
||||
|
||||
$this->doTestFileMatchesExpectedContent(
|
||||
__DIR__ . '/wrong/wrong3.php.inc',
|
||||
__DIR__ . '/correct/correct3.php.inc'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
<?php declare (strict_types=1);
|
||||
|
||||
class ClassWithNamedService implements ContainerAwareInterface
|
||||
{
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user