1212
1313namespace Composer \ClassMapGenerator ;
1414
15+ use RuntimeException ;
1516use Composer \Pcre \Preg ;
1617
1718/**
@@ -23,16 +24,17 @@ class PhpFileParser
2324 * Extract the classes in the given file
2425 *
2526 * @param string $path The file to check
26- * @throws \ RuntimeException
27+ * @throws RuntimeException
2728 * @return list<class-string> The found classes
2829 */
2930 public static function findClasses (string $ path ): array
3031 {
3132 $ extraTypes = self ::getExtraTypes ();
3233
3334 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. ' );
3536 }
37+
3638 // Use @ here instead of Silencer to actively suppress 'unhelpful' output
3739 // @link https://github.com/composer/composer/pull/4886
3840 $ contents = @php_strip_whitespace ($ path );
@@ -43,21 +45,23 @@ public static function findClasses(string $path): array
4345 $ message = 'File at "%s" is not readable, check its permissions ' ;
4446 } elseif ('' === trim ((string ) file_get_contents ($ path ))) {
4547 // The input file was really empty and thus contains no classes
46- return array () ;
48+ return [] ;
4749 } else {
4850 $ message = 'File at "%s" could not be parsed as PHP, it may be binary or corrupted ' ;
4951 }
52+
5053 $ error = error_get_last ();
5154 if (isset ($ error ['message ' ])) {
5255 $ message .= PHP_EOL . 'The following message may be helpful: ' . PHP_EOL . $ error ['message ' ];
5356 }
54- throw new \RuntimeException (sprintf ($ message , $ path ));
57+
58+ throw new RuntimeException (sprintf ($ message , $ path ));
5559 }
5660
5761 // return early if there is no chance of matching anything in this file
5862 Preg::matchAllStrictGroups ('{\b(?:class|interface|trait ' .$ extraTypes .')\s}i ' , $ contents , $ matches );
59- if (0 === \count ( $ matches) ) {
60- return array () ;
63+ if ([] === $ matches ) {
64+ return [] ;
6165 }
6266
6367 $ p = new PhpFileCleaner ($ contents , count ($ matches [0 ]));
@@ -71,22 +75,26 @@ public static function findClasses(string $path): array
7175 )
7276 }ix ' , $ contents , $ matches );
7377
74- $ classes = array () ;
78+ $ classes = [] ;
7579 $ namespace = '' ;
7680
77- for ($ i = 0 , $ len = count ($ matches ['type ' ]); $ i < $ len ; $ i ++ ) {
81+ for ($ i = 0 , $ len = count ($ matches ['type ' ]); $ i < $ len ; ++ $ i ) {
7882 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 ]) . '\\' ;
8084 } else {
8185 $ name = $ matches ['name ' ][$ i ];
8286 assert (is_string ($ name ));
8387 // skip anon classes extending/implementing
84- if ($ name === 'extends ' || $ name === 'implements ' ) {
88+ if ($ name === 'extends ' ) {
89+ continue ;
90+ }
91+ if ($ name === 'implements ' ) {
8592 continue ;
8693 }
94+
8795 if ($ name [0 ] === ': ' ) {
8896 // 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 );
9098 } elseif (strtolower ((string ) $ matches ['type ' ][$ i ]) === 'enum ' ) {
9199 // something like:
92100 // enum Foo: int { HERP = '123'; }
@@ -101,6 +109,7 @@ public static function findClasses(string $path): array
101109 $ name = substr ($ name , 0 , $ colonPos );
102110 }
103111 }
112+
104113 /** @var class-string */
105114 $ className = ltrim ($ namespace . $ name , '\\' );
106115 $ classes [] = $ className ;
@@ -110,9 +119,6 @@ public static function findClasses(string $path): array
110119 return $ classes ;
111120 }
112121
113- /**
114- * @return string
115- */
116122 private static function getExtraTypes (): string
117123 {
118124 static $ extraTypes = null ;
@@ -123,7 +129,7 @@ private static function getExtraTypes(): string
123129 $ extraTypes .= '|enum ' ;
124130 }
125131
126- $ extraTypesArray = array_filter (explode ('| ' , $ extraTypes ), function (string $ type ) {
132+ $ extraTypesArray = array_filter (explode ('| ' , $ extraTypes ), function (string $ type ): bool {
127133 return $ type !== '' ;
128134 });
129135 PhpFileCleaner::setTypeConfig (array_merge (['class ' , 'interface ' , 'trait ' ], $ extraTypesArray ));
@@ -140,7 +146,6 @@ private static function getExtraTypes(): string
140146 *
141147 * @see Composer\Util\Filesystem::isReadable
142148 *
143- * @param string $path
144149 * @return bool
145150 */
146151 private static function isReadable (string $ path )
0 commit comments