|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\CodingStyle\Rector\ClassMethod; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\Array_; |
| 9 | +use PhpParser\Node\Stmt\ClassMethod; |
| 10 | +use PhpParser\Node\Stmt\Return_; |
| 11 | +use Rector\Core\Rector\AbstractRector; |
| 12 | +use Rector\NodeTypeResolver\Node\AttributeKey; |
| 13 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 14 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 15 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 16 | + |
| 17 | +/** |
| 18 | + * @see \Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\DataProviderArrayItemsNewlinedRectorTest |
| 19 | + */ |
| 20 | +final class DataProviderArrayItemsNewlinedRector extends AbstractRector |
| 21 | +{ |
| 22 | + public function __construct( |
| 23 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + public function getRuleDefinition(): RuleDefinition |
| 28 | + { |
| 29 | + return new RuleDefinition('Change data provider in PHPUnit test case to newline per item', [ |
| 30 | + new CodeSample( |
| 31 | + <<<'CODE_SAMPLE' |
| 32 | +use PHPUnit\Framework\TestCase; |
| 33 | +
|
| 34 | +final class ImageBinaryTest extends TestCase |
| 35 | +{ |
| 36 | + /** |
| 37 | + * @dataProvider provideData() |
| 38 | + */ |
| 39 | + public function testGetBytesSize(string $content, int $number): void |
| 40 | + { |
| 41 | + // ... |
| 42 | + } |
| 43 | +
|
| 44 | + public function provideData(): array |
| 45 | + { |
| 46 | + return [['content', 8], ['content123', 11]]; |
| 47 | + } |
| 48 | +} |
| 49 | +CODE_SAMPLE |
| 50 | + |
| 51 | + , |
| 52 | + <<<'CODE_SAMPLE' |
| 53 | +use PHPUnit\Framework\TestCase; |
| 54 | +
|
| 55 | +final class ImageBinaryTest extends TestCase |
| 56 | +{ |
| 57 | + /** |
| 58 | + * @dataProvider provideData() |
| 59 | + */ |
| 60 | + public function testGetBytesSize(string $content, int $number): void |
| 61 | + { |
| 62 | + // ... |
| 63 | + } |
| 64 | +
|
| 65 | + public function provideData(): array |
| 66 | + { |
| 67 | + return [ |
| 68 | + ['content', 8], |
| 69 | + ['content123', 11] |
| 70 | + ]; |
| 71 | + } |
| 72 | +} |
| 73 | +CODE_SAMPLE |
| 74 | + ), |
| 75 | + ]); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @return array<class-string<Node>> |
| 80 | + */ |
| 81 | + public function getNodeTypes(): array |
| 82 | + { |
| 83 | + return [ClassMethod::class]; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param ClassMethod $node |
| 88 | + */ |
| 89 | + public function refactor(Node $node): ?Node |
| 90 | + { |
| 91 | + if (! $node->isPublic()) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + // skip test methods |
| 100 | + if ($this->isName($node, 'test*')) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + // find array in data provider - must contain a return node |
| 105 | + |
| 106 | + /** @var Return_[] $returns */ |
| 107 | + $returns = $this->betterNodeFinder->findInstanceOf((array) $node->stmts, Return_::class); |
| 108 | + |
| 109 | + $hasChanged = false; |
| 110 | + |
| 111 | + foreach ($returns as $return) { |
| 112 | + if (! $return->expr instanceof Array_) { |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + $array = $return->expr; |
| 117 | + if ($array->items === []) { |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + // ensure newlined printed |
| 122 | + $array->setAttribute(AttributeKey::NEWLINED_ARRAY_PRINT, true); |
| 123 | + |
| 124 | + // invoke reprint |
| 125 | + $array->setAttribute(AttributeKey::ORIGINAL_NODE, null); |
| 126 | + |
| 127 | + $hasChanged = true; |
| 128 | + } |
| 129 | + |
| 130 | + if ($hasChanged) { |
| 131 | + return $node; |
| 132 | + } |
| 133 | + |
| 134 | + return null; |
| 135 | + } |
| 136 | +} |
0 commit comments