12
12
13
13
namespace Composer \ClassMapGenerator ;
14
14
15
+ use RuntimeException ;
15
16
use Composer \Pcre \Preg ;
16
17
17
18
/**
@@ -23,16 +24,17 @@ class PhpFileParser
23
24
* Extract the classes in the given file
24
25
*
25
26
* @param string $path The file to check
26
- * @throws \ RuntimeException
27
+ * @throws RuntimeException
27
28
* @return list<class-string> The found classes
28
29
*/
29
30
public static function findClasses (string $ path ): array
30
31
{
31
32
$ extraTypes = self ::getExtraTypes ();
32
33
33
34
if (!function_exists ('php_strip_whitespace ' )) {
34
- throw new \ RuntimeException ('Classmap generation relies on the php_strip_whitespace function, but it has been disabled by the disable_functions directive. ' );
35
+ throw new RuntimeException ('Classmap generation relies on the php_strip_whitespace function, but it has been disabled by the disable_functions directive. ' );
35
36
}
37
+
36
38
// Use @ here instead of Silencer to actively suppress 'unhelpful' output
37
39
// @link https://github.com/composer/composer/pull/4886
38
40
$ contents = @php_strip_whitespace ($ path );
@@ -43,21 +45,23 @@ public static function findClasses(string $path): array
43
45
$ message = 'File at "%s" is not readable, check its permissions ' ;
44
46
} elseif ('' === trim ((string ) file_get_contents ($ path ))) {
45
47
// The input file was really empty and thus contains no classes
46
- return array () ;
48
+ return [] ;
47
49
} else {
48
50
$ message = 'File at "%s" could not be parsed as PHP, it may be binary or corrupted ' ;
49
51
}
52
+
50
53
$ error = error_get_last ();
51
54
if (isset ($ error ['message ' ])) {
52
55
$ message .= PHP_EOL . 'The following message may be helpful: ' . PHP_EOL . $ error ['message ' ];
53
56
}
54
- throw new \RuntimeException (sprintf ($ message , $ path ));
57
+
58
+ throw new RuntimeException (sprintf ($ message , $ path ));
55
59
}
56
60
57
61
// return early if there is no chance of matching anything in this file
58
62
Preg::matchAllStrictGroups ('{\b(?:class|interface|trait ' .$ extraTypes .')\s}i ' , $ contents , $ matches );
59
- if (0 === \count ( $ matches) ) {
60
- return array () ;
63
+ if ([] === $ matches ) {
64
+ return [] ;
61
65
}
62
66
63
67
$ p = new PhpFileCleaner ($ contents , count ($ matches [0 ]));
@@ -71,22 +75,26 @@ public static function findClasses(string $path): array
71
75
)
72
76
}ix ' , $ contents , $ matches );
73
77
74
- $ classes = array () ;
78
+ $ classes = [] ;
75
79
$ namespace = '' ;
76
80
77
- for ($ i = 0 , $ len = count ($ matches ['type ' ]); $ i < $ len ; $ i ++ ) {
81
+ for ($ i = 0 , $ len = count ($ matches ['type ' ]); $ i < $ len ; ++ $ i ) {
78
82
if (isset ($ matches ['ns ' ][$ i ]) && $ matches ['ns ' ][$ i ] !== '' ) {
79
- $ namespace = str_replace (array ( ' ' , "\t" , "\r" , "\n" ) , '' , (string ) $ matches ['nsname ' ][$ i ]) . '\\' ;
83
+ $ namespace = str_replace ([ ' ' , "\t" , "\r" , "\n" ] , '' , (string ) $ matches ['nsname ' ][$ i ]) . '\\' ;
80
84
} else {
81
85
$ name = $ matches ['name ' ][$ i ];
82
86
assert (is_string ($ name ));
83
87
// skip anon classes extending/implementing
84
- if ($ name === 'extends ' || $ name === 'implements ' ) {
88
+ if ($ name === 'extends ' ) {
89
+ continue ;
90
+ }
91
+ if ($ name === 'implements ' ) {
85
92
continue ;
86
93
}
94
+
87
95
if ($ name [0 ] === ': ' ) {
88
96
// This is an XHP class, https://github.com/facebook/xhp
89
- $ name = 'xhp ' .substr (str_replace (array ( '- ' , ': ' ), array ( '_ ' , '__ ' ) , $ name ), 1 );
97
+ $ name = 'xhp ' .substr (str_replace ([ '- ' , ': ' ], [ '_ ' , '__ ' ] , $ name ), 1 );
90
98
} elseif (strtolower ((string ) $ matches ['type ' ][$ i ]) === 'enum ' ) {
91
99
// something like:
92
100
// enum Foo: int { HERP = '123'; }
@@ -101,6 +109,7 @@ public static function findClasses(string $path): array
101
109
$ name = substr ($ name , 0 , $ colonPos );
102
110
}
103
111
}
112
+
104
113
/** @var class-string */
105
114
$ className = ltrim ($ namespace . $ name , '\\' );
106
115
$ classes [] = $ className ;
@@ -110,9 +119,6 @@ public static function findClasses(string $path): array
110
119
return $ classes ;
111
120
}
112
121
113
- /**
114
- * @return string
115
- */
116
122
private static function getExtraTypes (): string
117
123
{
118
124
static $ extraTypes = null ;
@@ -123,7 +129,7 @@ private static function getExtraTypes(): string
123
129
$ extraTypes .= '|enum ' ;
124
130
}
125
131
126
- $ extraTypesArray = array_filter (explode ('| ' , $ extraTypes ), function (string $ type ) {
132
+ $ extraTypesArray = array_filter (explode ('| ' , $ extraTypes ), function (string $ type ): bool {
127
133
return $ type !== '' ;
128
134
});
129
135
PhpFileCleaner::setTypeConfig (array_merge (['class ' , 'interface ' , 'trait ' ], $ extraTypesArray ));
@@ -140,7 +146,6 @@ private static function getExtraTypes(): string
140
146
*
141
147
* @see Composer\Util\Filesystem::isReadable
142
148
*
143
- * @param string $path
144
149
* @return bool
145
150
*/
146
151
private static function isReadable (string $ path )
0 commit comments