Skip to content

Commit

Permalink
[Php80] Skip PDO::query() on AddParamBasedOnParentClassMethodRector (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik authored Dec 3, 2023
1 parent eeadb49 commit 483aca1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

class SkipExtendsPDOQuery extends \PDO {
public function query() {
$args = func_get_args();
return parent::query($args[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -37,6 +39,7 @@ public function __construct(
private readonly AstResolver $astResolver,
private readonly BetterStandardPrinter $betterStandardPrinter,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ReflectionResolver $reflectionResolver
) {
}

Expand Down Expand Up @@ -109,6 +112,15 @@ public function refactor(Node $node): ?Node
return null;
}

$currentClassReflection = $this->reflectionResolver->resolveClassReflection($node);
$isPDO = $currentClassReflection instanceof ClassReflection && $currentClassReflection->isSubclassOf('PDO');

// It relies on phpstorm stubs that define 2 kind of query method for both php 7.4 and php 8.0
// @see https://github.com/JetBrains/phpstorm-stubs/blob/e2e898a29929d2f520fe95bdb2109d8fa895ba4a/PDO/PDO.php#L1096-L1126
if ($isPDO && $parentMethodReflection->getName() === 'query') {
return null;
}

$parentClassMethod = $this->astResolver->resolveClassMethodFromMethodReflection($parentMethodReflection);
if (! $parentClassMethod instanceof ClassMethod) {
return null;
Expand Down

0 comments on commit 483aca1

Please sign in to comment.