|
12 | 12 | use PHPStan\Reflection\ResolvedMethodReflection;
|
13 | 13 | use PHPStan\Type\Generic\GenericStaticType;
|
14 | 14 | use PHPStan\Type\StaticType;
|
| 15 | +use PHPStan\Type\ThisType; |
15 | 16 | use PHPStan\Type\Type;
|
16 | 17 | use PHPStan\Type\TypeTraverser;
|
17 | 18 | use function array_map;
|
@@ -79,39 +80,54 @@ public function withCalledOnType(Type $type): UnresolvedMethodPrototypeReflectio
|
79 | 80 |
|
80 | 81 | private function transformMethodWithStaticType(ClassReflection $declaringClass, ExtendedMethodReflection $method): ExtendedMethodReflection
|
81 | 82 | {
|
82 |
| - $variantFn = fn (ExtendedParametersAcceptor $acceptor): ExtendedParametersAcceptor => new ExtendedFunctionVariant( |
83 |
| - $acceptor->getTemplateTypeMap(), |
84 |
| - $acceptor->getResolvedTemplateTypeMap(), |
85 |
| - array_map( |
86 |
| - fn (ExtendedParameterReflection $parameter): ExtendedParameterReflection => new ExtendedDummyParameter( |
87 |
| - $parameter->getName(), |
88 |
| - $this->transformStaticType($parameter->getType()), |
89 |
| - $parameter->isOptional(), |
90 |
| - $parameter->passedByReference(), |
91 |
| - $parameter->isVariadic(), |
92 |
| - $parameter->getDefaultValue(), |
93 |
| - $parameter->getNativeType(), |
94 |
| - $this->transformStaticType($parameter->getPhpDocType()), |
95 |
| - $parameter->getOutType() !== null ? $this->transformStaticType($parameter->getOutType()) : null, |
96 |
| - $parameter->isImmediatelyInvokedCallable(), |
97 |
| - $parameter->getClosureThisType() !== null ? $this->transformStaticType($parameter->getClosureThisType()) : null, |
98 |
| - $parameter->getAttributes(), |
| 83 | + $selfOutType = $method->getSelfOutType() !== null ? $this->transformStaticType($method->getSelfOutType()) : null; |
| 84 | + $variantFn = function (ExtendedParametersAcceptor $acceptor) use ($selfOutType): ExtendedParametersAcceptor { |
| 85 | + $originalReturnType = $acceptor->getReturnType(); |
| 86 | + if ($originalReturnType instanceof ThisType && $selfOutType !== null) { |
| 87 | + $returnType = $selfOutType; |
| 88 | + } else { |
| 89 | + $returnType = $this->transformStaticType($originalReturnType); |
| 90 | + } |
| 91 | + return new ExtendedFunctionVariant( |
| 92 | + $acceptor->getTemplateTypeMap(), |
| 93 | + $acceptor->getResolvedTemplateTypeMap(), |
| 94 | + array_map( |
| 95 | + fn (ExtendedParameterReflection $parameter): ExtendedParameterReflection => new ExtendedDummyParameter( |
| 96 | + $parameter->getName(), |
| 97 | + $this->transformStaticType($parameter->getType()), |
| 98 | + $parameter->isOptional(), |
| 99 | + $parameter->passedByReference(), |
| 100 | + $parameter->isVariadic(), |
| 101 | + $parameter->getDefaultValue(), |
| 102 | + $parameter->getNativeType(), |
| 103 | + $this->transformStaticType($parameter->getPhpDocType()), |
| 104 | + $parameter->getOutType() !== null ? $this->transformStaticType($parameter->getOutType()) : null, |
| 105 | + $parameter->isImmediatelyInvokedCallable(), |
| 106 | + $parameter->getClosureThisType() !== null ? $this->transformStaticType($parameter->getClosureThisType()) : null, |
| 107 | + $parameter->getAttributes(), |
| 108 | + ), |
| 109 | + $acceptor->getParameters(), |
99 | 110 | ),
|
100 |
| - $acceptor->getParameters(), |
101 |
| - ), |
102 |
| - $acceptor->isVariadic(), |
103 |
| - $this->transformStaticType($acceptor->getReturnType()), |
104 |
| - $this->transformStaticType($acceptor->getPhpDocReturnType()), |
105 |
| - $this->transformStaticType($acceptor->getNativeReturnType()), |
106 |
| - $acceptor->getCallSiteVarianceMap(), |
107 |
| - ); |
| 111 | + $acceptor->isVariadic(), |
| 112 | + $returnType, |
| 113 | + $this->transformStaticType($acceptor->getPhpDocReturnType()), |
| 114 | + $this->transformStaticType($acceptor->getNativeReturnType()), |
| 115 | + $acceptor->getCallSiteVarianceMap(), |
| 116 | + ); |
| 117 | + }; |
108 | 118 | $variants = array_map($variantFn, $method->getVariants());
|
109 | 119 | $namedArgumentsVariants = $method->getNamedArgumentsVariants();
|
110 | 120 | $namedArgumentsVariants = $namedArgumentsVariants !== null
|
111 | 121 | ? array_map($variantFn, $namedArgumentsVariants)
|
112 | 122 | : null;
|
113 | 123 |
|
114 |
| - return new ChangedTypeMethodReflection($declaringClass, $method, $variants, $namedArgumentsVariants); |
| 124 | + return new ChangedTypeMethodReflection( |
| 125 | + $declaringClass, |
| 126 | + $method, |
| 127 | + $variants, |
| 128 | + $namedArgumentsVariants, |
| 129 | + $selfOutType, |
| 130 | + ); |
115 | 131 | }
|
116 | 132 |
|
117 | 133 | private function transformStaticType(Type $type): Type
|
|
0 commit comments