Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
75 | final class Glob |
||
76 | { |
||
77 | /** |
||
78 | * Flag: Enable escaping of special characters with leading backslashes. |
||
79 | */ |
||
80 | const ESCAPE = 1; |
||
81 | |||
82 | /** |
||
83 | * Globs the file system paths matching the glob. |
||
84 | * |
||
85 | * The glob may contain the wildcard "*". This wildcard matches any number |
||
86 | * of characters, *including* directory separators. |
||
87 | * |
||
88 | * ```php |
||
89 | * foreach (Glob::glob('/project/**.twig') as $path) { |
||
90 | * // do something... |
||
91 | * } |
||
92 | * ``` |
||
93 | * |
||
94 | * @param string $glob The canonical glob. The glob should contain forward |
||
95 | * slashes as directory separators only. It must not |
||
96 | * contain any "." or ".." segments. Use the |
||
97 | * "webmozart/path-util" utility to canonicalize globs |
||
98 | * prior to calling this method. |
||
99 | * @param int $flags A bitwise combination of the flag constants in this |
||
100 | * class. |
||
101 | * |
||
102 | * @return string[] The matching paths. The keys of the array are |
||
103 | * incrementing integers. |
||
104 | */ |
||
105 | 4 | public static function glob($glob, $flags = 0) |
|
113 | |||
114 | /** |
||
115 | * Matches a path against a glob. |
||
116 | * |
||
117 | * ```php |
||
118 | * if (Glob::match('/project/views/index.html.twig', '/project/**.twig')) { |
||
119 | * // path matches |
||
120 | * } |
||
121 | * ``` |
||
122 | * |
||
123 | * @param string $path The path to match. |
||
124 | * @param string $glob The canonical glob. The glob should contain forward |
||
125 | * slashes as directory separators only. It must not |
||
126 | * contain any "." or ".." segments. Use the |
||
127 | * "webmozart/path-util" utility to canonicalize globs |
||
128 | * prior to calling this method. |
||
129 | * @param int $flags A bitwise combination of the flag constants in |
||
130 | * this class. |
||
131 | * |
||
132 | * @return bool Returns `true` if the path is matched by the glob. |
||
133 | */ |
||
134 | 12 | public static function match($path, $glob, $flags = 0) |
|
150 | |||
151 | /** |
||
152 | * Filters an array for paths matching a glob. |
||
153 | * |
||
154 | * The filtered array is returned. This array preserves the keys of the |
||
155 | * passed array. |
||
156 | * |
||
157 | * ```php |
||
158 | * $filteredPaths = Glob::filter($paths, '/project/**.twig'); |
||
159 | * ``` |
||
160 | * |
||
161 | * @param string[] $paths A list of paths. |
||
162 | * @param string $glob The canonical glob. The glob should contain |
||
163 | * forward slashes as directory separators only. It |
||
164 | * must not contain any "." or ".." segments. Use the |
||
165 | * "webmozart/path-util" utility to canonicalize |
||
166 | * globs prior to calling this method. |
||
167 | * @param int $flags A bitwise combination of the flag constants in |
||
168 | * this class. |
||
169 | * |
||
170 | * @return string[] The paths matching the glob indexed by their original |
||
171 | * keys. |
||
172 | */ |
||
173 | 5 | public static function filter(array $paths, $glob, $flags = 0) |
|
190 | |||
191 | /** |
||
192 | * Returns the base path of a glob. |
||
193 | * |
||
194 | * This method returns the most specific directory that contains all files |
||
195 | * matched by the glob. If this directory does not exist on the file system, |
||
196 | * it's not necessary to execute the glob algorithm. |
||
197 | * |
||
198 | * More specifically, the "base path" is the longest path trailed by a "/" |
||
199 | * on the left of the first wildcard "*". If the glob does not contain |
||
200 | * wildcards, the directory name of the glob is returned. |
||
201 | * |
||
202 | * ```php |
||
203 | * Glob::getBasePath('/css/*.css'); |
||
204 | * // => /css |
||
205 | * |
||
206 | * Glob::getBasePath('/css/style.css'); |
||
207 | * // => /css |
||
208 | * |
||
209 | * Glob::getBasePath('/css/st*.css'); |
||
210 | * // => /css |
||
211 | * |
||
212 | * Glob::getBasePath('/*.css'); |
||
213 | * // => / |
||
214 | * ``` |
||
215 | * |
||
216 | * @param string $glob The canonical glob. The glob should contain forward |
||
217 | * slashes as directory separators only. It must not |
||
218 | * contain any "." or ".." segments. Use the |
||
219 | * "webmozart/path-util" utility to canonicalize globs |
||
220 | * prior to calling this method. |
||
221 | * @param int $flags A bitwise combination of the flag constants in this |
||
222 | * class. |
||
223 | * |
||
224 | * @return string The base path of the glob. |
||
225 | */ |
||
226 | 47 | public static function getBasePath($glob, $flags = 0) |
|
250 | |||
251 | /** |
||
252 | * Converts a glob to a regular expression. |
||
253 | * |
||
254 | * Use this method if you need to match many paths against a glob: |
||
255 | * |
||
256 | * ```php |
||
257 | * $staticPrefix = Glob::getStaticPrefix('/project/**.twig'); |
||
258 | * $regEx = Glob::toRegEx('/project/**.twig'); |
||
259 | * |
||
260 | * if (0 !== strpos($path, $staticPrefix)) { |
||
261 | * // no match |
||
262 | * } |
||
263 | * |
||
264 | * if (!preg_match($regEx, $path)) { |
||
265 | * // no match |
||
266 | * } |
||
267 | * ``` |
||
268 | * |
||
269 | * You should always test whether a path contains the static prefix of the |
||
270 | * glob returned by {@link getStaticPrefix()} to reduce the number of calls |
||
271 | * to the expensive {@link preg_match()}. |
||
272 | * |
||
273 | * @param string $glob The canonical glob. The glob should contain forward |
||
274 | * slashes as directory separators only. It must not |
||
275 | * contain any "." or ".." segments. Use the |
||
276 | * "webmozart/path-util" utility to canonicalize globs |
||
277 | * prior to calling this method. |
||
278 | * @param int $flags A bitwise combination of the flag constants in this |
||
279 | * class. |
||
280 | * |
||
281 | * @return string The regular expression for matching the glob. |
||
282 | */ |
||
283 | 77 | public static function toRegEx($glob, $flags = 0) |
|
284 | { |
||
285 | 77 | View Code Duplication | if (!Path::isAbsolute($glob) && false === strpos($glob, '://')) { |
286 | 1 | throw new InvalidArgumentException(sprintf( |
|
287 | 1 | 'The glob "%s" is not absolute and not a URI.', |
|
288 | $glob |
||
289 | )); |
||
290 | } |
||
291 | |||
292 | // From the PHP manual: To specify a literal single quote, escape it |
||
293 | // with a backslash (\). To specify a literal backslash, double it (\\). |
||
294 | // All other instances of backslash will be treated as a literal backslash. |
||
295 | |||
296 | // This method does the following replacements: |
||
297 | |||
298 | // Normal wildcards: "*" => "[^/]*" (regex match any except separator) |
||
299 | // Double wildcards: "**" => ".*" (regex match any) |
||
300 | // Sets: "{ab,cd}" => "(ab|cd)" (regex group) |
||
301 | |||
302 | // with flag Glob::ESCAPE: |
||
303 | // Escaped wildcards: "\*" => "\*" (regex star) |
||
304 | // Escaped backslashes: "\\" => "\\" (regex backslash) |
||
305 | |||
306 | // Other characters are escaped as usual for regular expressions. |
||
307 | |||
308 | // Quote regex characters |
||
309 | 76 | $quoted = preg_quote($glob, '~'); |
|
310 | |||
311 | 76 | if ($flags & self::ESCAPE) { |
|
312 | 36 | $regEx = self::toRegExEscaped($quoted); |
|
313 | } else { |
||
314 | 41 | $regEx = self::toRegExNonEscaped($quoted); |
|
315 | } |
||
316 | |||
317 | 76 | return '~^'.$regEx.'$~'; |
|
318 | } |
||
319 | |||
320 | /** |
||
321 | * Returns the static prefix of a glob. |
||
322 | * |
||
323 | * The "static prefix" is the part of the glob up to the first wildcard "*". |
||
324 | * If the glob does not contain wildcards, the full glob is returned. |
||
325 | * |
||
326 | * @param string $glob The canonical glob. The glob should contain forward |
||
327 | * slashes as directory separators only. It must not |
||
328 | * contain any "." or ".." segments. Use the |
||
329 | * "webmozart/path-util" utility to canonicalize globs |
||
330 | * prior to calling this method. |
||
331 | * @param int $flags A bitwise combination of the flag constants in this |
||
332 | * class. |
||
333 | * |
||
334 | * @return string The static prefix of the glob. |
||
335 | */ |
||
336 | 96 | public static function getStaticPrefix($glob, $flags = 0) |
|
337 | { |
||
338 | 96 | View Code Duplication | if (!Path::isAbsolute($glob) && false === strpos($glob, '://')) { |
339 | 5 | throw new InvalidArgumentException(sprintf( |
|
340 | 5 | 'The glob "%s" is not absolute and not a URI.', |
|
341 | $glob |
||
342 | )); |
||
343 | } |
||
344 | |||
345 | 91 | $prefix = $glob; |
|
346 | |||
347 | 91 | if ($flags & self::ESCAPE) { |
|
348 | // Read backslashes together with the next (the escaped) character |
||
349 | // up to the first non-escaped star/brace |
||
350 | 32 | if (preg_match('~^('.Symbol::BACKSLASH.'.|[^'.Symbol::BACKSLASH.Symbol::STAR.Symbol::L_BRACE.Symbol::QUESTION_MARK.Symbol::L_BRACKET.'])*~', $glob, $matches)) { |
|
351 | 32 | $prefix = $matches[0]; |
|
352 | } |
||
353 | |||
354 | // Replace escaped characters by their unescaped equivalents |
||
355 | 32 | $prefix = str_replace( |
|
356 | 32 | array('\\\\', '\\*', '\\{', '\\}', '\\?', '\\[', '\\]', '\\^'), |
|
357 | 32 | array('\\', '*', '{', '}', '?', '[', ']', '^'), |
|
358 | $prefix |
||
359 | ); |
||
360 | } else { |
||
361 | 61 | $pos1 = strpos($glob, '*'); |
|
362 | 61 | $pos2 = strpos($glob, '{'); |
|
363 | 61 | $pos3 = strpos($glob, '?'); |
|
364 | 61 | $pos4 = strpos($glob, '['); |
|
365 | |||
366 | $positions = array_filter(array($pos1, $pos2, $pos3, $pos4), function ($v) { |
||
367 | 61 | return false !== $v; |
|
368 | 61 | }); |
|
369 | |||
370 | 61 | if (!empty($positions)) { |
|
371 | 56 | $prefix = substr($glob, 0, min($positions)); |
|
372 | } |
||
373 | } |
||
374 | |||
375 | 91 | return $prefix; |
|
376 | } |
||
377 | |||
378 | /** |
||
379 | * Returns whether the glob contains a dynamic part. |
||
380 | * |
||
381 | * The glob contains a dynamic part if it contains an unescaped "*" or |
||
382 | * "{" character. |
||
383 | * |
||
384 | * @param string $glob The glob to test. |
||
385 | * |
||
386 | * @return bool Returns `true` if the glob contains a dynamic part and |
||
387 | * `false` otherwise. |
||
388 | */ |
||
389 | 34 | public static function isDynamic($glob) |
|
393 | |||
394 | private function __construct() |
||
397 | |||
398 | 41 | private static function toRegExNonEscaped($quoted) |
|
399 | { |
||
433 | |||
434 | 36 | private static function toRegExEscaped($quoted) |
|
513 | } |
||
514 |