99use PhpSpec \Locator \ResourceLocator ;
1010use PhpSpec \ObjectBehavior ;
1111use PhpSpec \Util \Filesystem ;
12- use PhpSpec \Wrapper \Subject ;
1312use PHPStan \Analyser \OutOfClassScope ;
1413use PHPStan \Broker \Broker ;
1514use PHPStan \Reflection \BrokerAwareExtension ;
1615use PHPStan \Reflection \ClassReflection ;
1716use PHPStan \Reflection \MethodReflection ;
1817use PHPStan \Reflection \MethodsClassReflectionExtension ;
19- use PHPStan \Type \ObjectType ;
2018use Proget \PHPStan \PhpSpec \Exception \SpecSourceClassNotFound ;
19+ use Proget \PHPStan \PhpSpec \Type \SubjectType ;
2120
2221final class ObjectBehaviorMethodsClassReflectionExtension implements MethodsClassReflectionExtension, BrokerAwareExtension
2322{
@@ -63,7 +62,13 @@ public function getMethod(ClassReflection $classReflection, string $methodName):
6362 if (count ($ resources ) === 0 ) {
6463 throw new SpecSourceClassNotFound (sprintf ('Source class from %s not found ' , $ classReflection ->getName ()));
6564 }
66- $ srcClassReflection = $ this ->broker ->getClass ($ resources [0 ]->getSrcClassname ());
65+
66+ $ className = $ resources [0 ]->getSrcClassname ();
67+ if (!class_exists ($ className )) {
68+ throw new SpecSourceClassNotFound (sprintf ('Spec source class %s not found ' , $ className ));
69+ }
70+
71+ $ srcClassReflection = $ this ->broker ->getClass ($ className );
6772
6873 $ method = $ srcClassReflection ->getNativeMethod ($ methodName );
6974 $ this ->replaceReturnType ($ method );
@@ -76,11 +81,11 @@ private function replaceReturnType(MethodReflection $method): void
7681 $ methodReflection = new \ReflectionClass ($ method );
7782 $ returnType = $ methodReflection ->getProperty ('nativeReturnType ' );
7883 $ returnType ->setAccessible (true );
79- $ returnType ->setValue ($ method , new ObjectType (Subject::class ));
84+ $ returnType ->setValue ($ method , new SubjectType ( $ returnType -> getValue ( $ method ) ));
8085
8186 $ nativeReturnType = $ methodReflection ->getProperty ('returnType ' );
8287 $ nativeReturnType ->setAccessible (true );
83- $ nativeReturnType ->setValue ($ method , new ObjectType (Subject::class ));
88+ $ nativeReturnType ->setValue ($ method , new SubjectType ( $ nativeReturnType -> getValue ( $ method ) ));
8489
8590 $ variants = $ methodReflection ->getProperty ('variants ' );
8691 $ variants ->setAccessible (true );
0 commit comments