|
6 | 6 | use PHPStan\Reflection\Dummy\ChangedTypeMethodReflection; |
7 | 7 | use PHPStan\Reflection\ExtendedFunctionVariant; |
8 | 8 | use PHPStan\Reflection\ExtendedMethodReflection; |
9 | | -use PHPStan\Reflection\ExtendedParameterReflection; |
10 | 9 | use PHPStan\Reflection\ExtendedParametersAcceptor; |
11 | 10 | use PHPStan\Reflection\Php\ExtendedDummyParameter; |
12 | 11 | use PHPStan\Reflection\ResolvedMethodReflection; |
13 | 12 | use PHPStan\Type\ThisType; |
14 | 13 | use PHPStan\Type\Type; |
15 | 14 | use PHPStan\Type\TypeCombinator; |
16 | | -use function array_map; |
17 | 15 |
|
18 | 16 | final class CallbackUnresolvedMethodPrototypeReflection implements UnresolvedMethodPrototypeReflection |
19 | 17 | { |
@@ -85,56 +83,70 @@ public function withCalledOnType(Type $type): UnresolvedMethodPrototypeReflectio |
85 | 83 | private function transformMethodWithStaticType(ClassReflection $declaringClass, ExtendedMethodReflection $method): ExtendedMethodReflection |
86 | 84 | { |
87 | 85 | $selfOutType = $method->getSelfOutType() !== null ? $this->transformStaticType($method->getSelfOutType()) : null; |
88 | | - $variantFn = function (ExtendedParametersAcceptor $acceptor) use (&$selfOutType): ExtendedParametersAcceptor { |
89 | | - $originalReturnType = $acceptor->getReturnType(); |
90 | | - if ($originalReturnType instanceof ThisType && $selfOutType !== null) { |
91 | | - $returnType = TypeCombinator::intersect($selfOutType, $this->transformStaticType($originalReturnType)); |
92 | | - $selfOutType = $returnType; |
93 | | - } else { |
94 | | - $returnType = $this->transformStaticType($originalReturnType); |
95 | | - } |
96 | | - return new ExtendedFunctionVariant( |
97 | | - $acceptor->getTemplateTypeMap(), |
98 | | - $acceptor->getResolvedTemplateTypeMap(), |
99 | | - array_map( |
100 | | - fn (ExtendedParameterReflection $parameter): ExtendedParameterReflection => new ExtendedDummyParameter( |
101 | | - $parameter->getName(), |
102 | | - $this->transformStaticType($parameter->getType()), |
103 | | - $parameter->isOptional(), |
104 | | - $parameter->passedByReference(), |
105 | | - $parameter->isVariadic(), |
106 | | - $parameter->getDefaultValue(), |
107 | | - $parameter->getNativeType(), |
108 | | - $this->transformStaticType($parameter->getPhpDocType()), |
109 | | - $parameter->getOutType() !== null ? $this->transformStaticType($parameter->getOutType()) : null, |
110 | | - $parameter->isImmediatelyInvokedCallable(), |
111 | | - $parameter->getClosureThisType() !== null ? $this->transformStaticType($parameter->getClosureThisType()) : null, |
112 | | - $parameter->getAttributes(), |
113 | | - ), |
114 | | - $acceptor->getParameters(), |
115 | | - ), |
116 | | - $acceptor->isVariadic(), |
117 | | - $returnType, |
118 | | - $this->transformStaticType($acceptor->getPhpDocReturnType()), |
119 | | - $this->transformStaticType($acceptor->getNativeReturnType()), |
120 | | - $acceptor->getCallSiteVarianceMap(), |
121 | | - ); |
122 | | - }; |
123 | | - $variants = array_map($variantFn, $method->getVariants()); |
| 86 | + |
| 87 | + $variants = []; |
| 88 | + foreach ($method->getVariants() as $variant) { |
| 89 | + $variants[] = $this->transformVariant($variant, $selfOutType); |
| 90 | + } |
| 91 | + |
| 92 | + $namedVariants = null; |
124 | 93 | $namedArgumentVariants = $method->getNamedArgumentsVariants(); |
125 | | - $namedArgumentVariants = $namedArgumentVariants !== null |
126 | | - ? array_map($variantFn, $namedArgumentVariants) |
127 | | - : null; |
| 94 | + if ($namedArgumentVariants !== null) { |
| 95 | + $namedVariants = []; |
| 96 | + foreach ($namedArgumentVariants as $namedArgumentVariant) { |
| 97 | + $namedVariants[] = $this->transformVariant($namedArgumentVariant, $selfOutType); |
| 98 | + } |
| 99 | + } |
128 | 100 |
|
129 | 101 | return new ChangedTypeMethodReflection( |
130 | 102 | $declaringClass, |
131 | 103 | $method, |
132 | 104 | $variants, |
133 | | - $namedArgumentVariants, |
| 105 | + $namedVariants, |
134 | 106 | $selfOutType, |
135 | 107 | ); |
136 | 108 | } |
137 | 109 |
|
| 110 | + private function transformVariant(ExtendedParametersAcceptor $acceptor, ?Type &$selfOutType): ExtendedParametersAcceptor |
| 111 | + { |
| 112 | + $originalReturnType = $acceptor->getReturnType(); |
| 113 | + if ($originalReturnType instanceof ThisType && $selfOutType !== null) { |
| 114 | + $returnType = TypeCombinator::intersect($selfOutType, $this->transformStaticType($originalReturnType)); |
| 115 | + $selfOutType = $returnType; |
| 116 | + } else { |
| 117 | + $returnType = $this->transformStaticType($originalReturnType); |
| 118 | + } |
| 119 | + |
| 120 | + $parameters = []; |
| 121 | + foreach ($acceptor->getParameters() as $parameter) { |
| 122 | + $parameters[] = new ExtendedDummyParameter( |
| 123 | + $parameter->getName(), |
| 124 | + $this->transformStaticType($parameter->getType()), |
| 125 | + $parameter->isOptional(), |
| 126 | + $parameter->passedByReference(), |
| 127 | + $parameter->isVariadic(), |
| 128 | + $parameter->getDefaultValue(), |
| 129 | + $parameter->getNativeType(), |
| 130 | + $this->transformStaticType($parameter->getPhpDocType()), |
| 131 | + $parameter->getOutType() !== null ? $this->transformStaticType($parameter->getOutType()) : null, |
| 132 | + $parameter->isImmediatelyInvokedCallable(), |
| 133 | + $parameter->getClosureThisType() !== null ? $this->transformStaticType($parameter->getClosureThisType()) : null, |
| 134 | + $parameter->getAttributes(), |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + return new ExtendedFunctionVariant( |
| 139 | + $acceptor->getTemplateTypeMap(), |
| 140 | + $acceptor->getResolvedTemplateTypeMap(), |
| 141 | + $parameters, |
| 142 | + $acceptor->isVariadic(), |
| 143 | + $returnType, |
| 144 | + $this->transformStaticType($acceptor->getPhpDocReturnType()), |
| 145 | + $this->transformStaticType($acceptor->getNativeReturnType()), |
| 146 | + $acceptor->getCallSiteVarianceMap(), |
| 147 | + ); |
| 148 | + } |
| 149 | + |
138 | 150 | private function transformStaticType(Type $type): Type |
139 | 151 | { |
140 | 152 | $callback = $this->transformStaticTypeCallback; |
|
0 commit comments