Skip to content

Commit 3cc018d

Browse files
committed
[code-quality] Add ParameterBagTypedGetMethodCallRector
1 parent 2216b24 commit 3cc018d

9 files changed

Lines changed: 216 additions & 27 deletions

File tree

config/sets/symfony/symfony-code-quality.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Rector\Symfony\CodeQuality\Rector\ClassMethod\ResponseReturnTypeControllerActionRector;
1717
use Rector\Symfony\CodeQuality\Rector\MethodCall\AssertSameResponseCodeWithDebugContentsRector;
1818
use Rector\Symfony\CodeQuality\Rector\MethodCall\LiteralGetToRequestClassConstantRector;
19+
use Rector\Symfony\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector;
1920
use Rector\Symfony\CodeQuality\Rector\MethodCall\StringCastDebugResponseRector;
2021
use Rector\Symfony\Symfony26\Rector\MethodCall\RedirectToRouteRector;
2122

@@ -35,6 +36,7 @@
3536

3637
// request method
3738
RequestIsMainRector::class,
39+
ParameterBagTypedGetMethodCallRector::class,
3840

3941
// tests
4042
AssertSameResponseCodeWithDebugContentsRector::class,

rules-tests/CodeQuality/Rector/BinaryOp/RequestIsMainRector/Fixture/fixture.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ final class SomeController
3131
{
3232
public function index(Request $request)
3333
{
34-
return $request->isMainRequest();
34+
return $request->isMasterRequest();
3535
}
3636

3737
public function second(Request $request)
3838
{
39-
return $request->isMainRequest();
39+
return $request->isMasterRequest();
4040
}
4141
}
4242

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector\Fixture;
4+
5+
use Symfony\Component\HttpFoundation\Request;
6+
7+
final class GetOnBoolean
8+
{
9+
public function run(Request $request)
10+
{
11+
$debug = (bool) $request->query->get('debug', false);
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector\Fixture;
20+
21+
use Symfony\Component\HttpFoundation\Request;
22+
23+
final class GetOnBoolean
24+
{
25+
public function run(Request $request)
26+
{
27+
$debug = $request->query->getBoolean('debug');
28+
}
29+
}
30+
31+
?>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector\Fixture;
4+
5+
use Symfony\Component\HttpFoundation\Request;
6+
7+
final class SkipNonCasted
8+
{
9+
public function run(Request $request)
10+
{
11+
$debug = $request->query->get('debug', false);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class ParameterBagTypedGetMethodCallRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Symfony\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(ParameterBagTypedGetMethodCallRector::class);
10+
};
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Symfony\CodeQuality\Rector\MethodCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\Cast\Bool_;
9+
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Identifier;
11+
use PHPStan\Type\ObjectType;
12+
use Rector\PhpParser\Node\Value\ValueResolver;
13+
use Rector\Rector\AbstractRector;
14+
use Rector\Symfony\Enum\SymfonyClass;
15+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
16+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
17+
18+
/**
19+
* @see \Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector\ParameterBagTypedGetMethodCallRectorTest
20+
*/
21+
final class ParameterBagTypedGetMethodCallRector extends AbstractRector
22+
{
23+
public function __construct(
24+
private readonly ValueResolver $valueResolver
25+
) {
26+
}
27+
28+
public function getRuleDefinition(): RuleDefinition
29+
{
30+
return new RuleDefinition(
31+
'Make use of specific ParameterBag::get*() method with native return type declaration',
32+
[
33+
new CodeSample(
34+
<<<'CODE_SAMPLE'
35+
use Symfony\Component\HttpFoundation\Request;
36+
37+
class SomeClass
38+
{
39+
public function run(Request $request)
40+
{
41+
$debug = (bool) $request->query->get('debug', false);
42+
}
43+
}
44+
CODE_SAMPLE
45+
46+
,
47+
<<<'CODE_SAMPLE'
48+
use Symfony\Component\HttpFoundation\Request;
49+
50+
class SomeClass
51+
{
52+
public function run(Request $request)
53+
{
54+
$debug = $request->query->getBoolean('debug');
55+
}
56+
}
57+
58+
CODE_SAMPLE
59+
),
60+
61+
]
62+
);
63+
}
64+
65+
/**
66+
* @return array<class-string<Node>>
67+
*/
68+
public function getNodeTypes(): array
69+
{
70+
return [MethodCall::class, Bool_::class];
71+
}
72+
73+
/**
74+
* @param MethodCall|Bool_ $node
75+
*/
76+
public function refactor(Node $node): ?Node
77+
{
78+
if ($node instanceof Bool_) {
79+
if (! $node->expr instanceof MethodCall) {
80+
return null;
81+
}
82+
83+
return $this->refactorMethodCall($node->expr);
84+
}
85+
86+
return null;
87+
}
88+
89+
private function refactorMethodCall(MethodCall $methodCall): ?MethodCall
90+
{
91+
// default value must be defined
92+
if (count($methodCall->getArgs()) !== 2) {
93+
return null;
94+
}
95+
96+
if (! $this->isName($methodCall->name, 'get')) {
97+
return null;
98+
}
99+
100+
$callerType = $this->getType($methodCall->var);
101+
if (! $callerType instanceof ObjectType) {
102+
return null;
103+
}
104+
105+
if (! $callerType->isInstanceOf(SymfonyClass::PARAMETER_BAG)->yes()) {
106+
return null;
107+
}
108+
109+
// the getBoolean() method must exist
110+
if (! $callerType->hasMethod('getBoolean')->yes()) {
111+
return null;
112+
}
113+
114+
$defaultArg = $methodCall->getArgs()[1];
115+
116+
if ($this->valueResolver->isFalse($defaultArg->value)) {
117+
unset($methodCall->args[1]);
118+
$methodCall->name = new Identifier('getBoolean');
119+
120+
return $methodCall;
121+
}
122+
123+
return null;
124+
}
125+
}

src/Enum/SymfonyClass.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,9 @@ final class SymfonyClass
135135
* @var string
136136
*/
137137
public const CONTAINER_CONFIGURATOR = 'Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator';
138+
139+
/**
140+
* @var string
141+
*/
142+
public const PARAMETER_BAG = 'Symfony\Component\HttpFoundation\ParameterBag';
138143
}

stubs/Symfony/Component/HttpFoundation/Request.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)