|
15 | 15 | use PhpParser\Node\UnionType; |
16 | 16 | use PhpParser\PrettyPrinter\Standard; |
17 | 17 | use PHPStan\Analyser\Scope; |
| 18 | +use PHPStan\Reflection\ClassReflection; |
18 | 19 | use PHPStan\Reflection\ExtendedMethodReflection; |
19 | 20 | use PHPStan\Reflection\ParametersAcceptorSelector; |
20 | 21 | use PHPStan\Type\ClosureType; |
@@ -88,29 +89,56 @@ public function printArgTypesAsString( |
88 | 89 | return implode('|', $stringArgTypes); |
89 | 90 | } |
90 | 91 |
|
91 | | - public function printParamTypesToString(ClassMethod $classMethod, string $className): string |
92 | | - { |
| 92 | + public function printParamTypesToString( |
| 93 | + ClassMethod $classMethod, |
| 94 | + ClassReflection $classReflection, |
| 95 | + Scope $scope |
| 96 | + ): string { |
| 97 | + $className = $classReflection->getName(); |
| 98 | + |
| 99 | + $parametersReflection = []; |
| 100 | + if ($classReflection->hasMethod($classMethod->name->name)) { |
| 101 | + $methodReflection = $classReflection->getMethod($classMethod->name->name, $scope); |
| 102 | + $variants = $methodReflection->getVariants(); |
| 103 | + if (count($variants) === 1) { |
| 104 | + $parametersReflection = $variants[0]->getParameters(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
93 | 108 | $printedParamTypes = []; |
94 | | - foreach ($classMethod->params as $param) { |
| 109 | + foreach ($classMethod->params as $i => $param) { |
95 | 110 | if ($param->type === null) { |
96 | 111 | $printedParamTypes[] = ''; |
97 | 112 | continue; |
98 | 113 | } |
99 | 114 |
|
100 | | - $paramType = $this->transformSelfToClassName($param->type, $className); |
101 | | - if ($paramType instanceof NullableType) { |
102 | | - // unite to phpstan type |
103 | | - $paramType = new UnionType([$paramType->type, new Identifier('null')]); |
| 115 | + $phpdocType = null; |
| 116 | + if (array_key_exists($i, $parametersReflection)) { |
| 117 | + $paramphpdocType = $parametersReflection[$i]->getPhpDocType(); |
| 118 | + if (! $paramphpdocType instanceof MixedType) { |
| 119 | + $phpdocType = $paramphpdocType; |
| 120 | + } |
104 | 121 | } |
105 | 122 |
|
106 | | - if ($paramType instanceof UnionType || $paramType instanceof NodeIntersectionType) { |
107 | | - $paramType = $this->resolveSortedTypes($paramType, $className); |
108 | | - } |
| 123 | + if ($phpdocType instanceof Type) { |
| 124 | + $printedParamType = $this->printTypeToString($phpdocType); |
| 125 | + } else { |
| 126 | + $paramType = $this->transformSelfToClassName($param->type, $className); |
| 127 | + |
| 128 | + if ($paramType instanceof NullableType) { |
| 129 | + // unite to phpstan type |
| 130 | + $paramType = new UnionType([$paramType->type, new Identifier('null')]); |
| 131 | + } |
109 | 132 |
|
110 | | - $printedParamType = $this->standard->prettyPrint([$paramType]); |
111 | | - $printedParamType = str_replace('\Closure', 'callable', $printedParamType); |
112 | | - $printedParamType = ltrim($printedParamType, '\\'); |
113 | | - $printedParamType = str_replace('|\\', '|', $printedParamType); |
| 133 | + if ($paramType instanceof UnionType || $paramType instanceof NodeIntersectionType) { |
| 134 | + $paramType = $this->resolveSortedTypes($paramType, $className); |
| 135 | + } |
| 136 | + |
| 137 | + $printedParamType = $this->standard->prettyPrint([$paramType]); |
| 138 | + $printedParamType = str_replace('\Closure', 'callable', $printedParamType); |
| 139 | + $printedParamType = ltrim($printedParamType, '\\'); |
| 140 | + $printedParamType = str_replace('|\\', '|', $printedParamType); |
| 141 | + } |
114 | 142 |
|
115 | 143 | // to avoid DateTime vs DateTimeImmutable vs DateTimeInterface conflicts |
116 | 144 | $printedParamType = $this->normalizeDateTime($printedParamType); |
|
0 commit comments