mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-06 19:30:50 +00:00
fix it all
This commit is contained in:
parent
102298d49a
commit
478ce64138
|
@ -41,6 +41,7 @@ checkers:
|
||||||
|
|
||||||
# Namespaces
|
# Namespaces
|
||||||
- PhpCsFixer\Fixer\Import\OrderedImportsFixer
|
- PhpCsFixer\Fixer\Import\OrderedImportsFixer
|
||||||
|
- PhpCsFixer\Fixer\Import\NoUnusedImportsFixer
|
||||||
|
|
||||||
PhpCsFixer\Fixer\Operator\ConcatSpaceFixer:
|
PhpCsFixer\Fixer\Operator\ConcatSpaceFixer:
|
||||||
spacing: one
|
spacing: one
|
||||||
|
|
|
@ -27,6 +27,10 @@ final class PropertyBuilder
|
||||||
|
|
||||||
public function addPropertyToClass(Class_ $classNode, string $propertyType, string $propertyName): void
|
public function addPropertyToClass(Class_ $classNode, string $propertyType, string $propertyName): void
|
||||||
{
|
{
|
||||||
|
if ($this->doesPropertyAlreadyExist($classNode, $propertyName)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$propertyNode = $this->buildPrivatePropertyNode($propertyType, $propertyName);
|
$propertyNode = $this->buildPrivatePropertyNode($propertyType, $propertyName);
|
||||||
|
|
||||||
$this->statementGlue->addAsFirstMethod($classNode, $propertyNode);
|
$this->statementGlue->addAsFirstMethod($classNode, $propertyNode);
|
||||||
|
@ -49,4 +53,20 @@ final class PropertyBuilder
|
||||||
. PHP_EOL . ' * @var ' . $propertyType
|
. PHP_EOL . ' * @var ' . $propertyType
|
||||||
. PHP_EOL . ' */');
|
. 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,8 @@ namespace Rector\NodeTraverser;
|
||||||
use PhpParser\NodeTraverser;
|
use PhpParser\NodeTraverser;
|
||||||
use PhpParser\NodeVisitor;
|
use PhpParser\NodeVisitor;
|
||||||
use PhpParser\NodeVisitor\CloningVisitor;
|
use PhpParser\NodeVisitor\CloningVisitor;
|
||||||
|
use Rector\NodeVisitor\Traverse\NodeConnectorNodeVisitor;
|
||||||
|
use Rector\NodeVisitor\Traverse\ParentConnectorNodeVisitor;
|
||||||
|
|
||||||
final class NodeTraverserFactory
|
final class NodeTraverserFactory
|
||||||
{
|
{
|
||||||
|
@ -13,6 +15,15 @@ final class NodeTraverserFactory
|
||||||
*/
|
*/
|
||||||
private $nodeVisitors = [];
|
private $nodeVisitors = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
private $priorityNodeVisitorClasses = [
|
||||||
|
CloningVisitor::class,
|
||||||
|
ParentConnectorNodeVisitor::class,
|
||||||
|
NodeConnectorNodeVisitor::class
|
||||||
|
];
|
||||||
|
|
||||||
public function addNodeVisitor(NodeVisitor $nodeVisitor): void
|
public function addNodeVisitor(NodeVisitor $nodeVisitor): void
|
||||||
{
|
{
|
||||||
$this->nodeVisitors[] = $nodeVisitor;
|
$this->nodeVisitors[] = $nodeVisitor;
|
||||||
|
@ -22,10 +33,15 @@ final class NodeTraverserFactory
|
||||||
{
|
{
|
||||||
$nodeTraverser = new NodeTraverser;
|
$nodeTraverser = new NodeTraverser;
|
||||||
|
|
||||||
// this one has priority
|
foreach ($this->priorityNodeVisitorClasses as $priorityNodeVisitor) {
|
||||||
$nodeTraverser->addVisitor(new CloningVisitor);
|
$nodeTraverser->addVisitor(new $priorityNodeVisitor);
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($this->nodeVisitors as $nodeVisitor) {
|
foreach ($this->nodeVisitors as $nodeVisitor) {
|
||||||
|
if (in_array(get_class($nodeVisitor), $this->priorityNodeVisitorClasses, true)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$nodeTraverser->addVisitor($nodeVisitor);
|
$nodeTraverser->addVisitor($nodeVisitor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -70,8 +70,6 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract
|
||||||
{
|
{
|
||||||
$propertiesForClass = $this->newClassPropertyCollector->getPropertiesforClass($className);
|
$propertiesForClass = $this->newClassPropertyCollector->getPropertiesforClass($className);
|
||||||
|
|
||||||
dump($propertiesForClass);
|
|
||||||
|
|
||||||
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);
|
||||||
|
|
|
@ -45,6 +45,8 @@ final class PropertyRector extends NodeVisitorAbstract
|
||||||
*/
|
*/
|
||||||
public function beforeTraverse(array $nodes): ?array
|
public function beforeTraverse(array $nodes): ?array
|
||||||
{
|
{
|
||||||
|
$this->className = null;
|
||||||
|
|
||||||
foreach ($nodes as $node) {
|
foreach ($nodes as $node) {
|
||||||
if ($node instanceof Class_) {
|
if ($node instanceof Class_) {
|
||||||
$this->className = (string) $node->name;
|
$this->className = (string) $node->name;
|
||||||
|
|
|
@ -60,6 +60,8 @@ final class GetterToPropertyRector extends NodeVisitorAbstract
|
||||||
*/
|
*/
|
||||||
public function beforeTraverse(array $nodes): ?array
|
public function beforeTraverse(array $nodes): ?array
|
||||||
{
|
{
|
||||||
|
$this->className = null;
|
||||||
|
|
||||||
foreach ($nodes as $node) {
|
foreach ($nodes as $node) {
|
||||||
if ($node instanceof Class_) {
|
if ($node instanceof Class_) {
|
||||||
$this->className = (string) $node->name;
|
$this->className = (string) $node->name;
|
||||||
|
|
45
src/NodeVisitor/Traverse/NodeConnectorNodeVisitor.php
Normal file
45
src/NodeVisitor/Traverse/NodeConnectorNodeVisitor.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
33
src/NodeVisitor/Traverse/ParentConnectorNodeVisitor.php
Normal file
33
src/NodeVisitor/Traverse/ParentConnectorNodeVisitor.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,7 +4,6 @@ namespace Rector\Printer;
|
||||||
|
|
||||||
use PhpParser\PrettyPrinter\Standard;
|
use PhpParser\PrettyPrinter\Standard;
|
||||||
use SplFileInfo;
|
use SplFileInfo;
|
||||||
use Tracy\Debugger;
|
|
||||||
|
|
||||||
final class FormatPerservingPrinter
|
final class FormatPerservingPrinter
|
||||||
{
|
{
|
||||||
|
|
|
@ -6,12 +6,10 @@ class ClassWithInjects
|
||||||
* @var stdClass
|
* @var stdClass
|
||||||
*/
|
*/
|
||||||
private $property;
|
private $property;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var DateTimeInterface
|
* @var DateTimeInterface
|
||||||
*/
|
*/
|
||||||
private $otherProperty;
|
private $otherProperty;
|
||||||
|
|
||||||
public function __construct(stdClass $property, DateTimeInterface $otherProperty)
|
public function __construct(stdClass $property, DateTimeInterface $otherProperty)
|
||||||
{
|
{
|
||||||
$this->property = $property;
|
$this->property = $property;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user