Skip to content

Commit faac72c

Browse files
authored
[code-quality] add filter_var() support to ParameterBagTypedGetMethodCallRector (#836)
* add filter var fixture * add filter_var() support to ParameterBagTypedGetMethodCallRector
1 parent f22802f commit faac72c

4 files changed

Lines changed: 96 additions & 2 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 = filter_var(
12+
$request->query->get('debug', false),
13+
FILTER_VALIDATE_BOOLEAN
14+
);
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector\Fixture;
23+
24+
use Symfony\Component\HttpFoundation\Request;
25+
26+
final class GetOnBoolean
27+
{
28+
public function run(Request $request)
29+
{
30+
$debug = $request->query->getBoolean('debug');
31+
}
32+
}
33+
34+
?>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector\Fixture;
4+
5+
use Symfony\Component\HttpFoundation\Request;
6+
7+
final class SkipNoInnerType
8+
{
9+
public function run(Request $request)
10+
{
11+
$debug = filter_var(
12+
$request->query->get('debug', true),
13+
FILTER_VALIDATE_BOOLEAN
14+
);
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector\Fixture;
4+
5+
use Symfony\Component\HttpFoundation\Request;
6+
7+
final class SkipSoleExprFilterVar
8+
{
9+
public function run(Request $request)
10+
{
11+
$debug = filter_var(
12+
$request->query->get('debug', false),
13+
);
14+
}
15+
}

rules/CodeQuality/Rector/MethodCall/ParameterBagTypedGetMethodCallRector.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr\Cast\Bool_;
9+
use PhpParser\Node\Expr\FuncCall;
910
use PhpParser\Node\Expr\MethodCall;
1011
use PhpParser\Node\Identifier;
1112
use PHPStan\Type\ObjectType;
@@ -67,14 +68,18 @@ public function run(Request $request)
6768
*/
6869
public function getNodeTypes(): array
6970
{
70-
return [MethodCall::class, Bool_::class];
71+
return [MethodCall::class, Bool_::class, FuncCall::class];
7172
}
7273

7374
/**
74-
* @param MethodCall|Bool_ $node
75+
* @param MethodCall|Bool_|FuncCall $node
7576
*/
7677
public function refactor(Node $node): ?Node
7778
{
79+
if ($node instanceof FuncCall && $this->isName($node->name, 'filter_var')) {
80+
return $this->refactorFilterVarFuncCall($node);
81+
}
82+
7883
if ($node instanceof Bool_) {
7984
if (! $node->expr instanceof MethodCall) {
8085
return null;
@@ -126,4 +131,28 @@ private function refactorMethodCall(MethodCall $methodCall): ?MethodCall
126131

127132
return null;
128133
}
134+
135+
private function refactorFilterVarFuncCall(FuncCall $funcCall): ?MethodCall
136+
{
137+
if ($funcCall->isFirstClassCallable()) {
138+
return null;
139+
}
140+
141+
// needs at least 2 args
142+
if (count($funcCall->getArgs()) < 2) {
143+
return null;
144+
}
145+
146+
$flagArg = $funcCall->getArgs()[1];
147+
if (! $this->valueResolver->isValue($flagArg->value, 'FILTER_VALIDATE_BOOLEAN')) {
148+
return null;
149+
}
150+
151+
$exprArg = $funcCall->getArgs()[0];
152+
if (!$exprArg->value instanceof MethodCall) {
153+
return null;
154+
}
155+
156+
return $this->refactorMethodCall($exprArg->value);
157+
}
129158
}

0 commit comments

Comments
 (0)