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 |
||
30 | class SS_FileFinder { |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected static $vcs_dirs = array( |
||
36 | '.git', '.svn', '.hg', '.bzr', 'node_modules', |
||
37 | ); |
||
38 | |||
39 | /** |
||
40 | * The default options that are set on a new finder instance. Options not |
||
41 | * present in this array cannot be set. |
||
42 | * |
||
43 | * Any default_option statics defined on child classes are also taken into |
||
44 | * account. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected static $default_options = array( |
||
49 | 'name_regex' => null, |
||
50 | 'accept_callback' => null, |
||
51 | 'accept_dir_callback' => null, |
||
52 | 'accept_file_callback' => null, |
||
53 | 'file_callback' => null, |
||
54 | 'dir_callback' => null, |
||
55 | 'ignore_files' => null, |
||
56 | 'ignore_dirs' => null, |
||
57 | 'ignore_vcs' => true, |
||
58 | 'min_depth' => null, |
||
59 | 'max_depth' => null |
||
60 | ); |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $options; |
||
66 | |||
67 | public function __construct() { |
||
77 | |||
78 | /** |
||
79 | * Returns an option value set on this instance. |
||
80 | * |
||
81 | * @param string $name |
||
82 | * @return mixed |
||
83 | */ |
||
84 | public function getOption($name) { |
||
91 | |||
92 | /** |
||
93 | * Set an option on this finder instance. See {@link SS_FileFinder} for the |
||
94 | * list of options available. |
||
95 | * |
||
96 | * @param string $name |
||
97 | * @param mixed $value |
||
98 | */ |
||
99 | public function setOption($name, $value) { |
||
106 | |||
107 | /** |
||
108 | * Sets several options at once. |
||
109 | * |
||
110 | * @param array $options |
||
111 | */ |
||
112 | public function setOptions(array $options) { |
||
115 | |||
116 | /** |
||
117 | * Finds all files matching the options within a directory. The search is |
||
118 | * performed depth first. |
||
119 | * |
||
120 | * @param string $base |
||
121 | * @return array |
||
122 | */ |
||
123 | public function find($base) { |
||
168 | |||
169 | /** |
||
170 | * Returns TRUE if the directory should be traversed. This can be overloaded |
||
171 | * to customise functionality, or extended with callbacks. |
||
172 | * |
||
173 | * @return bool |
||
174 | */ |
||
175 | protected function acceptDir($basename, $pathname, $depth) { |
||
198 | |||
199 | /** |
||
200 | * Returns TRUE if the file should be included in the results. This can be |
||
201 | * overloaded to customise functionality, or extended via callbacks. |
||
202 | * |
||
203 | * @return bool |
||
204 | */ |
||
205 | protected function acceptFile($basename, $pathname, $depth) { |
||
228 | |||
229 | } |
||
230 |