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:
Complex classes like Glob often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Glob, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
75 | final class Glob |
||
76 | { |
||
77 | /** |
||
78 | * Flag: Match the keys instead of the values in {@link Glob::filter()} |
||
79 | */ |
||
80 | const MATCH_KEYS = 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 | 8 | 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 | 8 | public static function filter(array $paths, $glob, $flags = 0) |
|
212 | |||
213 | /** |
||
214 | * Returns the base path of a glob. |
||
215 | * |
||
216 | * This method returns the most specific directory that contains all files |
||
217 | * matched by the glob. If this directory does not exist on the file system, |
||
218 | * it's not necessary to execute the glob algorithm. |
||
219 | * |
||
220 | * More specifically, the "base path" is the longest path trailed by a "/" |
||
221 | * on the left of the first wildcard "*". If the glob does not contain |
||
222 | * wildcards, the directory name of the glob is returned. |
||
223 | * |
||
224 | * ```php |
||
225 | * Glob::getBasePath('/css/*.css'); |
||
226 | * // => /css |
||
227 | * |
||
228 | * Glob::getBasePath('/css/style.css'); |
||
229 | * // => /css |
||
230 | * |
||
231 | * Glob::getBasePath('/css/st*.css'); |
||
232 | * // => /css |
||
233 | * |
||
234 | * Glob::getBasePath('/*.css'); |
||
235 | * // => / |
||
236 | * ``` |
||
237 | * |
||
238 | * @param string $glob The canonical glob. The glob should contain forward |
||
239 | * slashes as directory separators only. It must not |
||
240 | * contain any "." or ".." segments. Use the |
||
241 | * "webmozart/path-util" utility to canonicalize globs |
||
242 | * prior to calling this method. |
||
243 | * @param int $flags A bitwise combination of the flag constants in this |
||
244 | * class. |
||
245 | * |
||
246 | * @return string The base path of the glob. |
||
247 | */ |
||
248 | 42 | public static function getBasePath($glob, $flags = 0) |
|
272 | |||
273 | /** |
||
274 | * Converts a glob to a regular expression. |
||
275 | * |
||
276 | * Use this method if you need to match many paths against a glob: |
||
277 | * |
||
278 | * ```php |
||
279 | * $staticPrefix = Glob::getStaticPrefix('/project/**.twig'); |
||
280 | * $regEx = Glob::toRegEx('/project/**.twig'); |
||
281 | * |
||
282 | * if (0 !== strpos($path, $staticPrefix)) { |
||
283 | * // no match |
||
284 | * } |
||
285 | * |
||
286 | * if (!preg_match($regEx, $path)) { |
||
287 | * // no match |
||
288 | * } |
||
289 | * ``` |
||
290 | * |
||
291 | * You should always test whether a path contains the static prefix of the |
||
292 | * glob returned by {@link getStaticPrefix()} to reduce the number of calls |
||
293 | * to the expensive {@link preg_match()}. |
||
294 | * |
||
295 | * @param string $glob The canonical glob. The glob should contain forward |
||
296 | * slashes as directory separators only. It must not |
||
297 | * contain any "." or ".." segments. Use the |
||
298 | * "webmozart/path-util" utility to canonicalize globs |
||
299 | * prior to calling this method. |
||
300 | * @param int $flags A bitwise combination of the flag constants in this |
||
301 | * class. |
||
302 | * |
||
303 | * @return string The regular expression for matching the glob. |
||
304 | */ |
||
305 | 75 | public static function toRegEx($glob, $flags = 0, $delimiter = '~') |
|
433 | |||
434 | /** |
||
435 | * Returns the static prefix of a glob. |
||
436 | * |
||
437 | * The "static prefix" is the part of the glob up to the first wildcard "*". |
||
438 | * If the glob does not contain wildcards, the full glob is returned. |
||
439 | * |
||
440 | * @param string $glob The canonical glob. The glob should contain forward |
||
441 | * slashes as directory separators only. It must not |
||
442 | * contain any "." or ".." segments. Use the |
||
443 | * "webmozart/path-util" utility to canonicalize globs |
||
444 | * prior to calling this method. |
||
445 | * @param int $flags A bitwise combination of the flag constants in this |
||
446 | * class. |
||
447 | * |
||
448 | * @return string The static prefix of the glob. |
||
449 | */ |
||
450 | 81 | public static function getStaticPrefix($glob, $flags = 0) |
|
507 | |||
508 | /** |
||
509 | * Returns whether the glob contains a dynamic part. |
||
510 | * |
||
511 | * The glob contains a dynamic part if it contains an unescaped "*" or |
||
512 | * "{" character. |
||
513 | * |
||
514 | * @param string $glob The glob to test. |
||
515 | * |
||
516 | * @return bool Returns `true` if the glob contains a dynamic part and |
||
517 | * `false` otherwise. |
||
518 | */ |
||
519 | 40 | public static function isDynamic($glob) |
|
523 | |||
524 | private function __construct() |
||
527 | } |
||
528 |