-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[code-quality] Add NarrowUnusedSetUpDefinedPropertyRector #388
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
53 changes: 53 additions & 0 deletions
53
...ector/Class_/NarrowUnusedSetUpDefinedPropertyRector/Fixture/defined_only_in_setup.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,53 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector\Fixture; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector\Source\SomeType; | ||
|
||
final class DefinedOnlyInSetup extends TestCase | ||
{ | ||
private MockObject $someMock; | ||
|
||
private MockObject $anotherMock; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->someMock = $this->createMock(SomeType::class); | ||
$this->anotherMock = $this->createMock(SomeType::class); | ||
} | ||
|
||
public function test() | ||
{ | ||
$this->anotherMock->expects($this->once()); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector\Fixture; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector\Source\SomeType; | ||
|
||
final class DefinedOnlyInSetup extends TestCase | ||
{ | ||
private MockObject $someMock; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->someMock = $this->createMock(SomeType::class); | ||
$anotherMock = $this->createMock(SomeType::class); | ||
} | ||
|
||
public function test() | ||
{ | ||
$this->anotherMock->expects($this->once()); | ||
} | ||
} | ||
|
||
?> |
23 changes: 23 additions & 0 deletions
23
...ality/Rector/Class_/NarrowUnusedSetUpDefinedPropertyRector/Fixture/skip_non_mocks.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,23 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector\Source\SomeType; | ||
|
||
final class SkipNonMocks extends TestCase | ||
{ | ||
private SomeType $someMock; | ||
private SomeType $anotherMock; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->someMock = new SomeType(); | ||
$this->anotherMock = new SomeType(); | ||
} | ||
|
||
public function test() | ||
{ | ||
$this->anotherMock->expects($this->once()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ss_/NarrowUnusedSetUpDefinedPropertyRector/NarrowUnusedSetUpDefinedPropertyRectorTest.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_\NarrowUnusedSetUpDefinedPropertyRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class NarrowUnusedSetUpDefinedPropertyRectorTest 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
...ests/CodeQuality/Rector/Class_/NarrowUnusedSetUpDefinedPropertyRector/Source/SomeType.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 Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector\Source; | ||
|
||
final class SomeType | ||
{ | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...deQuality/Rector/Class_/NarrowUnusedSetUpDefinedPropertyRector/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_\NarrowUnusedSetUpDefinedPropertyRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(NarrowUnusedSetUpDefinedPropertyRector::class); | ||
}; |
183 changes: 183 additions & 0 deletions
183
rules/CodeQuality/Rector/Class_/NarrowUnusedSetUpDefinedPropertyRector.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,183 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\CodeQuality\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Property; | ||
use PhpParser\NodeFinder; | ||
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_\NarrowUnusedSetUpDefinedPropertyRector\NarrowUnusedSetUpDefinedPropertyRectorTest | ||
*/ | ||
final class NarrowUnusedSetUpDefinedPropertyRector extends AbstractRector | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private const MOCK_OBJECT_CLASS = 'PHPUnit\Framework\MockObject\MockObject'; | ||
|
||
private readonly NodeFinder $nodeFinder; | ||
|
||
public function __construct( | ||
private readonly TestsNodeAnalyzer $testsNodeAnalyzer, | ||
) { | ||
$this->nodeFinder = new NodeFinder(); | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Turn property used only in setUp() to variable', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SomeServiceTest extends TestCase | ||
{ | ||
private $someServiceMock; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->someServiceMock = $this->createMock(SomeService::class); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SomeServiceTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
$someServiceMock = $this->createMock(SomeService::class); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class]; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->testsNodeAnalyzer->isInTestClass($node)) { | ||
return null; | ||
} | ||
|
||
$setUpClassMethod = $node->getMethod(MethodName::SET_UP); | ||
if (! $setUpClassMethod instanceof ClassMethod) { | ||
return null; | ||
} | ||
|
||
$hasChanged = false; | ||
|
||
foreach ($node->stmts as $key => $classStmt) { | ||
if (! $classStmt instanceof Property) { | ||
continue; | ||
} | ||
|
||
$property = $classStmt; | ||
if (! $property->type instanceof Name) { | ||
continue; | ||
} | ||
|
||
if (! $this->isName($property->type, self::MOCK_OBJECT_CLASS)) { | ||
continue; | ||
} | ||
|
||
if (! $this->isPropertyUsedOutsideSetUpClassMethod($node, $setUpClassMethod, $property)) { | ||
continue; | ||
} | ||
|
||
$hasChanged = true; | ||
|
||
unset($node->stmts[$key]); | ||
$propertyName = $property->props[0]->name->toString(); | ||
|
||
// change property to variable in setUp() method | ||
$this->traverseNodesWithCallable($setUpClassMethod, function (Node $node) use ( | ||
$propertyName | ||
): ?Variable { | ||
if (! $node instanceof PropertyFetch) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node->var, 'this')) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node->name, $propertyName)) { | ||
return null; | ||
} | ||
|
||
return new Variable($propertyName); | ||
}); | ||
} | ||
|
||
if ($hasChanged) { | ||
return $node; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function isPropertyUsedOutsideSetUpClassMethod( | ||
Class_ $class, | ||
ClassMethod $setUpClassMethod, | ||
Property $property | ||
): bool { | ||
$isPropertyUsed = false; | ||
|
||
foreach ($class->getMethods() as $classMethod) { | ||
// skip setUp() method | ||
if ($classMethod === $setUpClassMethod) { | ||
continue; | ||
} | ||
|
||
// check if property is used anywhere else than setup | ||
$usedPropertyFetch = $this->nodeFinder->findFirst($classMethod, function (Node $node) use ( | ||
$property | ||
): bool { | ||
if (! $node instanceof PropertyFetch) { | ||
return false; | ||
} | ||
|
||
if (! $this->isName($node->var, 'this')) { | ||
return false; | ||
} | ||
|
||
$propertyName = $property->props[0]->name->toString(); | ||
return $this->isName($node->name, $propertyName); | ||
}); | ||
|
||
if ($usedPropertyFetch instanceof PropertyFetch) { | ||
$isPropertyUsed = true; | ||
} | ||
} | ||
|
||
return $isPropertyUsed; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the check is flipped, continue should check when property is used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, it seems fixed at f24bef5