Skip to content

Commit

Permalink
[DowngradePhp74] Add DowngradeReflectionGetTypeRector
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 6, 2021
1 parent a685282 commit 81c6e62
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/set/downgrade-php74.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\DowngradePhp74\Rector\Array_\DowngradeArraySpreadRector;
use Rector\DowngradePhp74\Rector\ArrowFunction\ArrowFunctionToAnonymousFunctionRector;
Expand All @@ -14,6 +15,7 @@
use Rector\DowngradePhp74\Rector\Identical\DowngradeFreadFwriteFalsyToNegationRector;
use Rector\DowngradePhp74\Rector\Interface_\DowngradePreviouslyImplementedInterfaceRector;
use Rector\DowngradePhp74\Rector\LNumber\DowngradeNumericLiteralSeparatorRector;
use Rector\DowngradePhp74\Rector\MethodCall\DowngradeReflectionGetTypeRector;
use Rector\DowngradePhp74\Rector\Property\DowngradeTypedPropertyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

Expand All @@ -33,4 +35,5 @@
$services->set(DowngradeArrayMergeCallWithoutArgumentsRector::class);
$services->set(DowngradeFreadFwriteFalsyToNegationRector::class);
$services->set(DowngradePreviouslyImplementedInterfaceRector::class);
$services->set(DowngradeReflectionGetTypeRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp74\Rector\MethodCall\DowngradeReflectionGetTypeRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class DowngradeReflectionGetTypeRectorTest 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,35 @@
<?php

namespace Rector\Tests\DowngradePhp74\Rector\MethodCall\DowngradeReflectionGetTypeRector\Fixture;

class SomeClass
{
public function run(\ReflectionProperty $reflectionProperty)
{
if ($reflectionProperty->getType()) {
return true;
}

return false;
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp74\Rector\MethodCall\DowngradeReflectionGetTypeRector\Fixture;

class SomeClass
{
public function run(\ReflectionProperty $reflectionProperty)
{
if (null) {
return true;
}

return false;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Rector\DowngradePhp74\Rector\MethodCall\DowngradeReflectionGetTypeRector;

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeReflectionGetTypeRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp74\Rector\MethodCall;

use PhpParser\Node;
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\DowngradePhp74\Rector\MethodCall\DowngradeReflectionGetTypeRector\DowngradeReflectionGetTypeRectorTest
*/
final class DowngradeReflectionGetTypeRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Downgrade reflection $refleciton->getType() method call', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run(ReflectionProperty $reflectionProperty)
{
if ($reflectionProperty->getType()) {
return true;
}
return false;
}
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run(ReflectionProperty $reflectionProperty)
{
if (null) {
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, 'getType')) {
return null;
}

if (! $this->isObjectType($node->var, new ObjectType('ReflectionProperty'))) {
return null;
}

return $this->nodeFactory->createNull();
}
}

0 comments on commit 81c6e62

Please sign in to comment.