Skip to content

Commit 3caf998

Browse files
[dead-code] Add RemoveFilterVarOnExactTypeRector (#6792)
* [dead-code] Add RemoveFilterVarOnExactTypeRector * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 9d34f67 commit 3caf998

File tree

10 files changed

+231
-3
lines changed

10 files changed

+231
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FuncCall\RemoveFilterVarOnExactTypeRector\Fixture;
4+
5+
final class RemoveFilterVarOnConstantFetch
6+
{
7+
private const NUMBER = 123;
8+
9+
public function run()
10+
{
11+
return filter_var(self::NUMBER, FILTER_VALIDATE_INT);
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Tests\DeadCode\Rector\FuncCall\RemoveFilterVarOnExactTypeRector\Fixture;
20+
21+
final class RemoveFilterVarOnConstantFetch
22+
{
23+
private const NUMBER = 123;
24+
25+
public function run()
26+
{
27+
return self::NUMBER;
28+
}
29+
}
30+
31+
?>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FuncCall\RemoveFilterVarOnExactTypeRector\Fixture;
4+
5+
class RemoveFilterVarOnExactType
6+
{
7+
public function run(int $number)
8+
{
9+
return filter_var($number, FILTER_VALIDATE_INT);
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DeadCode\Rector\FuncCall\RemoveFilterVarOnExactTypeRector\Fixture;
18+
19+
class RemoveFilterVarOnExactType
20+
{
21+
public function run(int $number)
22+
{
23+
return $number;
24+
}
25+
}
26+
27+
?>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FuncCall\RemoveFilterVarOnExactTypeRector\Fixture;
4+
5+
final class RemoveFilterVarOnConstantFetch
6+
{
7+
public function run(int $value)
8+
{
9+
return filter_var($value, FILTER_VALIDATE_EMAIL);
10+
}
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FuncCall\RemoveFilterVarOnExactTypeRector\Fixture;
4+
5+
final class SkipValueToBeModified
6+
{
7+
public function run(int $value)
8+
{
9+
$options = [
10+
'options' => [
11+
'min_range' => 10,
12+
],
13+
'flags' => FILTER_FLAG_ALLOW_OCTAL,
14+
];
15+
16+
return filter_var($value, FILTER_VALIDATE_INT, $options);
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FuncCall\RemoveFilterVarOnExactTypeRector\Fixture;
4+
5+
final class SkipValueToBeModified
6+
{
7+
public function run()
8+
{
9+
return filter_var('0755', FILTER_VALIDATE_INT);
10+
}
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DeadCode\Rector\FuncCall\RemoveFilterVarOnExactTypeRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class RemoveFilterVarOnExactTypeRectorTest 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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DeadCode\Rector\FuncCall\RemoveFilterVarOnExactTypeRector;
7+
8+
return RectorConfig::configure()
9+
->withRules([RemoveFilterVarOnExactTypeRector::class]);

rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/config/configured_rule.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
use Rector\Php80\ValueObject\AnnotationToAttribute;
1010
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Annotation\OpenApi\Annotation\NestedPastAnnotation;
1111
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Annotation\OpenApi\PastAnnotation;
12+
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\NewName1;
13+
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\NewName2;
1214
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\OpenApi\Attribute\NestedFutureAttribute;
1315
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\OpenApi\FutureAttribute;
16+
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\SameName;
1417
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericAnnotation;
1518
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericSingleImplicitAnnotation;
16-
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\NewName1;
17-
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\NewName2;
18-
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\SameName;
1919

2020
return static function (RectorConfig $rectorConfig): void {
2121
$rectorConfig->ruleWithConfiguration(AnnotationToAttributeRector::class, [
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DeadCode\Rector\FuncCall;
6+
7+
use PhpParser\Node\Expr\ConstFetch;
8+
use PhpParser\Node;
9+
use PhpParser\Node\Expr\FuncCall;
10+
use Rector\Rector\AbstractRector;
11+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
12+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
13+
14+
/**
15+
* @see \Rector\Tests\DeadCode\Rector\FuncCall\RemoveFilterVarOnExactTypeRector\RemoveFilterVarOnExactTypeRectorTest
16+
*/
17+
final class RemoveFilterVarOnExactTypeRector extends AbstractRector
18+
{
19+
public function getRuleDefinition(): RuleDefinition
20+
{
21+
return new RuleDefinition(
22+
'Removes filter_var() calls with exact type',
23+
[
24+
new CodeSample(
25+
<<<'CODE_SAMPLE'
26+
function (int $value) {
27+
$result = filter_var($value, FILTER_VALIDATE_INT);
28+
}
29+
CODE_SAMPLE
30+
,
31+
<<<'CODE_SAMPLE'
32+
function (int $value) {
33+
$result = $value;
34+
}
35+
CODE_SAMPLE
36+
),
37+
]
38+
);
39+
}
40+
41+
public function getNodeTypes(): array
42+
{
43+
return [FuncCall::class];
44+
}
45+
46+
/**
47+
* @param FuncCall $node
48+
*/
49+
public function refactor(Node $node): ?Node
50+
{
51+
if ($node->isFirstClassCallable()) {
52+
return null;
53+
}
54+
55+
if (! $this->isName($node, 'filter_var')) {
56+
return null;
57+
}
58+
59+
// we need exact 2nd arg to assess value type
60+
if (count($node->getArgs()) !== 2) {
61+
return null;
62+
}
63+
64+
$firstArgValue = $node->getArgs()[0]
65+
->value;
66+
$secondArgValue = $node->getArgs()[1]
67+
->value;
68+
69+
if (! $secondArgValue instanceof ConstFetch) {
70+
return null;
71+
}
72+
73+
$constantFilterName = $secondArgValue->name->toString();
74+
75+
$valueType = $this->getType($firstArgValue);
76+
77+
if ($constantFilterName === 'FILTER_VALIDATE_INT' && $valueType->isInteger()->yes()) {
78+
return $firstArgValue;
79+
}
80+
81+
if ($constantFilterName === 'FILTER_VALIDATE_FLOAT' && $valueType->isFloat()->yes()) {
82+
return $firstArgValue;
83+
}
84+
85+
if (in_array($constantFilterName, ['FILTER_VALIDATE_BOOLEAN', 'FILTER_VALIDATE_BOOL']) && $valueType->isBoolean()->yes()) {
86+
return $firstArgValue;
87+
}
88+
89+
return null;
90+
}
91+
}

src/Config/Level/DeadCodeLevel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Rector\DeadCode\Rector\For_\RemoveDeadIfForeachForRector;
3434
use Rector\DeadCode\Rector\For_\RemoveDeadLoopRector;
3535
use Rector\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector;
36+
use Rector\DeadCode\Rector\FuncCall\RemoveFilterVarOnExactTypeRector;
3637
use Rector\DeadCode\Rector\FunctionLike\RemoveDeadReturnRector;
3738
use Rector\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector;
3839
use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;
@@ -85,6 +86,7 @@ final class DeadCodeLevel
8586
RemoveNullPropertyInitializationRector::class,
8687
RemoveUselessReturnExprInConstructRector::class,
8788
ReplaceBlockToItsStmtsRector::class,
89+
RemoveFilterVarOnExactTypeRector::class,
8890

8991
RemoveTypedPropertyDeadInstanceOfRector::class,
9092
TernaryToBooleanOrFalseToBooleanAndRector::class,

0 commit comments

Comments
 (0)