mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-28 23:10:51 +00:00
[Transform] Remove unused ClassConstFetchToValueRector (#1332)
This commit is contained in:
parent
00dd0eb4ca
commit
fa9b4ef615
|
@ -18,7 +18,7 @@
|
|||
|
||||
- [Composer](#composer) (6)
|
||||
|
||||
- [DeadCode](#deadcode) (49)
|
||||
- [DeadCode](#deadcode) (50)
|
||||
|
||||
- [DependencyInjection](#dependencyinjection) (2)
|
||||
|
||||
|
@ -98,7 +98,7 @@
|
|||
|
||||
- [Strict](#strict) (5)
|
||||
|
||||
- [Transform](#transform) (36)
|
||||
- [Transform](#transform) (35)
|
||||
|
||||
- [TypeDeclaration](#typedeclaration) (22)
|
||||
|
||||
|
@ -3104,6 +3104,21 @@ Remove empty constructor
|
|||
|
||||
<br>
|
||||
|
||||
### RemoveDeadContinueRector
|
||||
|
||||
Remove useless continue at the end of loops
|
||||
|
||||
- class: [`Rector\DeadCode\Rector\For_\RemoveDeadContinueRector`](../rules/DeadCode/Rector/For_/RemoveDeadContinueRector.php)
|
||||
|
||||
```diff
|
||||
while ($i < 10) {
|
||||
++$i;
|
||||
- continue;
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### RemoveDeadIfForeachForRector
|
||||
|
||||
Remove if, foreach and for that does not do anything
|
||||
|
@ -10451,41 +10466,6 @@ Change singleton class to normal class that can be registered as a service
|
|||
|
||||
<br>
|
||||
|
||||
### ClassConstFetchToValueRector
|
||||
|
||||
Replaces constant by value
|
||||
|
||||
:wrench: **configure it!**
|
||||
|
||||
- class: [`Rector\Transform\Rector\ClassConstFetch\ClassConstFetchToValueRector`](../rules/Transform/Rector/ClassConstFetch/ClassConstFetchToValueRector.php)
|
||||
|
||||
```php
|
||||
use Rector\Transform\Rector\ClassConstFetch\ClassConstFetchToValueRector;
|
||||
use Rector\Transform\ValueObject\ClassConstFetchToValue;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
use Symplify\SymfonyPhpConfig\ValueObjectInliner;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
|
||||
$services->set(ClassConstFetchToValueRector::class)
|
||||
->call('configure', [[
|
||||
ClassConstFetchToValueRector::CLASS_CONST_FETCHES_TO_VALUES => ValueObjectInliner::inline([
|
||||
new ClassConstFetchToValue('Nette\Configurator', 'PRODUCTION', 'production'),
|
||||
]),
|
||||
]]);
|
||||
};
|
||||
```
|
||||
|
||||
↓
|
||||
|
||||
```diff
|
||||
-$value === Nette\Configurator::DEVELOPMENT
|
||||
+$value === "development"
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### DimFetchAssignToMethodCallRector
|
||||
|
||||
Change magic array access add to `$list[],` to explicit `$list->addMethod(...)`
|
||||
|
@ -11543,8 +11523,8 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
{
|
||||
public function run()
|
||||
{
|
||||
- $dotenv = JsonResponse::create(true);
|
||||
+ $dotenv = new JsonResponse();
|
||||
- $dotenv = JsonResponse::create(['foo' => 'bar'], Response::HTTP_OK);
|
||||
+ $dotenv = new JsonResponse(['foo' => 'bar'], Response::HTTP_OK);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\ClassConstFetch\ClassConstFetchToValueRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class ClassConstFetchToValueRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\ClassConstFetch\ClassConstFetchToValueRector\Fixture;
|
||||
|
||||
use Rector\Tests\Transform\Rector\ClassConstFetch\ClassConstFetchToValueRector\Source\OldClassWithConstants;
|
||||
|
||||
class ClassWithExternalConstant
|
||||
{
|
||||
public function getValue()
|
||||
{
|
||||
return OldClassWithConstants::DEVELOPMENT;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\ClassConstFetch\ClassConstFetchToValueRector\Fixture;
|
||||
|
||||
use Rector\Tests\Transform\Rector\ClassConstFetch\ClassConstFetchToValueRector\Source\OldClassWithConstants;
|
||||
|
||||
class ClassWithExternalConstant
|
||||
{
|
||||
public function getValue()
|
||||
{
|
||||
return 'development';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,18 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\ClassConstFetch\ClassConstFetchToValueRector\Source;
|
||||
|
||||
final class OldClassWithConstants
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const DEVELOPMENT = 'development';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const PRODUCTION = 'production';
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Tests\Transform\Rector\ClassConstFetch\ClassConstFetchToValueRector\Source\OldClassWithConstants;
|
||||
use Rector\Transform\Rector\ClassConstFetch\ClassConstFetchToValueRector;
|
||||
use Rector\Transform\ValueObject\ClassConstFetchToValue;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
use Symplify\SymfonyPhpConfig\ValueObjectInliner;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(ClassConstFetchToValueRector::class)
|
||||
->call('configure', [[
|
||||
ClassConstFetchToValueRector::CLASS_CONST_FETCHES_TO_VALUES => ValueObjectInliner::inline([
|
||||
new ClassConstFetchToValue(OldClassWithConstants::class, 'DEVELOPMENT', 'development'),
|
||||
new ClassConstFetchToValue(OldClassWithConstants::class, 'PRODUCTION', 'production'),
|
||||
]),
|
||||
]]);
|
||||
};
|
|
@ -1,88 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Transform\Rector\ClassConstFetch;
|
||||
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Transform\ValueObject\ClassConstFetchToValue;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\Transform\Rector\ClassConstFetch\ClassConstFetchToValueRector\ClassConstFetchToValueRectorTest
|
||||
*/
|
||||
final class ClassConstFetchToValueRector extends AbstractRector implements ConfigurableRectorInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const CLASS_CONST_FETCHES_TO_VALUES = 'old_constants_to_new_valuesByType';
|
||||
|
||||
/**
|
||||
* @var ClassConstFetchToValue[]
|
||||
*/
|
||||
private array $classConstFetchesToValues = [];
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
$configuration = [
|
||||
self::CLASS_CONST_FETCHES_TO_VALUES => [
|
||||
new ClassConstFetchToValue('Nette\Configurator', 'DEVELOPMENT', 'development'),
|
||||
new ClassConstFetchToValue('Nette\Configurator', 'PRODUCTION', 'production'),
|
||||
],
|
||||
];
|
||||
|
||||
return new RuleDefinition('Replaces constant by value', [
|
||||
new ConfiguredCodeSample(
|
||||
'$value === Nette\Configurator::DEVELOPMENT',
|
||||
'$value === "development"',
|
||||
$configuration
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [ClassConstFetch::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassConstFetch $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
foreach ($this->classConstFetchesToValues as $classConstFetchToValue) {
|
||||
if (! $this->isObjectType($node->class, $classConstFetchToValue->getObjectType())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! $this->isName($node->name, $classConstFetchToValue->getConstant())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return BuilderHelpers::normalizeValue($classConstFetchToValue->getValue());
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $configuration
|
||||
*/
|
||||
public function configure(array $configuration): void
|
||||
{
|
||||
$classConstFetchesToValues = $configuration[self::CLASS_CONST_FETCHES_TO_VALUES] ?? $configuration;
|
||||
Assert::allIsAOf($classConstFetchesToValues, ClassConstFetchToValue::class);
|
||||
|
||||
$this->classConstFetchesToValues = $classConstFetchesToValues;
|
||||
}
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Transform\ValueObject;
|
||||
|
||||
use PHPStan\Type\ObjectType;
|
||||
|
||||
final class ClassConstFetchToValue
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __construct(
|
||||
private string $class,
|
||||
private string $constant,
|
||||
private $value
|
||||
) {
|
||||
}
|
||||
|
||||
public function getObjectType(): ObjectType
|
||||
{
|
||||
return new ObjectType($this->class);
|
||||
}
|
||||
|
||||
public function getConstant(): string
|
||||
{
|
||||
return $this->constant;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValue()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user