Complex classes like SS_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 SS_FileFinder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class FileFinder { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected static $vcs_dirs = array( |
||
| 39 | '.git', '.svn', '.hg', '.bzr', 'node_modules', |
||
| 40 | ); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The default options that are set on a new finder instance. Options not |
||
| 44 | * present in this array cannot be set. |
||
| 45 | * |
||
| 46 | * Any default_option statics defined on child classes are also taken into |
||
| 47 | * account. |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected static $default_options = array( |
||
| 52 | 'name_regex' => null, |
||
| 53 | 'accept_callback' => null, |
||
| 54 | 'accept_dir_callback' => null, |
||
| 55 | 'accept_file_callback' => null, |
||
| 56 | 'file_callback' => null, |
||
| 57 | 'dir_callback' => null, |
||
| 58 | 'ignore_files' => null, |
||
| 59 | 'ignore_dirs' => null, |
||
| 60 | 'ignore_vcs' => true, |
||
| 61 | 'min_depth' => null, |
||
| 62 | 'max_depth' => null |
||
| 63 | ); |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $options; |
||
| 69 | |||
| 70 | public function __construct() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Returns an option value set on this instance. |
||
| 83 | * |
||
| 84 | * @param string $name |
||
| 85 | * @return mixed |
||
| 86 | */ |
||
| 87 | public function getOption($name) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Set an option on this finder instance. See {@link SS_FileFinder} for the |
||
| 97 | * list of options available. |
||
| 98 | * |
||
| 99 | * @param string $name |
||
| 100 | * @param mixed $value |
||
| 101 | */ |
||
| 102 | public function setOption($name, $value) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Sets several options at once. |
||
| 112 | * |
||
| 113 | * @param array $options |
||
| 114 | */ |
||
| 115 | public function setOptions(array $options) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Finds all files matching the options within a directory. The search is |
||
| 121 | * performed depth first. |
||
| 122 | * |
||
| 123 | * @param string $base |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | public function find($base) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Returns TRUE if the directory should be traversed. This can be overloaded |
||
| 174 | * to customise functionality, or extended with callbacks. |
||
| 175 | * |
||
| 176 | * @param string $basename |
||
| 177 | * @param string $pathname |
||
| 178 | * @param int $depth |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | protected function acceptDir($basename, $pathname, $depth) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Returns TRUE if the file should be included in the results. This can be |
||
| 207 | * overloaded to customise functionality, or extended via callbacks. |
||
| 208 | * |
||
| 209 | * @param string $basename |
||
| 210 | * @param string $pathname |
||
| 211 | * @param int $depth |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | protected function acceptFile($basename, $pathname, $depth) { |
||
| 237 | |||
| 238 | } |
||
| 239 |