From d17d3e814eada2fa3ada3601034407df01d84568 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 1 Oct 2024 20:02:39 +0700 Subject: [PATCH] [TypeDeclaration] Better approach for native type check on ReturnStrictTypeAnalyzer (#6343) --- phpstan.neon | 4 ---- .../TypeAnalyzer/ReturnStrictTypeAnalyzer.php | 16 ++++++---------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 6b4de4cd90..b9bc6261d0 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -53,10 +53,6 @@ parameters: message: '#Function "var_dump\(\)" cannot be used/left in the code#' path: src/functions/node_helper.php - - - message: '#Function "method_exists\(\)" cannot be used/left in the code#' - path: rules/TypeDeclaration/TypeAnalyzer/ReturnStrictTypeAnalyzer.php - # lack of generic array in nikic/php-parser - '#Method (.*?) should return array but returns array#' diff --git a/rules/TypeDeclaration/TypeAnalyzer/ReturnStrictTypeAnalyzer.php b/rules/TypeDeclaration/TypeAnalyzer/ReturnStrictTypeAnalyzer.php index f6e93203ff..37e912c64c 100644 --- a/rules/TypeDeclaration/TypeAnalyzer/ReturnStrictTypeAnalyzer.php +++ b/rules/TypeDeclaration/TypeAnalyzer/ReturnStrictTypeAnalyzer.php @@ -21,7 +21,8 @@ use PhpParser\Node\Stmt\Return_; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; -use PHPStan\Reflection\FunctionVariantWithPhpDocs; +use PHPStan\Reflection\Native\NativeFunctionReflection; +use PHPStan\Reflection\ParametersAcceptorWithPhpDocs; use PHPStan\Type\MixedType; use PHPStan\Type\StaticType; use PHPStan\Type\Type; @@ -120,19 +121,14 @@ public function resolveMethodCallReturnType(MethodCall | StaticCall | FuncCall $ $call, $scope ); - if ($parametersAcceptorWithPhpDocs instanceof FunctionVariantWithPhpDocs) { + + if ($methodReflection instanceof NativeFunctionReflection) { + $returnType = $parametersAcceptorWithPhpDocs->getReturnType(); + } elseif ($parametersAcceptorWithPhpDocs instanceof ParametersAcceptorWithPhpDocs) { // native return type is needed, as docblock can be false $returnType = $parametersAcceptorWithPhpDocs->getNativeReturnType(); } else { $returnType = $parametersAcceptorWithPhpDocs->getReturnType(); - - // around PHPStan 1.12.4+ handling - if (method_exists($parametersAcceptorWithPhpDocs, 'getNativeReturnType')) { - $nativeReturnType = $parametersAcceptorWithPhpDocs->getNativeReturnType(); - if ($nativeReturnType instanceof MixedType && ! $nativeReturnType->isExplicitMixed()) { - $returnType = $nativeReturnType; - } - } } if ($returnType instanceof MixedType) {