55namespace Proget \PHPStan \Yii2 ;
66
77use PhpParser \Node ;
8+ use yii \base \BaseObject ;
89
910final class ServiceMap
1011{
@@ -33,18 +34,19 @@ public function __construct(string $configPath)
3334 foreach ($ config ['container ' ]['singletons ' ] ?? [] as $ id => $ service ) {
3435 $ this ->addServiceDefinition ($ id , $ service );
3536 }
37+
3638 foreach ($ config ['container ' ]['definitions ' ] ?? [] as $ id => $ service ) {
3739 $ this ->addServiceDefinition ($ id , $ service );
3840 }
3941
4042 foreach ($ config ['components ' ] ?? [] as $ id => $ component ) {
41- if (is_object ($ component )) {
43+ if (\ is_object ($ component )) {
4244 $ this ->components [$ id ] = \get_class ($ component );
4345 continue ;
4446 }
4547
46- if (!is_array ($ component )) {
47- throw new \RuntimeException (sprintf ('Invalid value for component with id %s. Expected object or array. ' , $ id ));
48+ if (!\ is_array ($ component )) {
49+ throw new \RuntimeException (\ sprintf ('Invalid value for component with id %s. Expected object or array. ' , $ id ));
4850 }
4951
5052 if (null !== $ class = $ component ['class ' ] ?? null ) {
@@ -70,19 +72,49 @@ public function getComponentClassById(string $id): ?string
7072 /**
7173 * @param string|\Closure|array<mixed> $service
7274 *
73- * @throws \ReflectionException
75+ * @throws \RuntimeException|\ ReflectionException
7476 */
7577 private function addServiceDefinition (string $ id , $ service ): void
7678 {
79+ $ this ->services [$ id ] = $ this ->guessServiceDefinition ($ id , $ service );
80+ }
81+
82+ /**
83+ * @param string|\Closure|array<mixed> $service
84+ *
85+ * @throws \RuntimeException|\ReflectionException
86+ */
87+ private function guessServiceDefinition (string $ id , $ service ): string
88+ {
89+ if (\is_string ($ service ) && \class_exists ($ service )) {
90+ return $ service ;
91+ }
92+
7793 if ($ service instanceof \Closure || \is_string ($ service )) {
7894 $ returnType = (new \ReflectionFunction ($ service ))->getReturnType ();
7995 if (!$ returnType instanceof \ReflectionNamedType) {
80- throw new \RuntimeException (sprintf ('Please provide return type for %s service closure ' , $ id ));
96+ throw new \RuntimeException (\ sprintf ('Please provide return type for %s service closure ' , $ id ));
8197 }
8298
83- $ this ->services [$ id ] = $ returnType ->getName ();
84- } else {
85- $ this ->services [$ id ] = $ service ['class ' ] ?? $ service [0 ]['class ' ];
99+ return $ returnType ->getName ();
86100 }
101+
102+ if (!\is_array ($ service )) {
103+ throw new \RuntimeException (\sprintf ('Unsupported service definition for %s ' , $ id ));
104+ }
105+
106+ if (isset ($ service ['class ' ])) {
107+ return $ service ['class ' ];
108+ }
109+
110+ if (isset ($ service [0 ]['class ' ])) {
111+ return $ service [0 ]['class ' ];
112+ }
113+
114+ if (\is_subclass_of ($ id , BaseObject::class)) {
115+ return $ id ;
116+ }
117+
118+ throw new \RuntimeException (\sprintf ('Cannot guess service definition for %s ' , $ id ));
87119 }
88120}
0 commit comments