Complex classes like Editor 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 Editor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Editor extends BackendController |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * Editor model instance |
||
24 | * @var \gplcart\modules\editor\models\Editor $editor |
||
25 | */ |
||
26 | protected $editor; |
||
27 | |||
28 | /** |
||
29 | * Module model instance |
||
30 | * @var \gplcart\core\models\Module $module |
||
31 | */ |
||
32 | protected $module; |
||
33 | |||
34 | /** |
||
35 | * The current module |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $data_module = array(); |
||
39 | |||
40 | /** |
||
41 | * The current module file |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $data_file; |
||
45 | |||
46 | /** |
||
47 | * The current file extension |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $data_extension; |
||
51 | |||
52 | /** |
||
53 | * @param EditorModuleModel $editor |
||
54 | * @param ModuleModel $module |
||
55 | */ |
||
56 | public function __construct(EditorModuleModel $editor, ModuleModel $module) |
||
57 | { |
||
58 | parent::__construct(); |
||
59 | |||
60 | $this->editor = $editor; |
||
61 | $this->module = $module; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Route callback to display the select theme page |
||
66 | */ |
||
67 | public function themeEditor() |
||
68 | { |
||
69 | $this->setTitleThemeEditor(); |
||
70 | $this->setBreadcrumbThemeEditor(); |
||
71 | |||
72 | $this->setData('themes', $this->module->getByType('theme')); |
||
73 | |||
74 | $this->outputThemeEditor(); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Set title on the select theme page |
||
79 | */ |
||
80 | protected function setTitleThemeEditor() |
||
81 | { |
||
82 | $this->setTitle($this->text('Themes')); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Set breadcrumbs on the select theme page |
||
87 | */ |
||
88 | protected function setBreadcrumbThemeEditor() |
||
89 | { |
||
90 | $breadcrumbs = array(); |
||
91 | |||
92 | $breadcrumbs[] = array( |
||
93 | 'url' => $this->url('admin'), |
||
94 | 'text' => $this->text('Dashboard') |
||
95 | ); |
||
96 | |||
97 | $breadcrumbs[] = array( |
||
98 | 'url' => $this->url('admin/module/list'), |
||
99 | 'text' => $this->text('Modules') |
||
100 | ); |
||
101 | |||
102 | $this->setBreadcrumbs($breadcrumbs); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Output rendered templates on the select theme page |
||
107 | */ |
||
108 | protected function outputThemeEditor() |
||
109 | { |
||
110 | $this->output('editor|themes'); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Displays the module file overview page |
||
115 | * @param integer $module_id |
||
116 | */ |
||
117 | public function listEditor($module_id) |
||
118 | { |
||
119 | $this->setModuleEditor($module_id); |
||
120 | |||
121 | $this->setTitleListEditor(); |
||
122 | $this->setBreadcrumbListEditor(); |
||
123 | |||
124 | $this->setData('module', $this->data_module); |
||
125 | $this->setData('files', $this->getFilesEditor()); |
||
126 | |||
127 | $this->outputListEditor(); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Returns an array of module data |
||
132 | * @param string $module_id |
||
133 | */ |
||
134 | protected function setModuleEditor($module_id) |
||
135 | { |
||
136 | $module = $this->module->get($module_id); |
||
137 | |||
138 | if (empty($module)) { |
||
139 | $this->outputHttpStatus(404); |
||
140 | } |
||
141 | |||
142 | if ($module['type'] !== 'theme') { |
||
143 | $this->outputHttpStatus(403); |
||
144 | } |
||
145 | |||
146 | $this->data_module = $module; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Returns an array of files to edit |
||
151 | * @return array |
||
152 | */ |
||
153 | protected function getFilesEditor() |
||
154 | { |
||
155 | $data = $this->editor->getList($this->data_module); |
||
156 | return $this->prepareFilesEditor($data); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Prepares an array of files to be edited |
||
161 | * @param array $data |
||
162 | * @return array |
||
163 | */ |
||
164 | protected function prepareFilesEditor(array $data) |
||
165 | { |
||
166 | $prepared = array(); |
||
167 | foreach ($data as $folder => $files) { |
||
168 | foreach ($files as $file) { |
||
169 | |||
170 | $path = trim(str_replace($this->data_module['directory'], '', $file), '/'); |
||
171 | $depth = substr_count(str_replace('\\', '/', $path), '/'); // WIN compatibility |
||
172 | |||
173 | $pathinfo = pathinfo($path); |
||
174 | $directory = is_dir($file); |
||
175 | $parent = $directory ? $path : $pathinfo['dirname']; |
||
176 | |||
177 | $prepared[$folder][$parent][] = array( |
||
178 | 'file' => $file, |
||
179 | 'path' => $path, |
||
180 | 'depth' => $depth, |
||
181 | 'directory' => $directory, |
||
182 | 'name' => $pathinfo['basename'], |
||
183 | 'id' => gplcart_string_encode($path), |
||
184 | 'indentation' => str_repeat('<span class="indentation"></span>', $depth) |
||
185 | ); |
||
186 | } |
||
187 | |||
188 | ksort($prepared[$folder]); |
||
189 | } |
||
190 | return $prepared; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Sets title on theme files overview page |
||
195 | */ |
||
196 | protected function setTitleListEditor() |
||
197 | { |
||
198 | $text = $this->text('Edit theme %name', array('%name' => $this->data_module['name'])); |
||
199 | $this->setTitle($text); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Sets breadcrumbs on theme files overview page |
||
204 | */ |
||
205 | protected function setBreadcrumbListEditor() |
||
206 | { |
||
207 | $breadcrumbs = array(); |
||
208 | |||
209 | $breadcrumbs[] = array( |
||
210 | 'url' => $this->url('admin'), |
||
211 | 'text' => $this->text('Dashboard') |
||
212 | ); |
||
213 | |||
214 | $breadcrumbs[] = array( |
||
215 | 'url' => $this->url('admin/module/list'), |
||
216 | 'text' => $this->text('Modules') |
||
217 | ); |
||
218 | |||
219 | $this->setBreadcrumbs($breadcrumbs); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Renders templates of theme files overview page |
||
224 | */ |
||
225 | protected function outputListEditor() |
||
229 | |||
230 | /** |
||
231 | * Displays the file edit page |
||
232 | * @param string $module_id |
||
233 | * @param string $file_id |
||
234 | */ |
||
235 | public function editEditor($module_id, $file_id) |
||
236 | { |
||
237 | $this->setModuleEditor($module_id); |
||
238 | $this->setFilePathEditor($file_id); |
||
239 | |||
240 | $this->setTitleEditEditor(); |
||
254 | |||
255 | /** |
||
256 | * Sets messages on the file edit page |
||
257 | */ |
||
258 | protected function setMessageEditEditor() |
||
268 | |||
269 | /** |
||
270 | * Sets JavaScript settings on the file edit page |
||
271 | */ |
||
272 | protected function setJsSettingsEditor() |
||
281 | |||
282 | /** |
||
283 | * Saves an array of submitted data |
||
284 | */ |
||
285 | protected function submitEditor() |
||
291 | |||
292 | /** |
||
293 | * Validates a submitted data when editing a theme file |
||
294 | * @return bool |
||
295 | */ |
||
296 | protected function validateEditor() |
||
308 | |||
309 | /** |
||
310 | * Validate syntax of a submitted file |
||
311 | */ |
||
312 | protected function validateSyntaxEditor() |
||
329 | |||
330 | /** |
||
331 | * Writes a submitted content to a theme file |
||
332 | */ |
||
333 | protected function saveEditor() |
||
347 | |||
348 | /** |
||
349 | * Whether the current user can save the file |
||
350 | */ |
||
351 | protected function canSaveEditor() |
||
356 | |||
357 | /** |
||
358 | * Controls permissions to save a theme file for the current user |
||
359 | */ |
||
360 | protected function controlAccessSaveEditor() |
||
366 | |||
367 | /** |
||
368 | * Sets titles on the file edit page |
||
369 | */ |
||
370 | protected function setTitleEditEditor() |
||
376 | |||
377 | /** |
||
378 | * Sets breadcrumbs on the file edit page |
||
379 | */ |
||
380 | protected function setBreadcrumbEditEditor() |
||
406 | |||
407 | /** |
||
408 | * Renders the file edit page |
||
409 | */ |
||
410 | protected function outputEditEditor() |
||
414 | |||
415 | /** |
||
416 | * Returns a path to the file to be edited |
||
417 | * @param string $encoded_filename URL encoded base64 hash |
||
418 | */ |
||
419 | protected function setFilePathEditor($encoded_filename) |
||
431 | |||
432 | /** |
||
433 | * Returns a content of the file |
||
434 | * @return string |
||
435 | */ |
||
436 | protected function getFileContentEditor() |
||
440 | |||
441 | /** |
||
442 | * Returns the total number of lines in the file |
||
443 | * @return integer |
||
444 | */ |
||
445 | protected function getFileTotalLinesEditor() |
||
449 | |||
450 | } |
||
451 |