Skip to content

Commit f02926e

Browse files
authored
feat(php86): add DowngradeClampRector (#364)
1 parent 8c5e9e1 commit f02926e

11 files changed

Lines changed: 180 additions & 1 deletion

File tree

config/set/downgrade-php86.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DowngradePhp86\Rector\FuncCall\DowngradeClampRector;
7+
use Rector\ValueObject\PhpVersion;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->phpVersion(PhpVersion::PHP_85);
11+
$rectorConfig->rule(DowngradeClampRector::class);
12+
};

config/set/level/down-to-php84.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6+
use Rector\Set\ValueObject\DowngradeLevelSetList;
67
use Rector\Set\ValueObject\DowngradeSetList;
78

89
return static function (RectorConfig $rectorConfig): void {
9-
$rectorConfig->sets([DowngradeSetList::PHP_85]);
10+
$rectorConfig->sets([DowngradeLevelSetList::DOWN_TO_PHP_85, DowngradeSetList::PHP_85]);
1011
};

config/set/level/down-to-php85.php

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\Set\ValueObject\DowngradeSetList;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->sets([DowngradeSetList::PHP_86]);
10+
};
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\DowngradePhp86\Rector\FuncCall\DowngradeClampRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DowngradeClampRectorTest 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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp86\Rector\FuncCall\DowngradeClampRector\Fixture;
6+
7+
clamp($value, $min, $max);
8+
9+
?>
10+
-----
11+
<?php
12+
13+
declare(strict_types=1);
14+
15+
namespace Rector\Tests\DowngradePhp86\Rector\FuncCall\DowngradeClampRector\Fixture;
16+
17+
max($min, min($max, $value));
18+
19+
?>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp86\Rector\FuncCall\DowngradeClampRector\Fixture;
6+
7+
clamp(...);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp86\Rector\FuncCall\DowngradeClampRector\Fixture;
6+
7+
clamp(max: $max, value: $value, min: $min);
8+
9+
?>
10+
-----
11+
<?php
12+
13+
declare(strict_types=1);
14+
15+
namespace Rector\Tests\DowngradePhp86\Rector\FuncCall\DowngradeClampRector\Fixture;
16+
17+
max($min, min($max, $value));
18+
19+
?>
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\DowngradePhp86\Rector\FuncCall\DowngradeClampRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(DowngradeClampRector::class);
10+
};
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DowngradePhp86\Rector\FuncCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
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 https://wiki.php.net/rfc/clamp_v2
16+
* @see \Rector\Tests\DowngradePhp86\Rector\FuncCall\DowngradeClampRector\DowngradeClampRectorTest
17+
*/
18+
final class DowngradeClampRector extends AbstractRector
19+
{
20+
public function getRuleDefinition(): RuleDefinition
21+
{
22+
return new RuleDefinition(
23+
'Replace clamp() with min()/max().',
24+
[
25+
new CodeSample(
26+
<<<'CODE_SAMPLE'
27+
clamp($value, $min, $max);
28+
CODE_SAMPLE
29+
,
30+
<<<'CODE_SAMPLE'
31+
max($min, min($max, $value));
32+
CODE_SAMPLE
33+
),
34+
]
35+
);
36+
}
37+
38+
public function getNodeTypes(): array
39+
{
40+
return [FuncCall::class];
41+
}
42+
43+
/**
44+
* @param FuncCall $node
45+
*/
46+
public function refactor(Node $node): ?Node
47+
{
48+
if (! $this->isName($node, 'clamp')) {
49+
return null;
50+
}
51+
52+
if ($node->isFirstClassCallable()) {
53+
return null;
54+
}
55+
56+
$valueArg = $node->getArg('value', 0);
57+
$minArg = $node->getArg('min', 1);
58+
$maxArg = $node->getArg('max', 2);
59+
60+
if (! $valueArg instanceof Arg || ! $minArg instanceof Arg || ! $maxArg instanceof Arg) {
61+
return null;
62+
}
63+
64+
return $this->nodeFactory->createFuncCall('max', [
65+
$minArg->value,
66+
$this->nodeFactory->createFuncCall('min', [$maxArg->value, $valueArg->value]),
67+
]);
68+
}
69+
}

src/Set/ValueObject/DowngradeLevelSetList.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*/
1111
final class DowngradeLevelSetList
1212
{
13+
public const string DOWN_TO_PHP_85 = __DIR__ . '/../../../config/set/level/down-to-php85.php';
14+
1315
public const string DOWN_TO_PHP_84 = __DIR__ . '/../../../config/set/level/down-to-php84.php';
1416

1517
public const string DOWN_TO_PHP_83 = __DIR__ . '/../../../config/set/level/down-to-php83.php';

0 commit comments

Comments
 (0)