-
-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DowngradePhp8.0] Add getAttributes() reflection downgrade
- Loading branch information
1 parent
27a78d9
commit 4e5372d
Showing
8 changed files
with
227 additions
and
0 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
33 changes: 33 additions & 0 deletions
33
...all/DowngradeReflectionGetAttributesRector/DowngradeReflectionGetAttributesRectorTest.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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionGetAttributesRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class DowngradeReflectionGetAttributesRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
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'; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...tor/MethodCall/DowngradeReflectionGetAttributesRector/Fixture/skip_non_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,17 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionGetAttributesRector\Fixture; | ||
|
||
use Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionGetAttributesRector\Source\NonReflectionGetAttributes; | ||
|
||
final class SkipNonReflection | ||
{ | ||
public function run(NonReflectionGetAttributes $nonReflectionGetAttributes) | ||
{ | ||
if ($nonReflectionGetAttributes->getAttributes()) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...Php80/Rector/MethodCall/DowngradeReflectionGetAttributesRector/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,35 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionGetAttributesRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function run(\ReflectionClass $reflectionClass) | ||
{ | ||
if ($reflectionClass->getAttributes()) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionGetAttributesRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function run(\ReflectionClass $reflectionClass) | ||
{ | ||
if ([]) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
?> |
35 changes: 35 additions & 0 deletions
35
...0/Rector/MethodCall/DowngradeReflectionGetAttributesRector/Fixture/with_arguments.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,35 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionGetAttributesRector\Fixture; | ||
|
||
final class WithArguments | ||
{ | ||
public function run(\ReflectionClass $reflectionClass) | ||
{ | ||
if ($reflectionClass->getAttributes('someName', 1)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionGetAttributesRector\Fixture; | ||
|
||
final class WithArguments | ||
{ | ||
public function run(\ReflectionClass $reflectionClass) | ||
{ | ||
if ([]) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
?> |
13 changes: 13 additions & 0 deletions
13
...r/MethodCall/DowngradeReflectionGetAttributesRector/Source/NonReflectionGetAttributes.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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionGetAttributesRector\Source; | ||
|
||
final class NonReflectionGetAttributes | ||
{ | ||
public function getAttributes(): array | ||
{ | ||
return []; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...Php80/Rector/MethodCall/DowngradeReflectionGetAttributesRector/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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\DowngradePhp80\Rector\MethodCall\DowngradeReflectionGetAttributesRector; | ||
|
||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(DowngradeReflectionGetAttributesRector::class); | ||
}; |
79 changes: 79 additions & 0 deletions
79
rules/DowngradePhp80/Rector/MethodCall/DowngradeReflectionGetAttributesRector.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,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DowngradePhp80\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Array_; | ||
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\DowngradeReflectionGetAttributesRector\DowngradeReflectionGetAttributesRectorTest | ||
*/ | ||
final class DowngradeReflectionGetAttributesRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Remove reflection getAttributes() class method code', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run(ReflectionClass $reflectionClass) | ||
{ | ||
if ($reflectionClass->getAttributes()) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run(ReflectionClass $reflectionClass) | ||
{ | ||
if ([]) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
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->isName($node->name, 'getAttributes')) { | ||
return null; | ||
} | ||
|
||
if (! $this->isObjectType($node->var, new ObjectType('Reflector'))) { | ||
return null; | ||
} | ||
|
||
return new Array_([]); | ||
} | ||
} |