Skip to content

Commit 5226e84

Browse files
[assert] Kick of experimental rule, AddAssertArrayFromClassMethodDocblockRector (#7206)
* add ... * [assert] Kick of experimental rule, AddAssertArrayFromClassMethodDocblockRector * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent b48bd00 commit 5226e84

10 files changed

Lines changed: 394 additions & 2 deletions

File tree

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\Assert\Rector\ClassMethod\AddAssertArrayFromClassMethodDocblockRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class AddAssertArrayFromClassMethodDocblockRectorTest 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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Rector\Tests\Assert\Rector\ClassMethod\AddAssertArrayFromClassMethodDocblockRector\Fixture;
4+
5+
final class MultipleParameters
6+
{
7+
/**
8+
* @param array<string, string> $items
9+
* @param string[] $names
10+
*/
11+
public function run(array $items, array $names)
12+
{
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\Assert\Rector\ClassMethod\AddAssertArrayFromClassMethodDocblockRector\Fixture;
21+
22+
final class MultipleParameters
23+
{
24+
/**
25+
* @param array<string, string> $items
26+
* @param string[] $names
27+
*/
28+
public function run(array $items, array $names)
29+
{
30+
\Webmozart\Assert\Assert::allString($items);
31+
\Webmozart\Assert\Assert::allString(array_keys($items));
32+
\Webmozart\Assert\Assert::allString($names);
33+
}
34+
}
35+
36+
?>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Rector\Tests\Assert\Rector\ClassMethod\AddAssertArrayFromClassMethodDocblockRector\Fixture;
4+
5+
final class SimpleArray
6+
{
7+
/**
8+
* @param int[] $items
9+
*/
10+
public function run(array $items)
11+
{
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Tests\Assert\Rector\ClassMethod\AddAssertArrayFromClassMethodDocblockRector\Fixture;
20+
21+
final class SimpleArray
22+
{
23+
/**
24+
* @param int[] $items
25+
*/
26+
public function run(array $items)
27+
{
28+
\Webmozart\Assert\Assert::allInteger($items);
29+
}
30+
}
31+
32+
?>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\Assert\Rector\ClassMethod\AddAssertArrayFromClassMethodDocblockRector\Fixture;
4+
5+
final class SkipAlreadySet
6+
{
7+
/**
8+
* @param int[] $items
9+
*/
10+
public function run(array $items)
11+
{
12+
\Webmozart\Assert\Assert::allInteger($items);
13+
}
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\Assert\Rector\ClassMethod\AddAssertArrayFromClassMethodDocblockRector\Fixture;
4+
5+
final class SkipNoArray
6+
{
7+
/**
8+
* @param int[] $items
9+
*/
10+
public function run($items)
11+
{
12+
}
13+
}
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\Assert\Rector\ClassMethod\AddAssertArrayFromClassMethodDocblockRector;
6+
use Rector\Config\RectorConfig;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(AddAssertArrayFromClassMethodDocblockRector::class);
10+
};
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+
namespace Rector\Assert\Enum;
6+
7+
final class AssertClassName
8+
{
9+
/**
10+
* @var string
11+
*/
12+
public const ASSERT = 'Webmozart\Assert\Assert';
13+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Assert\NodeAnalyzer;
6+
7+
use PhpParser\Node\Expr\StaticCall;
8+
use PhpParser\Node\Name;
9+
use PhpParser\Node\Stmt\ClassMethod;
10+
use PhpParser\Node\Stmt\Expression;
11+
use PhpParser\PrettyPrinter\Standard;
12+
use Rector\Assert\Enum\AssertClassName;
13+
14+
final class ExistingAssertStaticCallResolver
15+
{
16+
/**
17+
* @return string[]
18+
*/
19+
public function resolve(ClassMethod $classMethod): array
20+
{
21+
if ($classMethod->stmts === null) {
22+
return [];
23+
}
24+
25+
$existingAssertCallHashes = [];
26+
$standard = new Standard();
27+
28+
foreach ($classMethod->stmts as $currentStmt) {
29+
if (! $currentStmt instanceof Expression) {
30+
continue;
31+
}
32+
33+
if (! $currentStmt->expr instanceof StaticCall) {
34+
continue;
35+
}
36+
37+
$staticCall = $currentStmt->expr;
38+
if (! $staticCall->class instanceof Name) {
39+
continue;
40+
}
41+
42+
if ($staticCall->class->toString() !== AssertClassName::ASSERT) {
43+
continue;
44+
}
45+
46+
$existingAssertCallHashes[] = $standard->prettyPrintExpr($staticCall);
47+
}
48+
49+
return $existingAssertCallHashes;
50+
}
51+
}

0 commit comments

Comments
 (0)