44
55use PhpParser \Node \Expr ;
66use PHPStan \Analyser \Scope ;
7- use PHPStan \Type \Constant \ ConstantStringType ;
7+ use PHPStan \Type \TypeUtils ;
88
99final class ServiceMap
1010{
1111
12- /** @var array<string, array> */
13- private $ services ;
12+ /** @var Service[] */
13+ private $ services = [] ;
1414
1515 public function __construct (string $ containerXml )
1616 {
17- $ this ->services = $ aliases = [];
17+ /** @var Service[] $aliases */
18+ $ aliases = [];
1819 /** @var \SimpleXMLElement|false $xml */
1920 $ xml = @simplexml_load_file ($ containerXml );
2021 if ($ xml === false ) {
@@ -25,44 +26,42 @@ public function __construct(string $containerXml)
2526 if (!isset ($ attrs ->id )) {
2627 continue ;
2728 }
28- $ service = [
29- 'id ' => (string ) $ attrs ->id ,
30- 'class ' => isset ($ attrs ->class ) ? (string ) $ attrs ->class : null ,
31- 'public ' => !isset ($ attrs ->public ) || (string ) $ attrs ->public !== 'false ' ,
32- 'synthetic ' => isset ($ attrs ->synthetic ) && (string ) $ attrs ->synthetic === 'true ' ,
33- ];
34- if (isset ($ attrs ->alias )) {
35- $ aliases [(string ) $ attrs ->id ] = array_merge ($ service , ['alias ' => (string ) $ attrs ->alias ]);
29+ $ service = new Service (
30+ (string ) $ attrs ->id ,
31+ isset ($ attrs ->class ) ? (string ) $ attrs ->class : null ,
32+ !isset ($ attrs ->public ) || (string ) $ attrs ->public !== 'false ' ,
33+ isset ($ attrs ->synthetic ) && (string ) $ attrs ->synthetic === 'true ' ,
34+ isset ($ attrs ->alias ) ? (string ) $ attrs ->alias : null
35+ );
36+ if ($ service ->getAlias () !== null ) {
37+ $ aliases [] = $ service ;
3638 } else {
37- $ this ->services [( string ) $ attrs -> id ] = $ service ;
39+ $ this ->services [$ service -> getId () ] = $ service ;
3840 }
3941 }
40- foreach ($ aliases as $ id => $ alias ) {
41- if (! array_key_exists ($ alias [ ' alias ' ] , $ this ->services )) {
42+ foreach ($ aliases as $ service ) {
43+ if ($ service -> getAlias () !== null && ! array_key_exists ($ service -> getAlias () , $ this ->services )) {
4244 continue ;
4345 }
44- $ this ->services [$ id ] = [
45- 'id ' => $ id ,
46- 'class ' => $ this ->services [$ alias ['alias ' ]]['class ' ],
47- 'public ' => $ alias ['public ' ],
48- 'synthetic ' => $ alias ['synthetic ' ],
49- ];
46+ $ this ->services [$ service ->getId ()] = new Service (
47+ $ service ->getId (),
48+ $ this ->services [$ service ->getAlias ()]->getClass (),
49+ $ service ->isPublic (),
50+ $ service ->isSynthetic (),
51+ null
52+ );
5053 }
5154 }
5255
53- /**
54- * @param string $id
55- * @return mixed[]|null
56- */
57- public function getService (string $ id ): ?array
56+ public function getService (string $ id ): ?Service
5857 {
5958 return $ this ->services [$ id ] ?? null ;
6059 }
6160
6261 public static function getServiceIdFromNode (Expr $ node , Scope $ scope ): ?string
6362 {
64- $ nodeType = $ scope ->getType ($ node );
65- return $ nodeType instanceof ConstantStringType ? $ nodeType ->getValue () : null ;
63+ $ strings = TypeUtils:: getConstantStrings ( $ scope ->getType ($ node) );
64+ return count ( $ strings ) === 1 ? $ strings [ 0 ] ->getValue () : null ;
6665 }
6766
6867}
0 commit comments