Complex classes like FileManager 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 FileManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class FileManager extends Controller |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The current file manager command |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $data_command; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The current absolute path |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $data_absolute_path; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The current relative path |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $data_path; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The current SplFileInfo file object |
||
| 43 | * @var \SplFileInfo $data_file |
||
| 44 | */ |
||
| 45 | protected $data_file; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * An array of SplFileInfo objects of selected files |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $data_selected = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The current access |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | protected $data_access = true; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Command model class instance |
||
| 61 | * @var \gplcart\modules\file_manager\models\Command $command |
||
| 62 | */ |
||
| 63 | protected $command; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Scanner model class instance |
||
| 67 | * @var \gplcart\modules\file_manager\models\Scanner $scanner |
||
| 68 | */ |
||
| 69 | protected $scanner; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * FileManager constructor. |
||
| 73 | * @param Command $command |
||
| 74 | * @param Scanner $scanner |
||
| 75 | */ |
||
| 76 | public function __construct(Command $command, Scanner $scanner) |
||
| 77 | { |
||
| 78 | parent::__construct(); |
||
| 79 | |||
| 80 | $this->command = $command; |
||
| 81 | $this->scanner = $scanner; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Sets the current working file path |
||
| 86 | */ |
||
| 87 | protected function setFileFileManager() |
||
| 88 | { |
||
| 89 | $initial_path = $this->scanner->getInitialPath(); |
||
| 90 | $initial_absolute_path = $this->scanner->getInitialPath(true); |
||
| 91 | $this->data_path = $this->getQuery('path', $initial_path); |
||
| 92 | |||
| 93 | if (empty($this->data_path)) { |
||
| 94 | $this->data_path = $initial_path; |
||
| 95 | } |
||
| 96 | |||
| 97 | $this->data_absolute_path = gplcart_file_absolute($this->data_path); |
||
| 98 | |||
| 99 | if (gplcart_path_starts($this->data_absolute_path, $initial_absolute_path) && file_exists($this->data_absolute_path)) { |
||
| 100 | $this->data_access = true; |
||
| 101 | $this->data_file = new SplFileInfo($this->data_absolute_path); |
||
| 102 | } else { |
||
| 103 | $this->data_access = false; |
||
| 104 | } |
||
| 105 | |||
| 106 | $this->setSelectedFileManager(); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Prepare and set selected files |
||
| 111 | */ |
||
| 112 | protected function setSelectedFileManager() |
||
| 113 | { |
||
| 114 | foreach ($this->session->get('file_manager_selected', array()) as $path) { |
||
| 115 | $file = new SplFileInfo(gplcart_file_absolute($path)); |
||
| 116 | if (is_object($file)) { |
||
| 117 | $this->data_selected[$path] = $file; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Displays the file manager page |
||
| 124 | */ |
||
| 125 | public function viewFileManager() |
||
| 126 | { |
||
| 127 | $this->setFileFileManager(); |
||
| 128 | $this->setAccessFileManager(); |
||
| 129 | $this->setDataMessagesFileManager(); |
||
| 130 | |||
| 131 | $this->setData('access', $this->data_access); |
||
| 132 | $this->setData('command', $this->data_command); |
||
| 133 | $this->setData('selected', $this->data_selected); |
||
| 134 | $this->setData('tabs', $this->getTabsFileManager()); |
||
| 135 | $this->setData('actions', $this->getActionsFileManager()); |
||
| 136 | $this->setData('process_selected', $this->isQuery('selected')); |
||
| 137 | |||
| 138 | $this->submitFileManager(); |
||
| 139 | $this->setDataContendFileManager(); |
||
| 140 | |||
| 141 | $rendered = $this->render('file_manager|filemanager', $this->data); |
||
| 142 | |||
| 143 | if ($this->isQuery('output')) { |
||
| 144 | $this->response->outputHtml($rendered); |
||
| 145 | } |
||
| 146 | |||
| 147 | $this->setJsFileManager(); |
||
| 148 | $this->setCssFileManager(); |
||
| 149 | |||
| 150 | $this->setTitleFileManager(); |
||
| 151 | $this->setBreadcrumbFileManager(); |
||
| 152 | |||
| 153 | $this->setData('rendered_filemanager', $rendered); |
||
| 154 | $this->outputViewFileManager(); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Sets rendered HTML for the current command |
||
| 159 | */ |
||
| 160 | protected function setDataContendFileManager() |
||
| 161 | { |
||
| 162 | if ($this->data_access) { |
||
| 163 | |||
| 164 | $data = $this->command->getView($this->data_command, array($this->data_file, $this)); |
||
| 165 | |||
| 166 | if (is_string($data)) { |
||
| 167 | $this->setMessageFileManager($data, 'warning'); |
||
| 168 | } else { |
||
| 169 | |||
| 170 | settype($data, 'array'); |
||
| 171 | |||
| 172 | $template_data = reset($data); |
||
| 173 | $template = key($data); |
||
| 174 | $template_data['file'] = $this->data_file; |
||
| 175 | $template_data['breadcrumbs'] = $this->getPathBreadcrumbsFileManager(); |
||
| 176 | $rendered = $this->render($template, array_merge($template_data, $this->data)); |
||
| 177 | |||
| 178 | $this->setData('content', $rendered); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Sets messages |
||
| 185 | */ |
||
| 186 | protected function setDataMessagesFileManager() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Sets CSS files |
||
| 196 | */ |
||
| 197 | protected function setCssFileManager() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Sets JS files |
||
| 204 | */ |
||
| 205 | protected function setJsFileManager() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Sets access to the current path |
||
| 213 | * @return bool |
||
| 214 | */ |
||
| 215 | protected function setAccessFileManager() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Whether the current user has access to the file |
||
| 245 | * @param string $path |
||
| 246 | * @return boolean |
||
| 247 | */ |
||
| 248 | protected function accessPathAccessFileManager($path) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Returns an array of path breadcrumbs |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | protected function getPathBreadcrumbsFileManager() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Sets file manager messages |
||
| 291 | * @param string $message |
||
| 292 | * @param string $severity |
||
| 293 | * @param bool $once |
||
| 294 | */ |
||
| 295 | protected function setMessageFileManager($message, $severity, $once = false) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Handles a submitted data |
||
| 308 | */ |
||
| 309 | protected function submitFileManager() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Handles submitted selected items |
||
| 322 | */ |
||
| 323 | protected function submitSelectedFileManager() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Process the current command |
||
| 341 | */ |
||
| 342 | protected function submitCommandFileManager() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Validates a submitted data |
||
| 352 | * @return bool |
||
| 353 | */ |
||
| 354 | protected function validateFileManager() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Returns an array of allowed tabs for the current command |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | protected function getTabsFileManager() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Returns an array of commands that support multiple selection |
||
| 397 | * @return array |
||
| 398 | */ |
||
| 399 | protected function getActionsFileManager() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Sets titles on the file manager page |
||
| 414 | */ |
||
| 415 | protected function setTitleFileManager() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Sets breadcrumbs on the file manager page |
||
| 422 | */ |
||
| 423 | protected function setBreadcrumbFileManager() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Render and display the file manager page |
||
| 442 | */ |
||
| 443 | protected function outputViewFileManager() |
||
| 447 | |||
| 448 | } |
||
| 449 |