Complex classes like File 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 File, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class File extends Base |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * File model instance |
||
22 | * @var \gplcart\core\models\File $file |
||
23 | */ |
||
24 | protected $file; |
||
25 | |||
26 | /** |
||
27 | * @param FileModel $file |
||
28 | */ |
||
29 | public function __construct(FileModel $file) |
||
35 | |||
36 | /** |
||
37 | * Callback for "file-get" command |
||
38 | */ |
||
39 | public function cmdGetFile() |
||
46 | |||
47 | /** |
||
48 | * Callback for "file-delete" command |
||
49 | */ |
||
50 | public function cmdDeleteFile() |
||
100 | |||
101 | /** |
||
102 | * Delete a file |
||
103 | * @param int|array $file |
||
104 | * @return bool |
||
105 | */ |
||
106 | protected function deleteFile($file) |
||
118 | |||
119 | /** |
||
120 | * Callback for "file-add" command |
||
121 | */ |
||
122 | public function cmdAddFile() |
||
132 | |||
133 | /** |
||
134 | * Callback for "file-update" command |
||
135 | */ |
||
136 | public function cmdUpdateFile() |
||
155 | |||
156 | /** |
||
157 | * Returns an array of files |
||
158 | * @return array |
||
159 | */ |
||
160 | protected function getListFile() |
||
192 | |||
193 | /** |
||
194 | * Output table format |
||
195 | * @param array $items |
||
196 | */ |
||
197 | protected function outputFormatTableFile(array $items) |
||
223 | |||
224 | /** |
||
225 | * Add a new file record |
||
226 | */ |
||
227 | protected function addFile() |
||
237 | |||
238 | /** |
||
239 | * Updates a file record |
||
240 | * @param string $file_id |
||
241 | */ |
||
242 | protected function updateFile($file_id) |
||
248 | |||
249 | /** |
||
250 | * Add a new file record at once |
||
251 | */ |
||
252 | protected function submitAddFile() |
||
258 | |||
259 | /** |
||
260 | * Add a new file record step by step |
||
261 | */ |
||
262 | protected function wizardAddFile() |
||
274 | |||
275 | } |
||
276 |