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 | * Globs the file system paths matching the glob. |
||
| 79 | * |
||
| 80 | * The glob may contain the wildcard "*". This wildcard matches any number |
||
| 81 | * of characters, *including* directory separators. |
||
| 82 | * |
||
| 83 | * ```php |
||
| 84 | * foreach (Glob::glob('/project/**.twig') as $path) { |
||
| 85 | * // do something... |
||
| 86 | * } |
||
| 87 | * ``` |
||
| 88 | * |
||
| 89 | * @param string $glob The canonical glob. The glob should contain forward |
||
| 90 | * slashes as directory separators only. It must not |
||
| 91 | * contain any "." or ".." segments. Use the |
||
| 92 | * "webmozart/path-util" utility to canonicalize globs |
||
| 93 | * prior to calling this method. |
||
| 94 | * @param int $flags A bitwise combination of the flag constants in this |
||
| 95 | * class. |
||
| 96 | * |
||
| 97 | * @return string[] The matching paths. The keys of the array are |
||
| 98 | * incrementing integers. |
||
| 99 | */ |
||
| 100 | 6 | public static function glob($glob, $flags = 0) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Matches a path against a glob. |
||
| 111 | * |
||
| 112 | * ```php |
||
| 113 | * if (Glob::match('/project/views/index.html.twig', '/project/**.twig')) { |
||
| 114 | * // path matches |
||
| 115 | * } |
||
| 116 | * ``` |
||
| 117 | * |
||
| 118 | * @param string $path The path to match. |
||
| 119 | * @param string $glob The canonical glob. The glob should contain forward |
||
| 120 | * slashes as directory separators only. It must not |
||
| 121 | * contain any "." or ".." segments. Use the |
||
| 122 | * "webmozart/path-util" utility to canonicalize globs |
||
| 123 | * prior to calling this method. |
||
| 124 | * @param int $flags A bitwise combination of the flag constants in |
||
| 125 | * this class. |
||
| 126 | * |
||
| 127 | * @return bool Returns `true` if the path is matched by the glob. |
||
| 128 | */ |
||
| 129 | 12 | public static function match($path, $glob, $flags = 0) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Filters an array for paths matching a glob. |
||
| 148 | * |
||
| 149 | * The filtered array is returned. This array preserves the keys of the |
||
| 150 | * passed array. |
||
| 151 | * |
||
| 152 | * ```php |
||
| 153 | * $filteredPaths = Glob::filter($paths, '/project/**.twig'); |
||
| 154 | * ``` |
||
| 155 | * |
||
| 156 | * @param string[] $paths A list of paths. |
||
| 157 | * @param string $glob The canonical glob. The glob should contain |
||
| 158 | * forward slashes as directory separators only. It |
||
| 159 | * must not contain any "." or ".." segments. Use the |
||
| 160 | * "webmozart/path-util" utility to canonicalize |
||
| 161 | * globs prior to calling this method. |
||
| 162 | * @param int $flags A bitwise combination of the flag constants in |
||
| 163 | * this class. |
||
| 164 | * |
||
| 165 | * @return string[] The paths matching the glob indexed by their original |
||
| 166 | * keys. |
||
| 167 | */ |
||
| 168 | 4 | public static function filter(array $paths, $glob, $flags = 0) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Returns the base path of a glob. |
||
| 188 | * |
||
| 189 | * This method returns the most specific directory that contains all files |
||
| 190 | * matched by the glob. If this directory does not exist on the file system, |
||
| 191 | * it's not necessary to execute the glob algorithm. |
||
| 192 | * |
||
| 193 | * More specifically, the "base path" is the longest path trailed by a "/" |
||
| 194 | * on the left of the first wildcard "*". If the glob does not contain |
||
| 195 | * wildcards, the directory name of the glob is returned. |
||
| 196 | * |
||
| 197 | * ```php |
||
| 198 | * Glob::getBasePath('/css/*.css'); |
||
| 199 | * // => /css |
||
| 200 | * |
||
| 201 | * Glob::getBasePath('/css/style.css'); |
||
| 202 | * // => /css |
||
| 203 | * |
||
| 204 | * Glob::getBasePath('/css/st*.css'); |
||
| 205 | * // => /css |
||
| 206 | * |
||
| 207 | * Glob::getBasePath('/*.css'); |
||
| 208 | * // => / |
||
| 209 | * ``` |
||
| 210 | * |
||
| 211 | * @param string $glob The canonical glob. The glob should contain forward |
||
| 212 | * slashes as directory separators only. It must not |
||
| 213 | * contain any "." or ".." segments. Use the |
||
| 214 | * "webmozart/path-util" utility to canonicalize globs |
||
| 215 | * prior to calling this method. |
||
| 216 | * @param int $flags A bitwise combination of the flag constants in this |
||
| 217 | * class. |
||
| 218 | * |
||
| 219 | * @return string The base path of the glob. |
||
| 220 | */ |
||
| 221 | 40 | public static function getBasePath($glob, $flags = 0) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Converts a glob to a regular expression. |
||
| 248 | * |
||
| 249 | * Use this method if you need to match many paths against a glob: |
||
| 250 | * |
||
| 251 | * ```php |
||
| 252 | * $staticPrefix = Glob::getStaticPrefix('/project/**.twig'); |
||
| 253 | * $regEx = Glob::toRegEx('/project/**.twig'); |
||
| 254 | * |
||
| 255 | * if (0 !== strpos($path, $staticPrefix)) { |
||
| 256 | * // no match |
||
| 257 | * } |
||
| 258 | * |
||
| 259 | * if (!preg_match($regEx, $path)) { |
||
| 260 | * // no match |
||
| 261 | * } |
||
| 262 | * ``` |
||
| 263 | * |
||
| 264 | * You should always test whether a path contains the static prefix of the |
||
| 265 | * glob returned by {@link getStaticPrefix()} to reduce the number of calls |
||
| 266 | * to the expensive {@link preg_match()}. |
||
| 267 | * |
||
| 268 | * @param string $glob The canonical glob. The glob should contain forward |
||
| 269 | * slashes as directory separators only. It must not |
||
| 270 | * contain any "." or ".." segments. Use the |
||
| 271 | * "webmozart/path-util" utility to canonicalize globs |
||
| 272 | * prior to calling this method. |
||
| 273 | * @param int $flags A bitwise combination of the flag constants in this |
||
| 274 | * class. |
||
| 275 | * |
||
| 276 | * @return string The regular expression for matching the glob. |
||
| 277 | */ |
||
| 278 | 70 | public static function toRegEx($glob, $flags = 0, $delimiter = '~') |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Returns the static prefix of a glob. |
||
| 409 | * |
||
| 410 | * The "static prefix" is the part of the glob up to the first wildcard "*". |
||
| 411 | * If the glob does not contain wildcards, the full glob is returned. |
||
| 412 | * |
||
| 413 | * @param string $glob The canonical glob. The glob should contain forward |
||
| 414 | * slashes as directory separators only. It must not |
||
| 415 | * contain any "." or ".." segments. Use the |
||
| 416 | * "webmozart/path-util" utility to canonicalize globs |
||
| 417 | * prior to calling this method. |
||
| 418 | * @param int $flags A bitwise combination of the flag constants in this |
||
| 419 | * class. |
||
| 420 | * |
||
| 421 | * @return string The static prefix of the glob. |
||
| 422 | */ |
||
| 423 | 76 | public static function getStaticPrefix($glob, $flags = 0) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Returns whether the glob contains a dynamic part. |
||
| 483 | * |
||
| 484 | * The glob contains a dynamic part if it contains an unescaped "*" or |
||
| 485 | * "{" character. |
||
| 486 | * |
||
| 487 | * @param string $glob The glob to test. |
||
| 488 | * |
||
| 489 | * @return bool Returns `true` if the glob contains a dynamic part and |
||
| 490 | * `false` otherwise. |
||
| 491 | */ |
||
| 492 | 34 | public static function isDynamic($glob) |
|
| 496 | |||
| 497 | private function __construct() |
||
| 500 | } |
||
| 501 |