Skip to content

Commit

Permalink
Make ChangeGlobalVariablesToPropertiesRector add properties directly,…
Browse files Browse the repository at this point in the history
… remove Nette inject methods (#3957)

* Make ChangeGlobalVariablesToPropertiesRector add properties directly

* remove inject property

* note

* remove parent property empty, as inject not used anymore
  • Loading branch information
TomasVotruba committed May 24, 2023
1 parent 69220a7 commit 179b5cb
Show file tree
Hide file tree
Showing 14 changed files with 14 additions and 320 deletions.
46 changes: 0 additions & 46 deletions .github/workflows/e2e_autoload.yaml

This file was deleted.

1 change: 0 additions & 1 deletion e2e/parent-property-empty/.gitignore

This file was deleted.

7 changes: 0 additions & 7 deletions e2e/parent-property-empty/composer.json

This file was deleted.

28 changes: 0 additions & 28 deletions e2e/parent-property-empty/expected-output.diff

This file was deleted.

22 changes: 0 additions & 22 deletions e2e/parent-property-empty/rector.php

This file was deleted.

23 changes: 0 additions & 23 deletions e2e/parent-property-empty/src/ProfilePresenter.php

This file was deleted.

34 changes: 1 addition & 33 deletions packages/PostRector/Collector/PropertyToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\PostRector\Collector;

use PhpParser\Node\Stmt\Class_;
use PHPStan\Type\Type;
use Rector\ChangesReporting\Collector\RectorChangeCollector;
use Rector\PostRector\Contract\Collector\NodeCollectorInterface;
use Rector\PostRector\ValueObject\PropertyMetadata;
Expand All @@ -17,23 +16,14 @@ final class PropertyToAddCollector implements NodeCollectorInterface
*/
private array $propertiesByClass = [];

/**
* @var array<string, array<string, Type|null>>
*/
private array $propertiesWithoutConstructorByClass = [];

public function __construct(
private readonly RectorChangeCollector $rectorChangeCollector
) {
}

public function isActive(): bool
{
if ($this->propertiesByClass !== []) {
return true;
}

return $this->propertiesWithoutConstructorByClass !== [];
return $this->propertiesByClass !== [];
}

public function addPropertyToClass(Class_ $class, PropertyMetadata $propertyMetadata): void
Expand All @@ -44,19 +34,6 @@ public function addPropertyToClass(Class_ $class, PropertyMetadata $propertyMeta
$this->rectorChangeCollector->notifyNodeFileInfo($class);
}

/**
* @api
*/
public function addPropertyWithoutConstructorToClass(
string $propertyName,
?Type $propertyType,
Class_ $class
): void {
$this->propertiesWithoutConstructorByClass[spl_object_hash($class)][$propertyName] = $propertyType;

$this->rectorChangeCollector->notifyNodeFileInfo($class);
}

/**
* @return PropertyMetadata[]
*/
Expand All @@ -65,13 +42,4 @@ public function getPropertiesByClass(Class_ $class): array
$classHash = spl_object_hash($class);
return $this->propertiesByClass[$classHash] ?? [];
}

/**
* @return array<string, Type|null>
*/
public function getPropertiesWithoutConstructorByClass(Class_ $class): array
{
$classHash = spl_object_hash($class);
return $this->propertiesWithoutConstructorByClass[$classHash] ?? [];
}
}
87 changes: 0 additions & 87 deletions packages/PostRector/NodeAnalyzer/NetteInjectDetector.php

This file was deleted.

24 changes: 1 addition & 23 deletions packages/PostRector/Rector/PropertyAddingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
use PhpParser\Node\Stmt\Class_;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\NodeManipulator\ClassDependencyManipulator;
use Rector\Core\NodeManipulator\ClassInsertManipulator;
use Rector\PostRector\Collector\PropertyToAddCollector;
use Rector\PostRector\NodeAnalyzer\NetteInjectDetector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -21,8 +19,6 @@ final class PropertyAddingPostRector extends AbstractPostRector
{
public function __construct(
private readonly ClassDependencyManipulator $classDependencyManipulator,
private readonly ClassInsertManipulator $classInsertManipulator,
private readonly NetteInjectDetector $netteInjectDetector,
private readonly PropertyToAddCollector $propertyToAddCollector,
private readonly ClassAnalyzer $classAnalyzer
) {
Expand All @@ -44,7 +40,6 @@ public function enterNode(Node $node): ?Node
}

$this->addProperties($node);
$this->addPropertiesWithoutConstructor($node);

return $node;
}
Expand Down Expand Up @@ -83,25 +78,8 @@ private function addProperties(Class_ $class): void
{
$propertiesMetadatas = $this->propertyToAddCollector->getPropertiesByClass($class);

$isNetteInjectPreferred = $this->netteInjectDetector->isNetteInjectPreferred($class);

foreach ($propertiesMetadatas as $propertyMetadata) {
if (! $isNetteInjectPreferred) {
$this->classDependencyManipulator->addConstructorDependency($class, $propertyMetadata);
} else {
$this->classDependencyManipulator->addInjectProperty($class, $propertyMetadata);
}
}
}

private function addPropertiesWithoutConstructor(Class_ $class): void
{
$propertiesWithoutConstructor = $this->propertyToAddCollector->getPropertiesWithoutConstructorByClass(
$class
);

foreach ($propertiesWithoutConstructor as $propertyName => $propertyType) {
$this->classInsertManipulator->addPropertyToClass($class, $propertyName, $propertyType);
$this->classDependencyManipulator->addConstructorDependency($class, $propertyMetadata);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Global_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PhpParser\NodeTraverser;
use Rector\Core\Rector\AbstractRector;
use Rector\PostRector\Collector\PropertyToAddCollector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -30,11 +31,6 @@ final class ChangeGlobalVariablesToPropertiesRector extends AbstractRector
*/
private array $globalVariableNames = [];

public function __construct(
private readonly PropertyToAddCollector $propertyToAddCollector
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -99,10 +95,16 @@ public function refactor(Node $node): ?Node
return null;
}

// @todo find ideal property position
$globalProperties = [];
foreach ($this->globalVariableNames as $globalVariableName) {
$this->propertyToAddCollector->addPropertyWithoutConstructorToClass($globalVariableName, null, $node);
$globalProperties[] = new Property(Class_::MODIFIER_PRIVATE, [
new PropertyProperty($globalVariableName),
]);
}

array_splice($node->stmts, 0, 0, $globalProperties);

return $node;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v28';
private const CACHE_KEY = 'v30';

private ContainerInterface|null $container = null;

Expand Down

0 comments on commit 179b5cb

Please sign in to comment.