66use PHPStan \Analyser \Scope ;
77use PHPStan \DependencyInjection \AutowiredParameter ;
88use PHPStan \DependencyInjection \AutowiredService ;
9+ use PHPStan \Reflection \ParametersAcceptor ;
910use PHPStan \Reflection \ReflectionProvider ;
1011use PHPStan \Type \BenevolentUnionType ;
1112use PHPStan \Type \CallableType ;
1617use PHPStan \Type \MixedType ;
1718use PHPStan \Type \NeverType ;
1819use PHPStan \Type \NullType ;
20+ use PHPStan \Type \SimultaneousTypeTraverser ;
1921use PHPStan \Type \StrictMixedType ;
2022use PHPStan \Type \Type ;
2123use PHPStan \Type \TypeCombinator ;
@@ -87,15 +89,18 @@ private function transformCommonType(Type $type): Type
8789 private function transformAcceptedType (Type $ acceptingType , Type $ acceptedType ): array
8890 {
8991 $ checkForUnion = $ this ->checkUnionTypes ;
90- $ acceptedType = TypeTraverser ::map ($ acceptedType , function (Type $ acceptedType , callable $ traverse ) use ($ acceptingType , &$ checkForUnion ): Type {
92+ $ acceptedType = SimultaneousTypeTraverser ::map ($ acceptedType , $ acceptingType , function (Type $ acceptedType , Type $ acceptingType , callable $ traverse ) use (&$ checkForUnion ): Type {
9193 if ($ acceptedType instanceof CallableType) {
9294 if ($ acceptedType ->isCommonCallable ()) {
9395 return $ acceptedType ;
9496 }
97+ if (!$ acceptingType instanceof ParametersAcceptor) {
98+ return $ acceptedType ;
99+ }
95100
96101 return new CallableType (
97102 $ acceptedType ->getParameters (),
98- $ traverse ($ this ->transformCommonType ($ acceptedType ->getReturnType ())),
103+ $ traverse ($ this ->transformCommonType ($ acceptedType ->getReturnType ()), $ acceptingType -> getReturnType () ),
99104 $ acceptedType ->isVariadic (),
100105 $ acceptedType ->getTemplateTypeMap (),
101106 $ acceptedType ->getResolvedTemplateTypeMap (),
@@ -109,9 +114,13 @@ private function transformAcceptedType(Type $acceptingType, Type $acceptedType):
109114 return $ acceptedType ;
110115 }
111116
117+ if (!$ acceptingType instanceof ParametersAcceptor) {
118+ return $ acceptedType ;
119+ }
120+
112121 return new ClosureType (
113122 $ acceptedType ->getParameters (),
114- $ traverse ($ this ->transformCommonType ($ acceptedType ->getReturnType ())),
123+ $ traverse ($ this ->transformCommonType ($ acceptedType ->getReturnType ()), $ acceptingType -> getReturnType () ),
115124 $ acceptedType ->isVariadic (),
116125 $ acceptedType ->getTemplateTypeMap (),
117126 $ acceptedType ->getResolvedTemplateTypeMap (),
@@ -128,21 +137,22 @@ private function transformAcceptedType(Type $acceptingType, Type $acceptedType):
128137
129138 if (
130139 !$ this ->checkNullables
131- && !$ acceptingType instanceof NullType
132- && !$ acceptedType instanceof NullType
133140 && !$ acceptedType instanceof BenevolentUnionType
141+ && !$ acceptedType instanceof NullType
142+ && TypeCombinator::containsNull ($ acceptedType )
143+ && !TypeCombinator::containsNull ($ acceptingType )
134144 ) {
135- return $ traverse (TypeCombinator::removeNull ($ acceptedType ));
145+ return $ traverse (TypeCombinator::removeNull ($ acceptedType ), $ acceptingType );
136146 }
137147
138148 if ($ this ->checkBenevolentUnionTypes ) {
139149 if ($ acceptedType instanceof BenevolentUnionType) {
140150 $ checkForUnion = true ;
141- return $ traverse (TypeUtils::toStrictUnion ($ acceptedType ));
151+ return $ traverse (TypeUtils::toStrictUnion ($ acceptedType ), $ acceptingType );
142152 }
143153 }
144154
145- return $ traverse ($ this ->transformCommonType ($ acceptedType ));
155+ return $ traverse ($ this ->transformCommonType ($ acceptedType ), $ acceptingType );
146156 });
147157
148158 return [$ acceptedType , $ checkForUnion ];
0 commit comments