Skip to content

Commit

Permalink
[DowngradePhp80] Add DowngradeReflectionPropertyGetDefaultValueRector (
Browse files Browse the repository at this point in the history
…#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
samsonasik and actions-user committed Dec 14, 2021
1 parent a77b3f4 commit 67d1b5f
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 4 deletions.
2 changes: 2 additions & 0 deletions config/set/downgrade-php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Rector\DowngradePhp80\Rector\Instanceof_\DowngradePhp80ResourceReturnToObjectRector;
use Rector\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector;
use Rector\DowngradePhp80\Rector\MethodCall\DowngradeReflectionGetAttributesRector;
use Rector\DowngradePhp80\Rector\MethodCall\DowngradeReflectionPropertyGetDefaultValueRector;
use Rector\DowngradePhp80\Rector\New_\DowngradeArbitraryExpressionsSupportRector;
use Rector\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector;
use Rector\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector;
Expand Down Expand Up @@ -74,4 +75,5 @@
$services->set(DowngradePhp80ResourceReturnToObjectRector::class);
$services->set(DowngradeReflectionGetAttributesRector::class);
$services->set(DowngradeRecursiveDirectoryIteratorHasChildrenRector::class);
$services->set(DowngradeReflectionPropertyGetDefaultValueRector::class);
};
5 changes: 1 addition & 4 deletions packages/NodeTypeResolver/TypeAnalyzer/ArrayTypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,7 @@ private function isPropertyFetchWithArrayDefault(Node $node): bool
$phpPropertyReflection = $this->reflectionResolver->resolvePropertyReflectionFromPropertyFetch($node);
if ($phpPropertyReflection instanceof PhpPropertyReflection) {
$reflectionProperty = $phpPropertyReflection->getNativeReflection();
$reflectionClass = $reflectionProperty->getDeclaringClass();
$defaultValue = $reflectionClass->getDefaultProperties()[$reflectionProperty->getName()] ?? null;

return is_array($defaultValue);
return is_array($reflectionProperty->getDefaultValue());
}

return false;
Expand Down
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';
}
}
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;
}
}

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

?>
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();
}
}
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();
}
}
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()
{
}
}
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);
};
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());
}
}

0 comments on commit 67d1b5f

Please sign in to comment.