Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config/set/downgrade-php82.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\ValueObject\PhpVersion;
use Rector\DowngradePhp82\Rector\Class_\DowngradeReadonlyClassRector;
use Rector\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector;
use Rector\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector;
use Rector\DowngradePhp82\Rector\FunctionLike\DowngradeStandaloneNullTrueFalseReturnTypeRector;
use Rector\DowngradePhp82\Rector\MethodCall\DowngradeReflectionMethodHasPrototypeRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->phpVersion(PhpVersion::PHP_81);
Expand All @@ -16,5 +17,6 @@
DowngradeStandaloneNullTrueFalseReturnTypeRector::class,
DowngradeIteratorCountToArrayRector::class,
DowngradeUnionIntersectionRector::class,
DowngradeReflectionMethodHasPrototypeRector::class,
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp82\Rector\MethodCall\DowngradeReflectionMethodHasPrototypeRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DowngradeReflectionMethodHasPrototypeRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::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,40 @@
<?php

namespace Rector\Tests\DowngradePhp82\Rector\MethodCall\DowngradeReflectionMethodHasPrototypeRector\Fixture;

use ReflectionMethod;

function resolvePrototype(ReflectionMethod $reflectionMethod): ReflectionMethod
{
if ($reflectionMethod->hasPrototype()) {
$reflectionMethod = $reflectionMethod->getPrototype();
}

return $reflectionMethod;
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp82\Rector\MethodCall\DowngradeReflectionMethodHasPrototypeRector\Fixture;

use ReflectionMethod;

function resolvePrototype(ReflectionMethod $reflectionMethod): ReflectionMethod
{
if ((function (\ReflectionMethod $reflectionMethod): bool {
try {
$reflectionMethod->getPrototype();
return true;
} catch (\ReflectionException) {
return false;
}
})($reflectionMethod)) {
$reflectionMethod = $reflectionMethod->getPrototype();
}

return $reflectionMethod;
}

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

namespace Rector\Tests\DowngradePhp82\Rector\MethodCall\DowngradeReflectionMethodHasPrototypeRector\Fixture;

use ReflectionMethod;

function firstClassCallable(ReflectionMethod $reflectionMethod): callable
{
return $reflectionMethod->hasPrototype(...);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\DowngradePhp82\Rector\MethodCall\DowngradeReflectionMethodHasPrototypeRector\Fixture;

final class SomeCustomReflection
{
public function hasPrototype(): bool
{
return true;
}
}

function runOnOther(SomeCustomReflection $someCustomReflection): bool
{
return $someCustomReflection->hasPrototype();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rector\Tests\DowngradePhp82\Rector\MethodCall\DowngradeReflectionMethodHasPrototypeRector\Fixture;

use ReflectionMethod;

function checkPrototype(ReflectionMethod $reflectionMethod): bool
{
return $reflectionMethod->hasPrototype();
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp82\Rector\MethodCall\DowngradeReflectionMethodHasPrototypeRector\Fixture;

use ReflectionMethod;

function checkPrototype(ReflectionMethod $reflectionMethod): bool
{
return (function (\ReflectionMethod $reflectionMethod): bool {
try {
$reflectionMethod->getPrototype();
return true;
} catch (\ReflectionException) {
return false;
}
})($reflectionMethod);
}

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

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp82\Rector\MethodCall\DowngradeReflectionMethodHasPrototypeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DowngradeReflectionMethodHasPrototypeRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp82\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\TryCatch;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\DowngradePhp82\Rector\MethodCall\DowngradeReflectionMethodHasPrototypeRector\DowngradeReflectionMethodHasPrototypeRectorTest
*/
final class DowngradeReflectionMethodHasPrototypeRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Downgrade ReflectionMethod::hasPrototype() by emulating it with getPrototype()', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run(ReflectionMethod $reflectionMethod): bool
{
return $reflectionMethod->hasPrototype();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run(ReflectionMethod $reflectionMethod): bool
{
return (function (\ReflectionMethod $reflectionMethod): bool {
try {
$reflectionMethod->getPrototype();
return true;
} catch (\ReflectionException) {
return false;
}
})($reflectionMethod);
}
}
CODE_SAMPLE
),
]);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}

/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if ($node->isFirstClassCallable()) {
return null;
}

if (! $this->isName($node->name, 'hasPrototype')) {
return null;
}

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

return new FuncCall($this->createClosure(), [new Arg($node->var)]);
}

private function createClosure(): Closure
{
$reflectionMethodVariable = new Variable('reflectionMethod');

$tryCatch = new TryCatch(
[
new Expression(new MethodCall($reflectionMethodVariable, 'getPrototype')),
new Return_(new ConstFetch(new Name('true'))),
],
[
new Catch_(
[new FullyQualified('ReflectionException')],
null,
[new Return_(new ConstFetch(new Name('false')))]
),
]
);

return new Closure([
'params' => [new Param($reflectionMethodVariable, null, new FullyQualified('ReflectionMethod'))],
'returnType' => new Identifier('bool'),
'stmts' => [$tryCatch],
]);
}
}
Loading