-
-
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.
[DowngradePhp80] Add DowngradeReflectionPropertyGetDefaultValueRector (…
…#1486) * [DowngradePhp80] Add DowngradeReflectionPropertyGetDefaultValueRector * add test * phpstan * use getDefaultValue() back * implemented * fixture skip different method name * [ci-review] Rector Rectify * skip different object * test for pull from phpstan reflection * eol Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
a77b3f4
commit 67d1b5f
Showing
10 changed files
with
230 additions
and
4 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
34 changes: 34 additions & 0 deletions
34
...ionPropertyGetDefaultValueRector/DowngradeReflectionPropertyGetDefaultValueRectorTest.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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionPropertyGetDefaultValueRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class DowngradeReflectionPropertyGetDefaultValueRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
* @requires PHP 8.0 | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...ector/MethodCall/DowngradeReflectionPropertyGetDefaultValueRector/Fixture/fixture.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,31 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionPropertyGetDefaultValueRector\Fixture; | ||
|
||
use ReflectionProperty; | ||
|
||
class SomeClass | ||
{ | ||
public function run(ReflectionProperty $reflectionProperty) | ||
{ | ||
return $reflectionProperty->getDefaultValue(); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionPropertyGetDefaultValueRector\Fixture; | ||
|
||
use ReflectionProperty; | ||
|
||
class SomeClass | ||
{ | ||
public function run(ReflectionProperty $reflectionProperty) | ||
{ | ||
return $reflectionProperty->getDeclaringClass()->getDefaultProperties()[$reflectionProperty->getName()] ?? null; | ||
} | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...gradeReflectionPropertyGetDefaultValueRector/Fixture/pull_from_phpstan_reflection.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\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionPropertyGetDefaultValueRector\Fixture; | ||
|
||
use PHPStan\Reflection\Php\PhpPropertyReflection; | ||
|
||
class PullFromPHPStanReflection | ||
{ | ||
public function get(PhpPropertyReflection $phpPropertyReflection) | ||
{ | ||
$reflectionProperty = $phpPropertyReflection->getNativeReflection(); | ||
return is_array($reflectionProperty->getDefaultValue()); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionPropertyGetDefaultValueRector\Fixture; | ||
|
||
use PHPStan\Reflection\Php\PhpPropertyReflection; | ||
|
||
class PullFromPHPStanReflection | ||
{ | ||
public function get(PhpPropertyReflection $phpPropertyReflection) | ||
{ | ||
$reflectionProperty = $phpPropertyReflection->getNativeReflection(); | ||
return is_array($reflectionProperty->getDeclaringClass()->getDefaultProperties()[$reflectionProperty->getName()] ?? null); | ||
} | ||
} | ||
|
||
?> |
13 changes: 13 additions & 0 deletions
13
...wngradeReflectionPropertyGetDefaultValueRector/Fixture/skip_different_method_call.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\DowngradePhp80\Rector\MethodCall\DowngradeReflectionPropertyGetDefaultValueRector\Fixture; | ||
|
||
use ReflectionProperty; | ||
|
||
class SkipDifferentMethodName | ||
{ | ||
public function run(ReflectionProperty $reflectionProperty) | ||
{ | ||
return $reflectionProperty->getName(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ll/DowngradeReflectionPropertyGetDefaultValueRector/Fixture/skip_different_object.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\DowngradePhp80\Rector\MethodCall\DowngradeReflectionPropertyGetDefaultValueRector\Fixture; | ||
|
||
use Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionPropertyGetDefaultValueRector\Source\SomeClassHasGetDefaultValue; | ||
|
||
class SkipDifferentObject | ||
{ | ||
public function run(SomeClassHasGetDefaultValue $someClass) | ||
{ | ||
return $someClass->getDefaultValue(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...l/DowngradeReflectionPropertyGetDefaultValueRector/Source/SomeClassHasGetDefaultValue.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 | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionPropertyGetDefaultValueRector\Source; | ||
|
||
class SomeClassHasGetDefaultValue | ||
{ | ||
public function getDefaultValue() | ||
{ | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...or/MethodCall/DowngradeReflectionPropertyGetDefaultValueRector/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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\DowngradePhp80\Rector\MethodCall\DowngradeReflectionPropertyGetDefaultValueRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(DowngradeReflectionPropertyGetDefaultValueRector::class); | ||
}; |
82 changes: 82 additions & 0 deletions
82
rules/DowngradePhp80/Rector/MethodCall/DowngradeReflectionPropertyGetDefaultValueRector.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,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DowngradePhp80\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\BinaryOp\Coalesce; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionPropertyGetDefaultValueRector\DowngradeReflectionPropertyGetDefaultValueRectorTest | ||
*/ | ||
final class DowngradeReflectionPropertyGetDefaultValueRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Downgrade ReflectionProperty->getDefaultValue()', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run(ReflectionProperty $reflectionProperty) | ||
{ | ||
return $reflectionProperty->getDefaultValue(); | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run(ReflectionProperty $reflectionProperty) | ||
{ | ||
return $reflectionProperty->getDeclaringClass()->getDefaultProperties()[$reflectionProperty->getName()] ?? null; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->nodeNameResolver->isName($node->name, 'getDefaultValue')) { | ||
return null; | ||
} | ||
|
||
$objectType = $this->nodeTypeResolver->getType($node->var); | ||
if (! $objectType instanceof ObjectType) { | ||
return null; | ||
} | ||
|
||
if ($objectType->getClassName() !== 'ReflectionProperty') { | ||
return null; | ||
} | ||
|
||
$getName = new MethodCall($node->var, 'getName'); | ||
$node = new MethodCall($node->var, 'getDeclaringClass'); | ||
$node = new MethodCall($node, 'getDefaultProperties'); | ||
$node = new ArrayDimFetch($node, $getName); | ||
|
||
return new Coalesce($node, $this->nodeFactory->createNull()); | ||
} | ||
} |