|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Fixable; |
| 4 | + |
| 5 | +use PhpParser\Comment\Doc; |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Name\FullyQualified; |
| 8 | +use PhpParser\Node\Stmt\ClassMethod; |
| 9 | +use PhpParser\NodeFinder; |
| 10 | +use PhpParser\NodeTraverser; |
| 11 | +use PhpParser\NodeVisitor\CloningVisitor; |
| 12 | +use PhpParser\ParserFactory; |
| 13 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +class NewClassNamesVisitorTest extends TestCase |
| 17 | +{ |
| 18 | + |
| 19 | + private const CODE = <<<'PHP' |
| 20 | + <?php |
| 21 | +
|
| 22 | + namespace HasWork; |
| 23 | +
|
| 24 | + class Foo |
| 25 | + { |
| 26 | +
|
| 27 | + /** @return int */ |
| 28 | + #[\Override] |
| 29 | + public function doFoo(\Bar\Baz $baz): int |
| 30 | + { |
| 31 | + return strlen(''); |
| 32 | + } |
| 33 | +
|
| 34 | + } |
| 35 | + PHP; |
| 36 | + |
| 37 | + /** |
| 38 | + * @return iterable<string, array{callable(ClassMethod): Node, bool}> |
| 39 | + */ |
| 40 | + public static function dataHasNewNames(): iterable |
| 41 | + { |
| 42 | + yield 'unchanged' => [ |
| 43 | + static fn (ClassMethod $method): Node => $method, |
| 44 | + false, |
| 45 | + ]; |
| 46 | + |
| 47 | + yield 'removed attribute' => [ |
| 48 | + static function (ClassMethod $method): Node { |
| 49 | + $method->attrGroups = []; |
| 50 | + return $method; |
| 51 | + }, |
| 52 | + false, |
| 53 | + ]; |
| 54 | + |
| 55 | + yield 'added name' => [ |
| 56 | + static function (ClassMethod $method): Node { |
| 57 | + $method->params[] = new Node\Param(new Node\Expr\Variable('qux'), type: new FullyQualified('Qux\Quux')); |
| 58 | + return $method; |
| 59 | + }, |
| 60 | + true, |
| 61 | + ]; |
| 62 | + |
| 63 | + yield 'changed PHPDoc' => [ |
| 64 | + static function (ClassMethod $method): Node { |
| 65 | + $method->setDocComment(new Doc('/** @return positive-int */')); |
| 66 | + return $method; |
| 67 | + }, |
| 68 | + true, |
| 69 | + ]; |
| 70 | + |
| 71 | + yield 'new node with a PHPDoc' => [ |
| 72 | + static function (ClassMethod $method): Node { |
| 73 | + $newMethod = new ClassMethod('doBar'); |
| 74 | + $newMethod->setDocComment(new Doc('/** @return void */')); |
| 75 | + return $newMethod; |
| 76 | + }, |
| 77 | + true, |
| 78 | + ]; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @param callable(ClassMethod): Node $fix |
| 83 | + */ |
| 84 | + #[DataProvider('dataHasNewNames')] |
| 85 | + public function testHasNewNames(callable $fix, bool $expected): void |
| 86 | + { |
| 87 | + $stmts = (new ParserFactory())->createForHostVersion()->parse(self::CODE); |
| 88 | + $this->assertNotNull($stmts); |
| 89 | + |
| 90 | + $newStmts = (new NodeTraverser(new CloningVisitor()))->traverse($stmts); |
| 91 | + $method = (new NodeFinder())->findFirstInstanceOf($newStmts, ClassMethod::class); |
| 92 | + $this->assertNotNull($method); |
| 93 | + |
| 94 | + $this->assertSame($expected, NewClassNamesVisitor::hasNewNames([$fix($method)])); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments