16
16
final class ConstantExtractor
17
17
{
18
18
/**
19
- * @param string $pattern
20
- *
21
- * @return array
22
19
* @throws LogicException
23
20
*/
24
21
public static function extract (string $ pattern ): array
@@ -34,21 +31,21 @@ public static function extract(string $pattern): array
34
31
35
32
private static function filter (array $ constants , string $ regexp , string $ pattern ): array
36
33
{
37
- $ matchingNames = preg_grep ($ regexp , array_keys ($ constants ));
34
+ $ matchingNames = \ preg_grep ($ regexp , \ array_keys ($ constants ));
38
35
39
- if (count ($ matchingNames ) === 0 ) {
36
+ if (\ count ($ matchingNames ) === 0 ) {
40
37
throw LogicException::cannotExtractConstants ($ pattern , 'Pattern matches no constant. ' );
41
38
}
42
39
43
- return array_values (array_intersect_key ($ constants , array_flip ($ matchingNames )));
40
+ return \ array_values (\ array_intersect_key ($ constants , \ array_flip ($ matchingNames )));
44
41
}
45
42
46
43
private static function publicConstants (string $ class , string $ pattern ): array
47
44
{
48
45
try {
49
46
$ constants = (new ReflectionClass ($ class ))->getReflectionConstants ();
50
47
} catch (ReflectionException $ exception ) {
51
- throw LogicException::cannotExtractConstants ($ pattern , sprintf ('Class %s does not exists. ' , $ class ));
48
+ throw LogicException::cannotExtractConstants ($ pattern , \ sprintf ('Class %s does not exists. ' , $ class ));
52
49
}
53
50
54
51
$ list = [];
@@ -60,34 +57,37 @@ private static function publicConstants(string $class, string $pattern): array
60
57
$ list [$ constant ->getName ()] = $ constant ->getValue ();
61
58
}
62
59
63
- if (count ($ list ) === 0 ) {
64
- throw LogicException::cannotExtractConstants ($ pattern , sprintf ('Class %s has no public constant. ' , $ class ));
60
+ if (\count ($ list ) === 0 ) {
61
+ throw LogicException::cannotExtractConstants (
62
+ $ pattern ,
63
+ \sprintf ('Class %s has no public constant. ' , $ class )
64
+ );
65
65
}
66
66
67
67
return $ list ;
68
68
}
69
69
70
70
private static function explode (string $ pattern ): array
71
71
{
72
- if (substr_count ($ pattern , ':: ' ) !== 1 ) {
72
+ if (\ substr_count ($ pattern , ':: ' ) !== 1 ) {
73
73
throw LogicException::cannotExtractConstants (
74
74
$ pattern ,
75
75
'Pattern must look like Fully \\Qualified \\ClassName::CONSTANT_*. '
76
76
);
77
77
}
78
78
79
- [$ class , $ constantsNamePattern ] = explode (':: ' , $ pattern );
79
+ [$ class , $ constantsNamePattern ] = \ explode (':: ' , $ pattern );
80
80
81
- if (substr_count ($ constantsNamePattern , '* ' ) === 0 ) {
81
+ if (\ substr_count ($ constantsNamePattern , '* ' ) === 0 ) {
82
82
throw LogicException::cannotExtractConstants (
83
83
$ pattern ,
84
84
'Pattern must look like Fully \\Qualified \\ClassName::CONSTANT_*. '
85
85
);
86
86
}
87
87
88
- $ constantsNameRegexp = sprintf (
88
+ $ constantsNameRegexp = \ sprintf (
89
89
'#^%s$# ' ,
90
- str_replace ('* ' , '[0-9a-zA-Z_]+ ' , $ constantsNamePattern )
90
+ \ str_replace ('* ' , '[0-9a-zA-Z_]+ ' , $ constantsNamePattern )
91
91
);
92
92
93
93
return [$ class , $ constantsNameRegexp ];
0 commit comments