Skip to content

Commit

Permalink
add class const fetch renames
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 27, 2021
1 parent b305983 commit ed316d4
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 6 deletions.
74 changes: 74 additions & 0 deletions config/set/symfony51.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

declare(strict_types=1);

use PHPStan\Type\ObjectType;
use Rector\Renaming\Rector\ClassConstFetch\RenameClassConstantRector;
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Renaming\ValueObject\RenameClassConstant;
use Rector\Transform\Rector\New_\NewArgToMethodCallRector;
use Rector\Transform\Rector\StaticCall\StaticCallToNewRector;
use Rector\Transform\ValueObject\NewArgToMethodCall;
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector;
use Rector\TypeDeclaration\ValueObject\AddParamTypeDeclaration;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

Expand All @@ -20,6 +26,9 @@
->call('configure', [[
RenameClassRector::OLD_TO_NEW_CLASSES => [
'Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy' => 'Symfony\Component\EventDispatcher\EventDispatcherInterface',
'Symfony\Component\Form\Extension\Validator\Util\ServerParams' => [
'Symfony\Component\Form\Util\ServerParams',
],
],
]]);

Expand Down Expand Up @@ -59,4 +68,69 @@
new NewArgToMethodCall('Symfony\Component\Dotenv\Dotenv', true, 'usePutenv'),
]),
]]);

$services->set(RenameClassConstantRector::class)
->call('configure', [[
RenameClassConstantRector::CLASS_CONSTANT_RENAME => ValueObjectInliner::inline([
new RenameClassConstant(
'Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer',
'ROUND_FLOOR',
'NumberFormatter::ROUND_FLOOR'
),
new RenameClassConstant(
'Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer',
'ROUND_DOWN',
'NumberFormatter::ROUND_DOWN'
),
new RenameClassConstant(
'Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer',
'ROUND_HALF_DOWN',
'NumberFormatter::ROUND_HALFDOWN'
),
new RenameClassConstant(
'Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer',
'ROUND_HALF_EVEN',
'NumberFormatter::ROUND_HALFEVEN'
),
new RenameClassConstant(
'Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer',
'ROUND_HALFUP',
'NumberFormatter::ROUND_HALFUP'
),
new RenameClassConstant(
'Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer',
'ROUND_UP',
'NumberFormatter::ROUND_UP'
),
new RenameClassConstant(
'Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer',
'ROUND_CEILING',
'NumberFormatter::ROUND_CEILING'
),
]),
]]);

$services->set(AddParamTypeDeclarationRector::class)
->call('configure', [[
// see https://github.com/symfony/symfony/pull/36943
AddParamTypeDeclarationRector::PARAMETER_TYPEHINTS => ValueObjectInliner::inline([
new AddParamTypeDeclaration(
'Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait',
'configureRoutes',
0,
new ObjectType('Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator'),
),
]),
]]);

$services->set(StaticCallToNewRector::class)
->call('configure', [[
// see https://github.com/symfony/symfony/pull/36943
StaticCallToNewRector::METHODS_BY_TYPES => [
'Symfony\Component\HttpFoundation\Response' => 'create',
'Symfony\Component\HttpFoundation\JsonResponse' => 'create',
'Symfony\Component\HttpFoundation\RedirectResponse' => 'create',
'Symfony\Component\HttpFoundation\StreamedResponse' => 'create',
],
]]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function createConfigurationConstants(array $ruleConfiguration): array
$classConsts = [];

foreach (array_keys($ruleConfiguration) as $constantName) {
$constantName = strtoupper($constantName);
$constantValue = strtolower($constantName);
$classConst = $this->nodeFactory->createPublicClassConst($constantName, $constantValue);
$classConsts[] = $classConst;
Expand Down
2 changes: 2 additions & 0 deletions packages/rector-generator/src/TemplateVariablesFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ private function createRuleConfiguration(string $rectorClass, array $configurati
{
$arrayItems = [];
foreach ($configuration as $constantName => $variableConfiguration) {
$constantName = strtoupper($constantName);

if ($rectorClass === self::SELF) {
$class = new Name(self::SELF);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@ final class RenameClassConstantsUseToStringsRector extends AbstractRector implem

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"',
[
self::CLASS_CONST_FETCHES_TO_VALUES => [
new ClassConstFetchToValue('Nette\Configurator', 'DEVELOPMENT', 'development'),
new ClassConstFetchToValue('Nette\Configurator', 'PRODUCTION', 'production'),
],
]
$configuration
),
]);
}
Expand Down
103 changes: 103 additions & 0 deletions rules/transform/src/Rector/StaticCall/StaticCallToNewRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

namespace Rector\Transform\Rector\StaticCall;

use PhpParser\Node;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://github.com/symfony/symfony/pull/35308
*
* @see \Rector\Transform\Tests\Rector\StaticCall\StaticCallToNewRector\StaticCallToNewRectorTest
*/
final class StaticCallToNewRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string
*/
public const METHODS_BY_TYPES = 'methods_by_types';

/**
* @var array<string, string>
*/
private $methodsByTypes = [];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change static call to new instance', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$dotenv = JsonResponse::create(true);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$dotenv = new JsonResponse();
}
}
CODE_SAMPLE
,
[
self::METHODS_BY_TYPES => [
'JsonResponse' => 'create',
],
]
),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Node\Expr\StaticCall::class];
}

/**
* @param Node\Expr\StaticCall $node
*/
public function refactor(Node $node): ?Node
{
foreach ($this->methodsByTypes as $type => $method) {
if (! $this->isName($node->class, $type)) {
continue;
}

if (! $this->isName($node->name, $method)) {
continue;
}

$class = $this->getName($node->class);
if ($class === null) {
continue;
}

return new Node\Expr\New_(new Node\Name\FullyQualified($class));
}

return $node;
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
$this->methodsByTypes = $configuration[self::METHODS_BY_TYPES] ?? [];
}
}
3 changes: 3 additions & 0 deletions rules/transform/src/ValueObject/NewArgToMethodCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public function getType(): string
return $this->type;
}

/**
* @return mixed
*/
public function getValue()
{
return $this->value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Transform\Tests\Rector\StaticCall\StaticCallToNewRector\Fixture;

use Rector\Transform\Tests\Rector\StaticCall\StaticCallToNewRector\Source\SomeJsonResponse;

class SomeClass
{
public function run()
{
$dotenv = SomeJsonResponse::create(true);
}
}

?>
-----
<?php

namespace Rector\Transform\Tests\Rector\StaticCall\StaticCallToNewRector\Fixture;

use Rector\Transform\Tests\Rector\StaticCall\StaticCallToNewRector\Source\SomeJsonResponse;

class SomeClass
{
public function run()
{
$dotenv = new \Rector\Transform\Tests\Rector\StaticCall\StaticCallToNewRector\Source\SomeJsonResponse();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Transform\Tests\Rector\StaticCall\StaticCallToNewRector\Source;

final class SomeJsonResponse
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Rector\Transform\Tests\Rector\StaticCall\StaticCallToNewRector;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Transform\Tests\Rector\StaticCall\StaticCallToNewRector\Source\SomeJsonResponse;

final class StaticCallToNewRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(\Symplify\SmartFileSystem\SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

public function provideData(): \Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

/**
* @return mixed[]
*/
protected function getRectorsWithConfiguration(): array
{
return [
\Rector\Transform\Rector\StaticCall\StaticCallToNewRector::class =>
[
\Rector\Transform\Rector\StaticCall\StaticCallToNewRector::METHODS_BY_TYPES => [
SomeJsonResponse::class => 'create',
],
],
];
}
}

0 comments on commit ed316d4

Please sign in to comment.