1313use function array_pop ;
1414use function count ;
1515use function ctype_alpha ;
16- use function preg_match ;
1716use function preg_quote ;
1817use function sprintf ;
1918use function strlen ;
20- use function substr ;
2119use RuntimeException ;
2220
2321/**
22+ * FileMatcher attempts to emulate the behavior of PHP's glob function on file
23+ * paths based on POSIX.2.
24+ *
25+ * - https://en.wikipedia.org/wiki/Glob_(programming)
26+ * - https://man7.org/linux/man-pages/man7/glob.7.html
27+ *
2428 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
2529 *
2630 * @internal This class is not covered by the backward compatibility promise for PHPUnit
4347 private const T_COLON = 'colon ' ;
4448 private const T_CHAR_CLASS = 'char_class ' ;
4549
46- public static function match (string $ path , FileMatcherPattern $ pattern ): bool
47- {
48- self ::assertIsAbsolute ($ path );
49-
50- $ regex = self ::toRegEx ($ pattern ->path );
51-
52- return preg_match ($ regex , $ path ) !== 0 ;
53- }
54-
5550 /**
56- * Based on webmozart/glob.
57- *
58- * @return string the regular expression for matching the glob
51+ * Compile a regex for the given glob.
5952 */
60- public static function toRegEx ($ glob , $ flags = 0 ): string
53+ public static function toRegEx (string $ glob ): FileMatcherRegex
6154 {
62- self ::assertIsAbsolute ($ glob );
63-
6455 $ tokens = self ::tokenize ($ glob );
65-
66- $ regex = '' ;
56+ $ regex = '' ;
6757
6858 foreach ($ tokens as $ token ) {
6959 $ type = $ token [0 ];
@@ -84,24 +74,15 @@ public static function toRegEx($glob, $flags = 0): string
8474 self ::T_BRACKET_CLOSE => '] ' ,
8575 self ::T_HYPHEN => '- ' ,
8676 self ::T_CHAR_CLASS => '[: ' . $ token [1 ] . ':] ' ,
87- default => '' ,
77+ default => throw new RuntimeException (sprintf (
78+ 'Unhandled token type: %s - this should not happen ' ,
79+ $ type ,
80+ )),
8881 };
8982 }
9083 $ regex .= '(/|$) ' ;
91- dump ($ tokens );
92- dump ($ regex );
9384
94- return '{^ ' . $ regex . '} ' ;
95- }
96-
97- private static function assertIsAbsolute (string $ path ): void
98- {
99- if (substr ($ path , 0 , 1 ) !== '/ ' ) {
100- throw new RuntimeException (sprintf (
101- 'Path "%s" must be absolute ' ,
102- $ path ,
103- ));
104- }
85+ return new FileMatcherRegex ('{^ ' . $ regex . '} ' );
10586 }
10687
10788 /**
0 commit comments