Skip to content

Commit

Permalink
[DowngradePhp80] Add getAttributes() reflection downgrade (#1406)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 6, 2021
1 parent 99d53a9 commit a685282
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/set/downgrade-php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Rector\Core\Configuration\Option;

use Rector\Core\ValueObject\PhpVersion;
use Rector\DowngradePhp80\Rector\Catch_\DowngradeNonCapturingCatchesRector;
use Rector\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector;
Expand All @@ -20,6 +21,7 @@
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector;
use Rector\DowngradePhp80\Rector\Instanceof_\DowngradePhp80ResourceReturnToObjectRector;
use Rector\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector;
use Rector\DowngradePhp80\Rector\MethodCall\DowngradeReflectionGetAttributesRector;
use Rector\DowngradePhp80\Rector\New_\DowngradeArbitraryExpressionsSupportRector;
use Rector\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector;
use Rector\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector;
Expand Down Expand Up @@ -67,4 +69,5 @@
$services->set(DowngradePhpTokenRector::class);
$services->set(DowngradeThrowExprRector::class);
$services->set(DowngradePhp80ResourceReturnToObjectRector::class);
$services->set(DowngradeReflectionGetAttributesRector::class);
};
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';
}
}
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;
}
}
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;
}
}

?>
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;
}
}

?>
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 [];
}
}
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);
};
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_([]);
}
}

0 comments on commit a685282

Please sign in to comment.