Skip to content

Commit

Permalink
Remove ChangeReadOnlyVariableWithDefaultValueToConstantRector overly …
Browse files Browse the repository at this point in the history
…complex and risky, better job for PHPStan (#3954)
  • Loading branch information
TomasVotruba committed May 24, 2023
1 parent c4b524e commit ce12406
Show file tree
Hide file tree
Showing 66 changed files with 17 additions and 2,379 deletions.
132 changes: 6 additions & 126 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# 402 Rules Overview
# 398 Rules Overview

<br>

## Categories

- [Arguments](#arguments) (6)

- [CodeQuality](#codequality) (75)
- [CodeQuality](#codequality) (74)

- [CodingStyle](#codingstyle) (34)

- [Compatibility](#compatibility) (1)

- [DeadCode](#deadcode) (47)
- [DeadCode](#deadcode) (46)

- [DependencyInjection](#dependencyinjection) (2)

Expand Down Expand Up @@ -50,7 +50,7 @@

- [Php82](#php82) (3)

- [Privatization](#privatization) (7)
- [Privatization](#privatization) (6)

- [Removing](#removing) (6)

Expand All @@ -60,7 +60,7 @@

- [Strict](#strict) (6)

- [Transform](#transform) (29)
- [Transform](#transform) (28)

- [TypeDeclaration](#typedeclaration) (40)

Expand Down Expand Up @@ -725,38 +725,6 @@ Make if conditions more explicit

<br>

### ExplicitMethodCallOverMagicGetSetRector

Replace magic property fetch using `__get()` and `__set()` with existing method get*()/set*() calls

- class: [`Rector\CodeQuality\Rector\PropertyFetch\ExplicitMethodCallOverMagicGetSetRector`](../rules/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector.php)

```diff
class MagicCallsObject
{
// adds magic __get() and __set() methods
use \Nette\SmartObject;

private $name;

public function getName()
{
return $this->name;
}
}

class SomeClass
{
public function run(MagicObject $magicObject)
{
- return $magicObject->name;
+ return $magicObject->getName();
}
}
```

<br>

### FlipTypeControlToUseExclusiveTypeRector

Flip type control from null compare to use exclusive instanceof object
Expand Down Expand Up @@ -2943,28 +2911,6 @@ Remove duplicated key in defined arrays.

<br>

### RemoveDuplicatedInstanceOfRector

Remove duplicated instanceof in one call

- class: [`Rector\DeadCode\Rector\BinaryOp\RemoveDuplicatedInstanceOfRector`](../rules/DeadCode/Rector/BinaryOp/RemoveDuplicatedInstanceOfRector.php)

```diff
class SomeClass
{
- public function run($value)
+ public function run($value): void
{
- $isIt = $value instanceof A || $value instanceof A;
- $isIt = $value instanceof A && $value instanceof A;
+ $isIt = $value instanceof A;
+ $isIt = $value instanceof A;
}
}
```

<br>

### RemoveEmptyClassMethodRector

Remove empty class methods not required by parents
Expand Down Expand Up @@ -5506,6 +5452,7 @@ Change curly based array and string to square bracket
$string = 'test';
-echo $string{0};
+echo $string[0];

$array = ['test'];
-echo $array{0};
+echo $array[0];
Expand Down Expand Up @@ -6524,39 +6471,6 @@ Change property with read only status with default value to constant

<br>

### ChangeReadOnlyVariableWithDefaultValueToConstantRector

Change variable with read only status with default value to constant

- class: [`Rector\Privatization\Rector\Class_\ChangeReadOnlyVariableWithDefaultValueToConstantRector`](../rules/Privatization/Rector/Class_/ChangeReadOnlyVariableWithDefaultValueToConstantRector.php)

```diff
class SomeClass
{
+ /**
+ * @var string[]
+ */
+ private const REPLACEMENTS = [
+ 'PHPUnit\Framework\TestCase\Notice' => 'expectNotice',
+ 'PHPUnit\Framework\TestCase\Deprecated' => 'expectDeprecation',
+ ];
+
public function run()
{
- $replacements = [
- 'PHPUnit\Framework\TestCase\Notice' => 'expectNotice',
- 'PHPUnit\Framework\TestCase\Deprecated' => 'expectDeprecation',
- ];
-
- foreach ($replacements as $class => $method) {
+ foreach (self::REPLACEMENTS as $class => $method) {
}
}
}
```

<br>

### FinalizeClassesWithoutChildrenRector

Finalize every class that has no children
Expand Down Expand Up @@ -7774,40 +7688,6 @@ return static function (RectorConfig $rectorConfig): void {

<br>

### GetAndSetToMethodCallRector

Turns defined `__get`/`__set` to specific method calls.

:wrench: **configure it!**

- class: [`Rector\Transform\Rector\Assign\GetAndSetToMethodCallRector`](../rules/Transform/Rector/Assign/GetAndSetToMethodCallRector.php)

```php
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Transform\Rector\Assign\GetAndSetToMethodCallRector;
use Rector\Transform\ValueObject\GetAndSetToMethodCall;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(GetAndSetToMethodCallRector::class, [
new GetAndSetToMethodCall('SomeContainer', 'addService', 'getService'),
]);
};
```


```diff
$container = new SomeContainer;
-$container->someService = $someService;
+$container->setService("someService", $someService);
```

<br>

### MergeInterfacesRector

Merges old interface to a new one, that already has its methods
Expand Down
2 changes: 0 additions & 2 deletions config/set/privatization.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Rector\Config\RectorConfig;
use Rector\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector;
use Rector\Privatization\Rector\Class_\ChangeReadOnlyVariableWithDefaultValueToConstantRector;
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector;
use Rector\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector;
use Rector\Privatization\Rector\MethodCall\PrivatizeLocalGetterToPropertyRector;
Expand All @@ -15,7 +14,6 @@
$rectorConfig->rule(FinalizeClassesWithoutChildrenRector::class);
$rectorConfig->rule(ChangeGlobalVariablesToPropertiesRector::class);
$rectorConfig->rule(ChangeReadOnlyPropertyWithDefaultValueToConstantRector::class);
$rectorConfig->rule(ChangeReadOnlyVariableWithDefaultValueToConstantRector::class);
$rectorConfig->rule(PrivatizeLocalGetterToPropertyRector::class);
$rectorConfig->rule(PrivatizeFinalClassPropertyRector::class);
$rectorConfig->rule(PrivatizeFinalClassMethodRector::class);
Expand Down

This file was deleted.

31 changes: 1 addition & 30 deletions packages/PostRector/Collector/PropertyToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,13 @@
namespace Rector\PostRector\Collector;

use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PHPStan\Type\Type;
use Rector\ChangesReporting\Collector\RectorChangeCollector;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PostRector\Contract\Collector\NodeCollectorInterface;
use Rector\PostRector\ValueObject\PropertyMetadata;

final class PropertyToAddCollector implements NodeCollectorInterface
{
/**
* @var array<string, array<string, ClassConst>>
*/
private array $constantsByClass = [];

/**
* @var array<string, PropertyMetadata[]>
*/
Expand All @@ -30,7 +23,6 @@ final class PropertyToAddCollector implements NodeCollectorInterface
private array $propertiesWithoutConstructorByClass = [];

public function __construct(
private readonly NodeNameResolver $nodeNameResolver,
private readonly RectorChangeCollector $rectorChangeCollector
) {
}
Expand All @@ -41,11 +33,7 @@ public function isActive(): bool
return true;
}

if ($this->propertiesWithoutConstructorByClass !== []) {
return true;
}

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

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

public function addConstantToClass(Class_ $class, ClassConst $classConst): void
{
$constantName = $this->nodeNameResolver->getName($classConst);
$this->constantsByClass[spl_object_hash($class)][$constantName] = $classConst;

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

/**
* @api
*/
Expand All @@ -77,15 +57,6 @@ public function addPropertyWithoutConstructorToClass(
$this->rectorChangeCollector->notifyNodeFileInfo($class);
}

/**
* @return ClassConst[]
*/
public function getConstantsByClass(Class_ $class): array
{
$classHash = spl_object_hash($class);
return $this->constantsByClass[$classHash] ?? [];
}

/**
* @return PropertyMetadata[]
*/
Expand Down
10 changes: 0 additions & 10 deletions packages/PostRector/Rector/PropertyAddingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public function enterNode(Node $node): ?Node
return null;
}

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

Expand Down Expand Up @@ -80,15 +79,6 @@ public function run()
);
}

private function addConstants(Class_ $class): void
{
$constants = $this->propertyToAddCollector->getConstantsByClass($class);

foreach ($constants as $constantName => $nodeConst) {
$this->classInsertManipulator->addConstantToClass($class, $constantName, $nodeConst);
}
}

private function addProperties(Class_ $class): void
{
$propertiesMetadatas = $this->propertyToAddCollector->getPropertiesByClass($class);
Expand Down

This file was deleted.

0 comments on commit ce12406

Please sign in to comment.