diff --git a/packages/DeadCode/src/Rector/MethodCall/RemoveDefaultArgumentValueRector.php b/packages/DeadCode/src/Rector/MethodCall/RemoveDefaultArgumentValueRector.php index b043542ea71..bc45c388bf2 100644 --- a/packages/DeadCode/src/Rector/MethodCall/RemoveDefaultArgumentValueRector.php +++ b/packages/DeadCode/src/Rector/MethodCall/RemoveDefaultArgumentValueRector.php @@ -102,7 +102,7 @@ PHP */ public function refactor(Node $node): ?Node { - if ($node->args === []) { + if ($this->shouldSkip($node)) { return null; } @@ -227,13 +227,26 @@ PHP return $defaultValues; } - $coreFunctionReflection = $this->functionReflectionResolver->resolveCoreStubFunctionNode($nodeName); + return []; + } - // unable to found - if ($coreFunctionReflection === null) { - return []; + /** + * @param MethodCall|StaticCall|FuncCall $node + */ + private function shouldSkip(Node $node): bool + { + if ($node->args === []) { + return true; } - return $this->resolveDefaultParamValuesFromFunctionLike($coreFunctionReflection); + if (! $node instanceof FuncCall) { + return false; + } + + // skip native functions, hard to analyze without stubs (stubs would make working with IDE non-practical) + /** @var string $functionName */ + $functionName = $this->getName($node); + + return $this->functionReflectionResolver->isPhpNativeFunction($functionName); } } diff --git a/packages/DeadCode/tests/Rector/MethodCall/RemoveDefaultArgumentValueRector/Fixture/system_function.php.inc b/packages/DeadCode/tests/Rector/MethodCall/RemoveDefaultArgumentValueRector/Fixture/skip_system_function.php.inc similarity index 50% rename from packages/DeadCode/tests/Rector/MethodCall/RemoveDefaultArgumentValueRector/Fixture/system_function.php.inc rename to packages/DeadCode/tests/Rector/MethodCall/RemoveDefaultArgumentValueRector/Fixture/skip_system_function.php.inc index 19412b8e0a3..e280fcd9ca8 100644 --- a/packages/DeadCode/tests/Rector/MethodCall/RemoveDefaultArgumentValueRector/Fixture/system_function.php.inc +++ b/packages/DeadCode/tests/Rector/MethodCall/RemoveDefaultArgumentValueRector/Fixture/skip_system_function.php.inc @@ -9,19 +9,3 @@ class SystemFunction trigger_error('Error message', E_USER_NOTICE); } } - -?> ------ - diff --git a/packages/DeadCode/tests/Rector/MethodCall/RemoveDefaultArgumentValueRector/RemoveDefaultArgumentValueRectorTest.php b/packages/DeadCode/tests/Rector/MethodCall/RemoveDefaultArgumentValueRector/RemoveDefaultArgumentValueRectorTest.php index 6f26accd27d..fbe6fe3f743 100644 --- a/packages/DeadCode/tests/Rector/MethodCall/RemoveDefaultArgumentValueRector/RemoveDefaultArgumentValueRectorTest.php +++ b/packages/DeadCode/tests/Rector/MethodCall/RemoveDefaultArgumentValueRector/RemoveDefaultArgumentValueRectorTest.php @@ -22,7 +22,7 @@ final class RemoveDefaultArgumentValueRectorTest extends AbstractRectorTestCase yield [__DIR__ . '/Fixture/skip_previous_order.php.inc']; yield [__DIR__ . '/Fixture/function.php.inc']; yield [__DIR__ . '/Fixture/user_vendor_function.php.inc']; - yield [__DIR__ . '/Fixture/system_function.php.inc']; + yield [__DIR__ . '/Fixture/skip_system_function.php.inc']; } protected function getRectorClass(): string diff --git a/packages/NodeTypeResolver/config/config.yaml b/packages/NodeTypeResolver/config/config.yaml index 6fd748c9caf..b70a912189a 100644 --- a/packages/NodeTypeResolver/config/config.yaml +++ b/packages/NodeTypeResolver/config/config.yaml @@ -25,3 +25,6 @@ services: PHPStan\Analyser\ScopeFactory: factory: ['@Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory', 'createScopeFactory'] + + PHPStan\Reflection\SignatureMap\SignatureMapProvider: + factory: ['@Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory', 'createSignatureMapProvider'] diff --git a/packages/NodeTypeResolver/src/DependencyInjection/PHPStanServicesFactory.php b/packages/NodeTypeResolver/src/DependencyInjection/PHPStanServicesFactory.php index 0faf8847860..f73e8c31df7 100644 --- a/packages/NodeTypeResolver/src/DependencyInjection/PHPStanServicesFactory.php +++ b/packages/NodeTypeResolver/src/DependencyInjection/PHPStanServicesFactory.php @@ -8,6 +8,7 @@ use PHPStan\Analyser\ScopeFactory; use PHPStan\Analyser\TypeSpecifier; use PHPStan\Broker\Broker; use PHPStan\DependencyInjection\ContainerFactory; +use PHPStan\Reflection\SignatureMap\SignatureMapProvider; final class PHPStanServicesFactory { @@ -47,6 +48,11 @@ final class PHPStanServicesFactory return $this->container->getByType(TypeSpecifier::class); } + public function createSignatureMapProvider(): SignatureMapProvider + { + return $this->container->getByType(SignatureMapProvider::class); + } + public function createScopeFactory(): ScopeFactory { return $this->container->getByType(ScopeFactory::class); diff --git a/src/Reflection/FunctionReflectionResolver.php b/src/Reflection/FunctionReflectionResolver.php index da4255ef652..064d723fa32 100644 --- a/src/Reflection/FunctionReflectionResolver.php +++ b/src/Reflection/FunctionReflectionResolver.php @@ -2,65 +2,22 @@ namespace Rector\Reflection; -use PhpParser\Node; -use PhpParser\Node\Stmt\Function_; -use Rector\Exception\ShouldNotHappenException; -use Rector\PhpParser\Node\BetterNodeFinder; -use Rector\PhpParser\Parser\Parser; +use PHPStan\Reflection\SignatureMap\SignatureMapProvider; final class FunctionReflectionResolver { /** - * @var string[] + * @var SignatureMapProvider */ - private const POSSIBLE_CORE_STUB_LOCATIONS = [ - __DIR__ . '/../../../../jetbrains/phpstorm-stubs/Core/Core.php', - __DIR__ . '/../../vendor/jetbrains/phpstorm-stubs/Core/Core.php', - ]; + private $signatureMapProvider; - /** - * @var Parser - */ - private $parser; - - /** - * @var BetterNodeFinder - */ - private $betterNodeFinder; - - public function __construct(Parser $parser, BetterNodeFinder $betterNodeFinder) + public function __construct(SignatureMapProvider $signatureMapProvider) { - $this->parser = $parser; - $this->betterNodeFinder = $betterNodeFinder; + $this->signatureMapProvider = $signatureMapProvider; } - public function resolveCoreStubFunctionNode(string $nodeName): ?Function_ + public function isPhpNativeFunction(string $functionName): bool { - $stubFileLocation = $this->resolveCoreStubLocation(); - - $nodes = $this->parser->parseFile($stubFileLocation); - - /** @var Function_|null $function */ - $function = $this->betterNodeFinder->findFirst($nodes, function (Node $node) use ($nodeName): bool { - if (! $node instanceof Function_) { - return false; - } - - return (string) $node->name === $nodeName; - }); - - return $function; - } - - private function resolveCoreStubLocation(): string - { - foreach (self::POSSIBLE_CORE_STUB_LOCATIONS as $possibleCoreStubLocation) { - if (file_exists($possibleCoreStubLocation)) { - /** @var string $possibleCoreStubLocation */ - return $possibleCoreStubLocation; - } - } - - throw new ShouldNotHappenException(); + return $this->signatureMapProvider->hasFunctionSignature($functionName); } }