-
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.
[PHPUnit 10] Add AssertIssetToAssertObjectHasPropertyRector
- Loading branch information
1 parent
f0d26e4
commit 1e207dd
Showing
7 changed files
with
255 additions
and
50 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
28 changes: 28 additions & 0 deletions
28
...rtIssetToAssertObjectHasPropertyRector/AssertIssetToAssertObjectHasPropertyRectorTest.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\PHPUnit100\Rector\MethodCall\AssertIssetToAssertObjectHasPropertyRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class AssertIssetToAssertObjectHasPropertyRectorTest 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'; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...hodCall/AssertIssetToAssertObjectHasPropertyRector/Fixture/some_isset_to_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,33 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\MethodCall\AssertIssetToAssertObjectHasPropertyRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SomeIssetToProperty extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$object = new \stdClass(); | ||
$this->assertTrue(isset($object->someProperty)); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\MethodCall\AssertIssetToAssertObjectHasPropertyRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SomeIssetToProperty extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$object = new \stdClass(); | ||
$this->assertObjectHasAttribute('someProperty', $object); | ||
} | ||
} | ||
|
||
?> |
9 changes: 9 additions & 0 deletions
9
...0/Rector/MethodCall/AssertIssetToAssertObjectHasPropertyRector/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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\PHPUnit\PHPUnit100\Rector\MethodCall\AssertIssetToAssertObjectHasPropertyRector; | ||
|
||
return RectorConfig::configure() | ||
->withRules([AssertIssetToAssertObjectHasPropertyRector::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
152 changes: 152 additions & 0 deletions
152
rules/PHPUnit100/Rector/MethodCall/AssertIssetToAssertObjectHasPropertyRector.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,152 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\PHPUnit100\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Isset_; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Type\ObjectWithoutClassType; | ||
use PHPStan\Type\TypeWithClassName; | ||
use Rector\PHPUnit\Enum\AssertMethod; | ||
use Rector\PHPUnit\NodeAnalyzer\IdentifierManipulator; | ||
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\Reflection\ClassReflectionAnalyzer; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\PHPUnit\Tests\PHPUnit100\Rector\MethodCall\AssertIssetToAssertObjectHasPropertyRector\AssertIssetToAssertObjectHasPropertyRectorTest | ||
* | ||
* @changelog https://github.com/sebastianbergmann/phpunit/issues/5220 | ||
*/ | ||
final class AssertIssetToAssertObjectHasPropertyRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly TestsNodeAnalyzer $testsNodeAnalyzer, | ||
private readonly IdentifierManipulator $identifierManipulator, | ||
private readonly ClassReflectionAnalyzer $classReflectionAnalyzer, | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change "isset()" to check property, to assertObjectHasAttribute()method', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
final class SomeTest extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$object = new stdClass(); | ||
$this->assertTrue(isset($object->someProperty)); | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
final class SomeTest extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$object = new stdClass(); | ||
$this->assertObjectHasProperty('someProperty', $object); | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class, StaticCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall|StaticCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames( | ||
$node, | ||
[AssertMethod::ASSERT_TRUE, AssertMethod::ASSERT_FALSE] | ||
)) { | ||
return null; | ||
} | ||
|
||
if ($node->isFirstClassCallable()) { | ||
return null; | ||
} | ||
|
||
$firstArg = $node->getArgs()[0]; | ||
$firstArgValue = $firstArg->value; | ||
if (! $firstArgValue instanceof Isset_) { | ||
return null; | ||
} | ||
|
||
$issetExpr = $firstArgValue->vars[0]; | ||
if (! $issetExpr instanceof PropertyFetch) { | ||
return null; | ||
} | ||
|
||
if ($this->hasMagicIsset($issetExpr->var)) { | ||
return null; | ||
} | ||
|
||
$name = $this->getName($issetExpr); | ||
if ($name === null) { | ||
return null; | ||
} | ||
|
||
$this->identifierManipulator->renameNodeWithMap($node, [ | ||
AssertMethod::ASSERT_TRUE => 'assertObjectHasAttribute', | ||
AssertMethod::ASSERT_FALSE => 'assertObjectNotHasAttribute', | ||
]); | ||
|
||
$oldArgs = $node->getArgs(); | ||
unset($oldArgs[0]); | ||
|
||
$newArgs = $this->nodeFactory->createArgs([new Node\Scalar\String_($name), $issetExpr->var]); | ||
$node->args = [...$newArgs, ...$oldArgs]; | ||
return $node; | ||
} | ||
|
||
private function hasMagicIsset(Expr $expr): bool | ||
{ | ||
$type = $this->nodeTypeResolver->getType($expr); | ||
if (! $type instanceof TypeWithClassName) { | ||
// object not found, skip | ||
return $type instanceof ObjectWithoutClassType; | ||
} | ||
|
||
$classReflection = $type->getClassReflection(); | ||
if (! $classReflection instanceof ClassReflection) { | ||
return false; | ||
} | ||
|
||
if ($classReflection->hasMethod('__isset')) { | ||
return true; | ||
} | ||
|
||
if (! $classReflection->isClass()) { | ||
return false; | ||
} | ||
|
||
return $this->classReflectionAnalyzer->resolveParentClassName($classReflection) !== 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\Enum; | ||
|
||
final class AssertMethod | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const ASSERT_FALSE = 'assertFalse'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public const ASSERT_TRUE = 'assertTrue'; | ||
} |