-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodingStyle] Add DataProviderArrayItemsNewlinedRector (#3271)
- Loading branch information
1 parent
e8dd953
commit 58c8f17
Showing
16 changed files
with
281 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...sMethod/DataProviderArrayItemsNewlinedRector/DataProviderArrayItemsNewlinedRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class DataProviderArrayItemsNewlinedRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
/** | ||
* @return Iterator<string> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...yle/Rector/ClassMethod/DataProviderArrayItemsNewlinedRector/Fixture/skip_no_array.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SkipNoArray extends TestCase | ||
{ | ||
public function provideData(): array | ||
{ | ||
return 'content' . 8; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
.../Rector/ClassMethod/DataProviderArrayItemsNewlinedRector/Fixture/skip_non_phpunit.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture; | ||
|
||
final class SkipNonPhpUnit | ||
{ | ||
public function provideData(): array | ||
{ | ||
return [['content', 8], ['content123', 11]]; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...gStyle/Rector/ClassMethod/DataProviderArrayItemsNewlinedRector/Fixture/some_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ImageBinaryTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function testGetBytesSize(string $content, int $number): void | ||
{ | ||
// ... | ||
} | ||
|
||
public function provideData(): array | ||
{ | ||
return [['content', 8], ['content123', 11]]; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ImageBinaryTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function testGetBytesSize(string $content, int $number): void | ||
{ | ||
// ... | ||
} | ||
|
||
public function provideData(): array | ||
{ | ||
return [ | ||
['content', 8], | ||
['content123', 11], | ||
]; | ||
} | ||
} | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
...gStyle/Rector/ClassMethod/DataProviderArrayItemsNewlinedRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(DataProviderArrayItemsNewlinedRector::class); | ||
}; |
136 changes: 136 additions & 0 deletions
136
rules/CodingStyle/Rector/ClassMethod/DataProviderArrayItemsNewlinedRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodingStyle\Rector\ClassMethod; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Return_; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\DataProviderArrayItemsNewlinedRectorTest | ||
*/ | ||
final class DataProviderArrayItemsNewlinedRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly TestsNodeAnalyzer $testsNodeAnalyzer | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change data provider in PHPUnit test case to newline per item', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
final class ImageBinaryTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function testGetBytesSize(string $content, int $number): void | ||
{ | ||
// ... | ||
} | ||
public function provideData(): array | ||
{ | ||
return [['content', 8], ['content123', 11]]; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
final class ImageBinaryTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function testGetBytesSize(string $content, int $number): void | ||
{ | ||
// ... | ||
} | ||
public function provideData(): array | ||
{ | ||
return [ | ||
['content', 8], | ||
['content123', 11] | ||
]; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ClassMethod::class]; | ||
} | ||
|
||
/** | ||
* @param ClassMethod $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $node->isPublic()) { | ||
return null; | ||
} | ||
|
||
if (! $this->testsNodeAnalyzer->isInTestClass($node)) { | ||
return null; | ||
} | ||
|
||
// skip test methods | ||
if ($this->isName($node, 'test*')) { | ||
return null; | ||
} | ||
|
||
// find array in data provider - must contain a return node | ||
|
||
/** @var Return_[] $returns */ | ||
$returns = $this->betterNodeFinder->findInstanceOf((array) $node->stmts, Return_::class); | ||
|
||
$hasChanged = false; | ||
|
||
foreach ($returns as $return) { | ||
if (! $return->expr instanceof Array_) { | ||
continue; | ||
} | ||
|
||
$array = $return->expr; | ||
if ($array->items === []) { | ||
continue; | ||
} | ||
|
||
// ensure newlined printed | ||
$array->setAttribute(AttributeKey::NEWLINED_ARRAY_PRINT, true); | ||
|
||
// invoke reprint | ||
$array->setAttribute(AttributeKey::ORIGINAL_NODE, null); | ||
|
||
$hasChanged = true; | ||
} | ||
|
||
if ($hasChanged) { | ||
return $node; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.