Skip to content

Commit

Permalink
fixup! 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 ed316d4 commit a88902a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
13 changes: 7 additions & 6 deletions config/set/symfony51.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Rector\Transform\Rector\New_\NewArgToMethodCallRector;
use Rector\Transform\Rector\StaticCall\StaticCallToNewRector;
use Rector\Transform\ValueObject\NewArgToMethodCall;
use Rector\Transform\ValueObject\StaticCallToNew;
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector;
use Rector\TypeDeclaration\ValueObject\AddParamTypeDeclaration;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
Expand Down Expand Up @@ -126,11 +127,11 @@
$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',
],
StaticCallToNewRector::STATIC_CALLS_TO_NEWS => ValueObjectInliner::inline([
new StaticCallToNew('Symfony\Component\HttpFoundation\Response', 'create'),
new StaticCallToNew('Symfony\Component\HttpFoundation\JsonResponse', 'create'),
new StaticCallToNew('Symfony\Component\HttpFoundation\RedirectResponse', 'create'),
new StaticCallToNew('Symfony\Component\HttpFoundation\StreamedResponse', 'create'),
]),
]]);
};
25 changes: 13 additions & 12 deletions rules/transform/src/Rector/StaticCall/StaticCallToNewRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Rector\Transform\Rector\StaticCall;

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

Expand All @@ -20,12 +22,12 @@ final class StaticCallToNewRector extends AbstractRector implements Configurable
/**
* @var string
*/
public const METHODS_BY_TYPES = 'methods_by_types';
public const STATIC_CALLS_TO_NEWS = 'static_calls_to_news';

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

public function getRuleDefinition(): RuleDefinition
{
Expand All @@ -52,9 +54,7 @@ public function run()
CODE_SAMPLE
,
[
self::METHODS_BY_TYPES => [
'JsonResponse' => 'create',
],
self::STATIC_CALLS_TO_NEWS => [new StaticCallToNew('JsonResponse', 'create')],
]
),
]);
Expand All @@ -73,12 +73,12 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
foreach ($this->methodsByTypes as $type => $method) {
if (! $this->isName($node->class, $type)) {
foreach ($this->staticCallsToNews as $staticCallToNew) {
if (! $this->isName($node->class, $staticCallToNew->getClass())) {
continue;
}

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

Expand All @@ -87,17 +87,18 @@ public function refactor(Node $node): ?Node
continue;
}

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

return $node;
}

/**
* @param mixed[] $configuration
* @param array<string, StaticCallToNew[]> $configuration
*/
public function configure(array $configuration): void
{
$this->methodsByTypes = $configuration[self::METHODS_BY_TYPES] ?? [];
$staticCallsToNews = $configuration[self::STATIC_CALLS_TO_NEWS] ?? [];
$this->staticCallsToNews = $staticCallsToNews;
}
}
34 changes: 34 additions & 0 deletions rules/transform/src/ValueObject/StaticCallToNew.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Rector\Transform\ValueObject;

final class StaticCallToNew
{
/**
* @var string
*/
private $class;

/**
* @var string
*/
private $method;

public function __construct(string $class, string $method)
{
$this->class = $class;
$this->method = $method;
}

public function getClass(): string
{
return $this->class;
}

public function getMethod(): string
{
return $this->method;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function getRectorsWithConfiguration(): array
return [
\Rector\Transform\Rector\StaticCall\StaticCallToNewRector::class =>
[
\Rector\Transform\Rector\StaticCall\StaticCallToNewRector::METHODS_BY_TYPES => [
\Rector\Transform\Rector\StaticCall\StaticCallToNewRector::STATIC_CALLS_TO_NEWS => [
SomeJsonResponse::class => 'create',
],
],
Expand Down

0 comments on commit a88902a

Please sign in to comment.