Complex classes like FileFinder 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 FileFinder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class FileFinder |
||
| 34 | { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected static $vcs_dirs = array( |
||
| 40 | '.git', '.svn', '.hg', '.bzr', 'node_modules', |
||
| 41 | ); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The default options that are set on a new finder instance. Options not |
||
| 45 | * present in this array cannot be set. |
||
| 46 | * |
||
| 47 | * Any default_option statics defined on child classes are also taken into |
||
| 48 | * account. |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected static $default_options = array( |
||
| 53 | 'name_regex' => null, |
||
| 54 | 'accept_callback' => null, |
||
| 55 | 'accept_dir_callback' => null, |
||
| 56 | 'accept_file_callback' => null, |
||
| 57 | 'file_callback' => null, |
||
| 58 | 'dir_callback' => null, |
||
| 59 | 'ignore_files' => null, |
||
| 60 | 'ignore_dirs' => null, |
||
| 61 | 'ignore_vcs' => true, |
||
| 62 | 'min_depth' => null, |
||
| 63 | 'max_depth' => null |
||
| 64 | ); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $options; |
||
| 70 | |||
| 71 | public function __construct() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Returns an option value set on this instance. |
||
| 84 | * |
||
| 85 | * @param string $name |
||
| 86 | * @return mixed |
||
| 87 | */ |
||
| 88 | public function getOption($name) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Set an option on this finder instance. See {@link SS_FileFinder} for the |
||
| 99 | * list of options available. |
||
| 100 | * |
||
| 101 | * @param string $name |
||
| 102 | * @param mixed $value |
||
| 103 | */ |
||
| 104 | public function setOption($name, $value) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Sets several options at once. |
||
| 115 | * |
||
| 116 | * @param array $options |
||
| 117 | */ |
||
| 118 | public function setOptions(array $options) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Finds all files matching the options within a directory. The search is |
||
| 127 | * performed depth first. |
||
| 128 | * |
||
| 129 | * @param string $base |
||
| 130 | * @return array |
||
| 131 | */ |
||
| 132 | public function find($base) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Returns TRUE if the directory should be traversed. This can be overloaded |
||
| 187 | * to customise functionality, or extended with callbacks. |
||
| 188 | * |
||
| 189 | * @param string $basename |
||
| 190 | * @param string $pathname |
||
| 191 | * @param int $depth |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | protected function acceptDir($basename, $pathname, $depth) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Returns TRUE if the file should be included in the results. This can be |
||
| 229 | * overloaded to customise functionality, or extended via callbacks. |
||
| 230 | * |
||
| 231 | * @param string $basename |
||
| 232 | * @param string $pathname |
||
| 233 | * @param int $depth |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | protected function acceptFile($basename, $pathname, $depth) |
||
| 270 | } |
||
| 271 |