4
4
5
5
use PhpParser \Node \Expr ;
6
6
use PHPStan \Analyser \Scope ;
7
- use PHPStan \Type \Constant \ ConstantStringType ;
7
+ use PHPStan \Type \TypeUtils ;
8
8
9
9
final class ServiceMap
10
10
{
11
11
12
- /** @var array<string, array> */
13
- private $ services ;
12
+ /** @var Service[] */
13
+ private $ services = [] ;
14
14
15
15
public function __construct (string $ containerXml )
16
16
{
17
- $ this ->services = $ aliases = [];
17
+ /** @var Service[] $aliases */
18
+ $ aliases = [];
18
19
/** @var \SimpleXMLElement|false $xml */
19
20
$ xml = @simplexml_load_file ($ containerXml );
20
21
if ($ xml === false ) {
@@ -25,44 +26,42 @@ public function __construct(string $containerXml)
25
26
if (!isset ($ attrs ->id )) {
26
27
continue ;
27
28
}
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 ;
36
38
} else {
37
- $ this ->services [( string ) $ attrs -> id ] = $ service ;
39
+ $ this ->services [$ service -> getId () ] = $ service ;
38
40
}
39
41
}
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 )) {
42
44
continue ;
43
45
}
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
+ );
50
53
}
51
54
}
52
55
53
- /**
54
- * @param string $id
55
- * @return mixed[]|null
56
- */
57
- public function getService (string $ id ): ?array
56
+ public function getService (string $ id ): ?Service
58
57
{
59
58
return $ this ->services [$ id ] ?? null ;
60
59
}
61
60
62
61
public static function getServiceIdFromNode (Expr $ node , Scope $ scope ): ?string
63
62
{
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 ;
66
65
}
67
66
68
67
}
0 commit comments