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 | 70 | public static function toRegEx($glob, $flags = 0) |
|
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) |
|
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 | 31 | private static function toRegExNonEscaped($quoted) |
|
433 | |||
434 | 38 | private static function toRegExEscaped($quoted) |
|
521 | } |
||
522 |