mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-29 11:33:33 +01:00
add StateHolder as hotfix for printer bug
This commit is contained in:
parent
5dfa359257
commit
9ab6c84629
22
src/NodeTraverser/StateHolder.php
Normal file
22
src/NodeTraverser/StateHolder.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\NodeTraverser;
|
||||
|
||||
final class StateHolder
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $isAfterTraverseCalled = false;
|
||||
|
||||
public function setAfterTraverserIsCalled(): void
|
||||
{
|
||||
$this->isAfterTraverseCalled = true;
|
||||
}
|
||||
|
||||
public function isAfterTraverseCalled(): bool
|
||||
{
|
||||
return $this->isAfterTraverseCalled;
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ use PhpParser\NodeVisitorAbstract;
|
||||
use Rector\Builder\Class_\ClassPropertyCollector;
|
||||
use Rector\Builder\ConstructorMethodBuilder;
|
||||
use Rector\Builder\PropertyBuilder;
|
||||
use Rector\NodeTraverser\StateHolder;
|
||||
|
||||
/**
|
||||
* Add new propertis to class and to contructor.
|
||||
@ -29,14 +30,21 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract
|
||||
*/
|
||||
private $newClassPropertyCollector;
|
||||
|
||||
/**
|
||||
* @var StateHolder
|
||||
*/
|
||||
private $stateHolder;
|
||||
|
||||
public function __construct(
|
||||
ConstructorMethodBuilder $constructorMethodBuilder,
|
||||
PropertyBuilder $propertyBuilder,
|
||||
ClassPropertyCollector $newClassPropertyCollector
|
||||
ClassPropertyCollector $newClassPropertyCollector,
|
||||
StateHolder $stateHolder
|
||||
) {
|
||||
$this->constructorMethodBuilder = $constructorMethodBuilder;
|
||||
$this->propertyBuilder = $propertyBuilder;
|
||||
$this->newClassPropertyCollector = $newClassPropertyCollector;
|
||||
$this->stateHolder = $stateHolder;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -60,6 +68,7 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract
|
||||
$propertiesForClass = $this->newClassPropertyCollector->getPropertiesforClass($className);
|
||||
|
||||
foreach ($propertiesForClass as $propertyType => $propertyName) {
|
||||
$this->stateHolder->setAfterTraverserIsCalled();
|
||||
$this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $propertyType, $propertyName);
|
||||
$this->propertyBuilder->addPropertyToClass($classNode, $propertyType, $propertyName);
|
||||
}
|
||||
|
@ -3,9 +3,9 @@
|
||||
namespace Rector\Testing\Application;
|
||||
|
||||
use PhpParser\Lexer;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\Parser;
|
||||
use Rector\NodeTraverser\StateHolder;
|
||||
use Rector\Printer\CodeStyledPrinter;
|
||||
use SplFileInfo;
|
||||
|
||||
@ -31,16 +31,23 @@ final class FileReconstructor
|
||||
*/
|
||||
private $nodeTraverser;
|
||||
|
||||
/**
|
||||
* @var StateHolder
|
||||
*/
|
||||
private $stateHolder;
|
||||
|
||||
public function __construct(
|
||||
Parser $parser,
|
||||
CodeStyledPrinter $codeStyledPrinter,
|
||||
Lexer $lexer,
|
||||
NodeTraverser $nodeTraverser
|
||||
NodeTraverser $nodeTraverser,
|
||||
StateHolder $stateHolder
|
||||
) {
|
||||
$this->parser = $parser;
|
||||
$this->codeStyledPrinter = $codeStyledPrinter;
|
||||
$this->lexer = $lexer;
|
||||
$this->nodeTraverser = $nodeTraverser;
|
||||
$this->stateHolder = $stateHolder;
|
||||
}
|
||||
|
||||
# ref: https://github.com/nikic/PHP-Parser/issues/344#issuecomment-298162516
|
||||
@ -52,6 +59,10 @@ final class FileReconstructor
|
||||
$oldTokens = $this->lexer->getTokens();
|
||||
$newStmts = $this->nodeTraverser->traverse($oldStmts);
|
||||
|
||||
if (! $this->stateHolder->isAfterTraverseCalled()) {
|
||||
[$newStmts, $oldStmts] = [$oldStmts, $newStmts];
|
||||
}
|
||||
|
||||
return $this->codeStyledPrinter->printToString($newStmts, $oldStmts, $oldTokens);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user