fix it all

This commit is contained in:
TomasVotruba 2017-08-08 18:17:40 +02:00
parent 102298d49a
commit 478ce64138
10 changed files with 121 additions and 7 deletions

View File

@ -41,6 +41,7 @@ checkers:
# Namespaces
- PhpCsFixer\Fixer\Import\OrderedImportsFixer
- PhpCsFixer\Fixer\Import\NoUnusedImportsFixer
PhpCsFixer\Fixer\Operator\ConcatSpaceFixer:
spacing: one

View File

@ -27,6 +27,10 @@ final class PropertyBuilder
public function addPropertyToClass(Class_ $classNode, string $propertyType, string $propertyName): void
{
if ($this->doesPropertyAlreadyExist($classNode, $propertyName)) {
return;
}
$propertyNode = $this->buildPrivatePropertyNode($propertyType, $propertyName);
$this->statementGlue->addAsFirstMethod($classNode, $propertyNode);
@ -49,4 +53,20 @@ final class PropertyBuilder
. PHP_EOL . ' * @var ' . $propertyType
. PHP_EOL . ' */');
}
private function doesPropertyAlreadyExist(Class_ $classNode, string $propertyName): bool
{
foreach ($classNode->stmts as $inClassNode) {
if (! $inClassNode instanceof Property) {
continue;
}
$classPropertyName = (string) $inClassNode->props[0]->name;
if ($classPropertyName === $propertyName) {
return true;
}
}
return false;
}
}

View File

@ -5,6 +5,8 @@ namespace Rector\NodeTraverser;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use PhpParser\NodeVisitor\CloningVisitor;
use Rector\NodeVisitor\Traverse\NodeConnectorNodeVisitor;
use Rector\NodeVisitor\Traverse\ParentConnectorNodeVisitor;
final class NodeTraverserFactory
{
@ -13,6 +15,15 @@ final class NodeTraverserFactory
*/
private $nodeVisitors = [];
/**
* @var string[]
*/
private $priorityNodeVisitorClasses = [
CloningVisitor::class,
ParentConnectorNodeVisitor::class,
NodeConnectorNodeVisitor::class
];
public function addNodeVisitor(NodeVisitor $nodeVisitor): void
{
$this->nodeVisitors[] = $nodeVisitor;
@ -22,10 +33,15 @@ final class NodeTraverserFactory
{
$nodeTraverser = new NodeTraverser;
// this one has priority
$nodeTraverser->addVisitor(new CloningVisitor);
foreach ($this->priorityNodeVisitorClasses as $priorityNodeVisitor) {
$nodeTraverser->addVisitor(new $priorityNodeVisitor);
}
foreach ($this->nodeVisitors as $nodeVisitor) {
if (in_array(get_class($nodeVisitor), $this->priorityNodeVisitorClasses, true)) {
continue;
}
$nodeTraverser->addVisitor($nodeVisitor);
}

View File

@ -70,8 +70,6 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract
{
$propertiesForClass = $this->newClassPropertyCollector->getPropertiesforClass($className);
dump($propertiesForClass);
foreach ($propertiesForClass as $propertyType => $propertyName) {
$this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $propertyType, $propertyName);
$this->propertyBuilder->addPropertyToClass($classNode, $propertyType, $propertyName);

View File

@ -45,6 +45,8 @@ final class PropertyRector extends NodeVisitorAbstract
*/
public function beforeTraverse(array $nodes): ?array
{
$this->className = null;
foreach ($nodes as $node) {
if ($node instanceof Class_) {
$this->className = (string) $node->name;

View File

@ -60,6 +60,8 @@ final class GetterToPropertyRector extends NodeVisitorAbstract
*/
public function beforeTraverse(array $nodes): ?array
{
$this->className = null;
foreach ($nodes as $node) {
if ($node instanceof Class_) {
$this->className = (string) $node->name;

View File

@ -0,0 +1,45 @@
<?php declare(strict_types=1);
namespace Rector\NodeVisitor\Traverse;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
final class NodeConnectorNodeVisitor extends NodeVisitorAbstract
{
/**
* @var Node[]
*/
private $stack = [];
/**
* @var Node
*/
private $prev;
public function beforeTraverse(array $nodes): void
{
$this->stack = [];
$this->prev = null;
}
public function enterNode(Node $node): void
{
if (! empty($this->stack)) {
$node->setAttribute('parent', $this->stack[count($this->stack)-1]);
}
if ($this->prev && $this->prev->getAttribute('parent') === $node->getAttribute('parent')) {
$node->setAttribute('prev', $this->prev);
$this->prev->setAttribute('next', $node);
}
$this->stack[] = $node;
}
public function leaveNode(Node $node): void
{
$this->prev = $node;
array_pop($this->stack);
}
}

View File

@ -0,0 +1,33 @@
<?php declare(strict_types=1);
namespace Rector\NodeVisitor\Traverse;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
final class ParentConnectorNodeVisitor extends NodeVisitorAbstract
{
/**
* @var Node[]
*/
private $stack = [];
public function beforeTraverse(array $nodes): void
{
$this->stack = [];
}
public function enterNode(Node $node): void
{
if (! empty($this->stack)) {
$node->setAttribute('parent', $this->stack[count($this->stack)-1]);
}
$this->stack[] = $node;
}
public function leaveNode(Node $node): void
{
array_pop($this->stack);
}
}

View File

@ -4,7 +4,6 @@ namespace Rector\Printer;
use PhpParser\PrettyPrinter\Standard;
use SplFileInfo;
use Tracy\Debugger;
final class FormatPerservingPrinter
{

View File

@ -6,12 +6,10 @@ class ClassWithInjects
* @var stdClass
*/
private $property;
/**
* @var DateTimeInterface
*/
private $otherProperty;
public function __construct(stdClass $property, DateTimeInterface $otherProperty)
{
$this->property = $property;