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) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Route callback to display the select theme page |
||
| 66 | */ |
||
| 67 | public function themeEditor() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Set title on the select theme page |
||
| 79 | */ |
||
| 80 | protected function setTitleThemeEditor() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Set breadcrumbs on the select theme page |
||
| 87 | */ |
||
| 88 | protected function setBreadcrumbThemeEditor() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Output rendered templates on the select theme page |
||
| 107 | */ |
||
| 108 | protected function outputThemeEditor() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Displays the module file overview page |
||
| 115 | * @param integer $module_id |
||
| 116 | */ |
||
| 117 | public function listEditor($module_id) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns an array of module data |
||
| 132 | * @param string $module_id |
||
| 133 | */ |
||
| 134 | protected function setModuleEditor($module_id) |
||
| 135 | { |
||
| 136 | $this->data_module = $this->module->get($module_id); |
||
| 137 | |||
| 138 | if (empty($this->data_module)) { |
||
| 139 | $this->outputHttpStatus(404); |
||
| 140 | } |
||
| 141 | |||
| 142 | if ($this->data_module['type'] !== 'theme') { |
||
| 143 | $this->outputHttpStatus(403); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns an array of files to edit |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | protected function getFilesEditor() |
||
| 152 | { |
||
| 153 | $data = $this->editor->getList($this->data_module); |
||
| 154 | return $this->prepareFilesEditor($data); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Prepares an array of files to be edited |
||
| 159 | * @param array $data |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | protected function prepareFilesEditor(array $data) |
||
| 163 | { |
||
| 164 | $prepared = array(); |
||
| 165 | foreach ($data as $folder => $files) { |
||
| 166 | foreach ($files as $file) { |
||
| 167 | |||
| 168 | $path = trim(str_replace($this->data_module['directory'], '', $file), '/'); |
||
| 169 | $depth = substr_count(str_replace('\\', '/', $path), '/'); // WIN compatibility |
||
| 170 | |||
| 171 | $pathinfo = pathinfo($path); |
||
| 172 | $directory = is_dir($file); |
||
| 173 | $parent = $directory ? $path : $pathinfo['dirname']; |
||
| 174 | |||
| 175 | $prepared[$folder][$parent][] = array( |
||
| 176 | 'file' => $file, |
||
| 177 | 'path' => $path, |
||
| 178 | 'depth' => $depth, |
||
| 179 | 'directory' => $directory, |
||
| 180 | 'name' => $pathinfo['basename'], |
||
| 181 | 'id' => gplcart_string_encode($path), |
||
| 182 | 'indentation' => str_repeat('<span class="indentation"></span>', $depth) |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | |||
| 186 | ksort($prepared[$folder]); |
||
| 187 | } |
||
| 188 | return $prepared; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Sets title on theme files overview page |
||
| 193 | */ |
||
| 194 | protected function setTitleListEditor() |
||
| 195 | { |
||
| 196 | $text = $this->text('Edit theme %name', array('%name' => $this->data_module['name'])); |
||
| 197 | $this->setTitle($text); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Sets breadcrumbs on theme files overview page |
||
| 202 | */ |
||
| 203 | protected function setBreadcrumbListEditor() |
||
| 204 | { |
||
| 205 | $breadcrumbs = array(); |
||
| 206 | |||
| 207 | $breadcrumbs[] = array( |
||
| 208 | 'url' => $this->url('admin'), |
||
| 209 | 'text' => $this->text('Dashboard') |
||
| 210 | ); |
||
| 211 | |||
| 212 | $breadcrumbs[] = array( |
||
| 213 | 'url' => $this->url('admin/module/list'), |
||
| 214 | 'text' => $this->text('Modules') |
||
| 215 | ); |
||
| 216 | |||
| 217 | $this->setBreadcrumbs($breadcrumbs); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Renders templates of theme files overview page |
||
| 222 | */ |
||
| 223 | protected function outputListEditor() |
||
| 224 | { |
||
| 225 | $this->output('editor|list'); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Displays the file edit page |
||
| 230 | * @param string $module_id |
||
| 231 | * @param string $file_id |
||
| 232 | */ |
||
| 233 | public function editEditor($module_id, $file_id) |
||
| 234 | { |
||
| 235 | $this->setModuleEditor($module_id); |
||
| 236 | $this->setFilePathEditor($file_id); |
||
| 237 | |||
| 238 | $this->setTitleEditEditor(); |
||
| 239 | $this->setBreadcrumbEditEditor(); |
||
| 240 | $this->setMessageEditEditor(); |
||
| 241 | |||
| 242 | $this->setData('module', $this->data_module); |
||
| 243 | $this->setData('can_save', $this->canSaveEditor()); |
||
| 244 | $this->setData('lines', $this->getFileTotalLinesEditor()); |
||
| 245 | $this->setData('editor.content', $this->getFileContentEditor()); |
||
| 246 | |||
| 247 | $this->submitEditor(); |
||
| 248 | |||
| 249 | $this->setJsSettingsEditor(); |
||
| 250 | $this->outputEditEditor(); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Sets messages on the file edit page |
||
| 255 | */ |
||
| 256 | protected function setMessageEditEditor() |
||
| 257 | { |
||
| 258 | if ($this->current_theme['id'] == $this->data_module['id']) { |
||
| 259 | $message = $this->text('You cannot edit the current theme'); |
||
| 260 | $this->setMessage($message, 'warning'); |
||
| 261 | } else if ($this->canSaveEditor() && $this->data_extension === 'php') { |
||
| 262 | $this->setMessage($this->text('Be careful! Invalid PHP code can break down all your site!'), 'danger'); |
||
| 263 | if (!$this->editor->canValidatePhpCode()) { |
||
| 264 | $message = $this->text('PHP syntax validation is disabled due to your environment settings'); |
||
| 265 | $this->setMessage($message, 'warning'); |
||
| 266 | } |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Sets JavaScript settings on the file edit page |
||
| 272 | */ |
||
| 273 | protected function setJsSettingsEditor() |
||
| 274 | { |
||
| 275 | $settings = array( |
||
| 276 | 'readonly' => !$this->canSaveEditor(), |
||
| 277 | 'file_extension' => $this->data_extension |
||
| 278 | ); |
||
| 279 | |||
| 280 | $this->setJsSettings('editor', $settings); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Saves an array of submitted data |
||
| 285 | */ |
||
| 286 | protected function submitEditor() |
||
| 287 | { |
||
| 288 | if ($this->isPosted('save') && $this->validateEditor()) { |
||
| 289 | $this->saveEditor(); |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Validates a submitted data when editing a theme file |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | protected function validateEditor() |
||
| 298 | { |
||
| 299 | $this->setSubmitted('editor', null, false); |
||
| 300 | |||
| 301 | $this->setSubmitted('user_id', $this->uid); |
||
| 302 | $this->setSubmitted('path', $this->data_file); |
||
| 303 | $this->setSubmitted('module', $this->data_module); |
||
| 304 | |||
| 305 | $this->validateSyntaxEditor(); |
||
| 306 | |||
| 307 | return !$this->hasErrors(); |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Validate syntax of a submitted file |
||
| 312 | */ |
||
| 313 | protected function validateSyntaxEditor() |
||
| 314 | { |
||
| 315 | $code = $this->getSubmitted('content'); |
||
| 316 | |||
| 317 | if (empty($code) || $this->data_extension !== 'php') { |
||
| 318 | return null; |
||
| 319 | } |
||
| 320 | |||
| 321 | $result = $this->editor->validatePhpCode($code); |
||
| 322 | |||
| 323 | if (!isset($result) || $result === true) { |
||
| 324 | return null; |
||
| 325 | } |
||
| 326 | |||
| 327 | $error = empty($result) ? $this->text('There is a syntax error in your file') : $result; |
||
| 328 | $this->setError('content', $error); |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Writes a submitted content to a theme file |
||
| 333 | */ |
||
| 334 | protected function saveEditor() |
||
| 335 | { |
||
| 336 | $this->controlAccessSaveEditor(); |
||
| 337 | |||
| 338 | $submitted = $this->getSubmitted(); |
||
| 339 | $result = $this->editor->save($submitted); |
||
| 340 | |||
| 341 | if ($result === true) { |
||
| 342 | $message = $this->text('Theme file has been saved'); |
||
| 343 | $this->redirect("admin/tool/editor/{$submitted['module']['id']}", $message, 'success'); |
||
| 344 | } |
||
| 345 | |||
| 346 | $this->redirect('', $this->text('An error occurred'), 'warning'); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Whether the current user can save the file |
||
| 351 | */ |
||
| 352 | protected function canSaveEditor() |
||
| 353 | { |
||
| 354 | return $this->access('editor_edit')// |
||
| 355 | && $this->current_theme['id'] != $this->data_module['id']; |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Controls permissions to save a theme file for the current user |
||
| 360 | */ |
||
| 361 | protected function controlAccessSaveEditor() |
||
| 362 | { |
||
| 363 | if (!$this->canSaveEditor()) { |
||
| 364 | $this->outputHttpStatus(403); |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Sets titles on the file edit page |
||
| 370 | */ |
||
| 371 | protected function setTitleEditEditor() |
||
| 372 | { |
||
| 373 | $vars = array('%name' => str_replace('\\', '/', gplcart_relative_path($this->data_file))); |
||
| 374 | $this->setTitle($this->text('Edit file %name', $vars)); |
||
| 375 | } |
||
| 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) |
||
| 420 | { |
||
| 421 | $filepath = gplcart_string_decode($encoded_filename); |
||
| 422 | $this->data_file = "{$this->data_module['directory']}/$filepath"; |
||
| 423 | |||
| 424 | if (!is_file($this->data_file) || !is_readable($this->data_file)) { |
||
| 425 | $this->outputHttpStatus(404); |
||
| 426 | } |
||
| 427 | |||
| 428 | $this->data_extension = pathinfo($this->data_file, PATHINFO_EXTENSION); |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Returns a content of the file |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | protected function getFileContentEditor() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Returns the total number of lines in the file |
||
| 442 | * @return integer |
||
| 443 | */ |
||
| 444 | protected function getFileTotalLinesEditor() |
||
| 448 | |||
| 449 | } |
||
| 450 |