Skip to content

Commit 11b6e02

Browse files
authored
feat(php86): add min/max to clamp rector (#7942)
* chore: add php 8.6 support * feat: add AbstractRector::getNativeType() * feat: add min/max to clamp rector
1 parent febd1cc commit 11b6e02

File tree

19 files changed

+340
-2
lines changed

19 files changed

+340
-2
lines changed

config/set/level/up-to-php86.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Set\ValueObject\LevelSetList;
7+
use Rector\Set\ValueObject\SetList;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->sets([SetList::PHP_86, LevelSetList::UP_TO_PHP_85]);
11+
};

config/set/php86.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\Php86\Rector\FuncCall\MinMaxToClampRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rules([MinMaxToClampRector::class]);
10+
};

phpstan.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ parameters:
152152
- '#Callable callable\(PHPStan\\Type\\Type\)\: PHPStan\\Type\\Type invoked with 2 parameters, 1 required#'
153153

154154
# known value
155-
- '#Method (.*?) should return 50200\|50300\|50400\|50500\|50600\|70000\|70100\|70200\|70300\|70400\|80000\|80100\|80200\|80300\|80400\|80500\|100000 but returns int#'
155+
- '#Method (.*?) should return 50200\|50300\|50400\|50500\|50600\|70000\|70100\|70200\|70300\|70400\|80000\|80100\|80200\|80300\|80400\|80500\|80600\|100000 but returns int#'
156156

157157
-
158158
message: '#Function "class_exists\(\)" cannot be used/left in the code#'
@@ -325,7 +325,7 @@ parameters:
325325
message: '#PHPDoc tag @var with type int is not subtype of native type array\|PHPStan\\PhpDocParser\\Ast\\ConstExpr\\ConstExprNode\|Rector\\BetterPhpDocParser\\PhpDoc\\DoctrineAnnotationTagValueNode\|Rector\\BetterPhpDocParser\\PhpDoc\\StringNode\|Rector\\BetterPhpDocParser\\ValueObject\\PhpDoc\\DoctrineAnnotation\\CurlyListNode\|string#'
326326
path: src/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser.php
327327

328-
- '#Parameter \#1 \$phpVersion of method Rector\\Config\\RectorConfig\:\:phpVersion\(\) expects 50200\|50300\|50400\|50500\|50600\|70000\|70100\|70200\|70300\|70400\|80000\|80100\|80200\|80300\|80400\|80500\|100000, 79999 given#'
328+
- '#Parameter \#1 \$phpVersion of method Rector\\Config\\RectorConfig\:\:phpVersion\(\) expects 50200\|50300\|50400\|50500\|50600\|70000\|70100\|70200\|70300\|70400\|80000\|80100\|80200\|80300\|80400\|80500\|80600\|100000, 79999 given#'
329329

330330
# node vs stmts mix
331331
- '#expects array<PhpParser\\Node\\Stmt>, array<PhpParser\\Node> given#'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php86\Rector\FuncCall\MinMaxToClampRector\Fixture;
4+
5+
$value = random_int(0, 100);
6+
7+
max(0, min(100, $value));
8+
max(0, min($value, 100));
9+
max(min($value, 100), 0);
10+
max(min(100, $value), 0);
11+
12+
min(100, max(0, $value));
13+
min(100, max($value, 0));
14+
min(max($value, 0), 100);
15+
min(max(0, $value), 100);
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\Php86\Rector\FuncCall\MinMaxToClampRector\Fixture;
22+
23+
$value = random_int(0, 100);
24+
25+
clamp($value, 0, 100);
26+
clamp($value, 0, 100);
27+
clamp($value, 0, 100);
28+
clamp($value, 0, 100);
29+
30+
clamp($value, 0, 100);
31+
clamp($value, 0, 100);
32+
clamp($value, 0, 100);
33+
clamp($value, 0, 100);
34+
35+
?>
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\Php86\Rector\FuncCall\MinMaxToClampRector\Fixture;
4+
5+
$limit = 100;
6+
7+
fn (int $value) => max(0, min(100, $value));
8+
fn (int $value) => max(0, min($limit, $value));
9+
fn (int $value) => max(0, min($value, $limit));
10+
fn (int $value) => max(PHP_INT_MIN, min(PHP_INT_MAX, $value));
11+
fn (string $value) => max('a', min('z', $value));
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\Php86\Rector\FuncCall\MinMaxToClampRector\Fixture;
18+
19+
$limit = 100;
20+
21+
fn (int $value) => clamp($value, 0, 100);
22+
fn (int $value) => clamp($value, 0, $limit);
23+
fn (int $value) => clamp($value, 0, $limit);
24+
fn (int $value) => clamp($value, PHP_INT_MIN, PHP_INT_MAX);
25+
fn (string $value) => clamp($value, 'a', 'z');
26+
27+
?>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php86\Rector\FuncCall\MinMaxToClampRector\Fixture;
4+
5+
fn (int $value, int $min) => max($min, min(100, $value));
6+
fn (int $value, int $max) => min($max, max(0, $value));
7+
8+
?>
9+
-----
10+
<?php
11+
12+
namespace Rector\Tests\Php86\Rector\FuncCall\MinMaxToClampRector\Fixture;
13+
14+
fn (int $value, int $min) => clamp($value, $min, 100);
15+
fn (int $value, int $max) => clamp($value, 0, $max);
16+
17+
?>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php86\Rector\FuncCall\MinMaxToClampRector\Fixture;
4+
5+
fn (int $value, int $min, int $max) => max($min, min($value, $max));
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php86\Rector\FuncCall\MinMaxToClampRector\Fixture;
4+
5+
fn (int $value, int $min, int $max) => max($min, max($max, $value));
6+
fn (int $value, int $min, int $max) => min($min, min($max, $value));
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\Php86\Rector\FuncCall\MinMaxToClampRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class MinMaxToClampRectorTest 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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Php86\Rector\FuncCall\MinMaxToClampRector;
7+
use Rector\ValueObject\PhpVersion;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->rule(MinMaxToClampRector::class);
11+
12+
$rectorConfig->phpVersion(PhpVersion::PHP_86);
13+
};

0 commit comments

Comments
 (0)