Skip to content

Commit

Permalink
[TypeDeclaration] Better approach for native type check on ReturnStri…
Browse files Browse the repository at this point in the history
…ctTypeAnalyzer (#6343)
  • Loading branch information
samsonasik authored Oct 1, 2024
1 parent 697b37b commit d17d3e8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
4 changes: 0 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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<PhpParser\\Node\\(.*?)\> but returns array<PhpParser\\Node\>#'

Expand Down
16 changes: 6 additions & 10 deletions rules/TypeDeclaration/TypeAnalyzer/ReturnStrictTypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit d17d3e8

Please sign in to comment.