-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
887a20a
commit 8acf739
Showing
9 changed files
with
337 additions
and
2 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
21 changes: 21 additions & 0 deletions
21
...Quality/Rector/Class_/SetUpBeforeClassToSetUpRector/Fixture/keep_unrelated_method.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,21 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SetUpBeforeClassToSetUpRector\Fixture; | ||
|
||
use CodeQuality\Rector\Class_\SetUpBeforeClassToSetUpRector\Source\SomeService; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class KeepUnrelatedMethod extends TestCase | ||
{ | ||
private static $someService; | ||
|
||
public static function prepare(): void | ||
{ | ||
self::$someService = new SomeService(); | ||
} | ||
|
||
public function test() | ||
{ | ||
$result = self::$someService->getValue(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...deQuality/Rector/Class_/SetUpBeforeClassToSetUpRector/Fixture/skip_other_property.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,16 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SetUpBeforeClassToSetUpRector\Fixture; | ||
|
||
use CodeQuality\Rector\Class_\SetUpBeforeClassToSetUpRector\Source\SomeService; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SkipOtherProperty extends TestCase | ||
{ | ||
private static $otherProperty; | ||
|
||
public static function setUpBeforeClass(): void | ||
{ | ||
$someService = new SomeService(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...ality/Rector/Class_/SetUpBeforeClassToSetUpRector/Fixture/some_setup_before_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,47 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SetUpBeforeClassToSetUpRector\Fixture; | ||
|
||
use CodeQuality\Rector\Class_\SetUpBeforeClassToSetUpRector\Source\SomeService; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SomeTest extends TestCase | ||
{ | ||
private static $someService; | ||
|
||
public static function setUpBeforeClass(): void | ||
{ | ||
self::$someService = new SomeService(); | ||
} | ||
|
||
public function test() | ||
{ | ||
$result = self::$someService->getValue(); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SetUpBeforeClassToSetUpRector\Fixture; | ||
|
||
use CodeQuality\Rector\Class_\SetUpBeforeClassToSetUpRector\Source\SomeService; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SomeTest extends TestCase | ||
{ | ||
private $someService; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->someService = new SomeService(); | ||
} | ||
|
||
public function test() | ||
{ | ||
$result = $this->someService->getValue(); | ||
} | ||
} | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
...Quality/Rector/Class_/SetUpBeforeClassToSetUpRector/SetUpBeforeClassToSetUpRectorTest.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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SetUpBeforeClassToSetUpRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class SetUpBeforeClassToSetUpRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
rules-tests/CodeQuality/Rector/Class_/SetUpBeforeClassToSetUpRector/Source/SomeService.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,8 @@ | ||
<?php | ||
|
||
namespace CodeQuality\Rector\Class_\SetUpBeforeClassToSetUpRector\Source; | ||
|
||
final class SomeService | ||
{ | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...-tests/CodeQuality/Rector/Class_/SetUpBeforeClassToSetUpRector/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\Config\RectorConfig; | ||
use Rector\PHPUnit\CodeQuality\Rector\Class_\SetUpBeforeClassToSetUpRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(SetUpBeforeClassToSetUpRector::class); | ||
}; |
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
174 changes: 174 additions & 0 deletions
174
rules/CodeQuality/Rector/Class_/SetUpBeforeClassToSetUpRector.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,174 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\CodeQuality\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PhpParser\Node\Expr\StaticPropertyFetch; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\ValueObject\MethodName; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SetUpBeforeClassToSetUpRector\SetUpBeforeClassToSetUpRectorTest | ||
*/ | ||
final class SetUpBeforeClassToSetUpRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly TestsNodeAnalyzer $testsNodeAnalyzer, | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change setUpBeforeClass() to setUp() if not needed', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
final class SomeTest extends TestCase | ||
{ | ||
private static $someService; | ||
public static function setUpBeforeClass(): void | ||
{ | ||
self::$someService = new SomeService(); | ||
} | ||
public function test() | ||
{ | ||
$result = self::$someService->getValue(); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
final class SomeTest extends TestCase | ||
{ | ||
private $someService; | ||
protected function setUp(): void | ||
{ | ||
$this->someService = new SomeService(); | ||
} | ||
public function test() | ||
{ | ||
$result = $this->someService->getValue(); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class]; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$className = $this->getName($node); | ||
if ($className === null) { | ||
return null; | ||
} | ||
|
||
if (! $this->testsNodeAnalyzer->isInTestClass($node)) { | ||
return null; | ||
} | ||
|
||
$setUpBeforeClassMethod = $node->getMethod(MethodName::SET_UP_BEFORE_CLASS); | ||
if (! $setUpBeforeClassMethod instanceof ClassMethod) { | ||
return null; | ||
} | ||
|
||
$changedPropertyNames = []; | ||
|
||
// replace static property fetches | ||
$this->traverseNodesWithCallable($setUpBeforeClassMethod, function (Node $node) use ( | ||
&$changedPropertyNames | ||
) { | ||
if (! $node instanceof Assign) { | ||
return null; | ||
} | ||
|
||
if (! $node->var instanceof StaticPropertyFetch) { | ||
return null; | ||
} | ||
|
||
$staticPropertyFetch = $node->var; | ||
if (! $this->isName($staticPropertyFetch->class, 'self')) { | ||
return null; | ||
} | ||
|
||
$node->var = new PropertyFetch(new Variable('this'), $staticPropertyFetch->name); | ||
$propertyName = $this->getName($staticPropertyFetch->name); | ||
if (! is_string($propertyName)) { | ||
return null; | ||
} | ||
|
||
$changedPropertyNames[] = $propertyName; | ||
}); | ||
|
||
if ($changedPropertyNames === []) { | ||
return null; | ||
} | ||
|
||
// remove static flag | ||
$setUpBeforeClassMethod->flags -= Class_::MODIFIER_STATIC; | ||
|
||
// remove public flag | ||
$setUpBeforeClassMethod->flags -= Class_::MODIFIER_PUBLIC; | ||
|
||
// make protected | ||
$setUpBeforeClassMethod->flags += Class_::MODIFIER_PROTECTED; | ||
|
||
$setUpBeforeClassMethod->name = new Identifier('setUp'); | ||
|
||
foreach ($node->getProperties() as $property) { | ||
if (! $property->isStatic()) { | ||
continue; | ||
} | ||
|
||
if ($this->isNames($property, $changedPropertyNames)) { | ||
$property->flags -= Class_::MODIFIER_STATIC; | ||
} | ||
} | ||
|
||
// replace same property access in the class | ||
$this->traverseNodesWithCallable($node->getMethods(), function (Node $node) use ( | ||
$changedPropertyNames | ||
): ?PropertyFetch { | ||
if (! $node instanceof StaticPropertyFetch) { | ||
return null; | ||
} | ||
|
||
if (! $this->isNames($node, $changedPropertyNames)) { | ||
return null; | ||
} | ||
|
||
return new PropertyFetch(new Variable('this'), $node->name); | ||
}); | ||
|
||
return $node; | ||
} | ||
} |