Total Complexity | 72 |
Total Lines | 649 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 0 |
Complex classes like app_Controller 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.
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 app_Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | class app_Controller extends bab_Controller implements app_Object_Interface |
||
46 | { |
||
47 | /** |
||
48 | * @var Func_App |
||
49 | */ |
||
50 | protected $app = null; |
||
51 | |||
52 | |||
53 | |||
54 | /** |
||
55 | * @var string[] |
||
56 | */ |
||
57 | protected $reloadSelectors = array(); |
||
58 | |||
59 | |||
60 | |||
61 | /** |
||
62 | * @param string $reloadSelector |
||
63 | */ |
||
64 | public function addReloadSelector($reloadSelector) |
||
65 | { |
||
66 | $this->reloadSelectors[$reloadSelector] = $reloadSelector; |
||
67 | return $this; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @return string[] |
||
72 | */ |
||
73 | public function getReloadSelectors() |
||
74 | { |
||
75 | return $this->reloadSelectors; |
||
76 | } |
||
77 | |||
78 | |||
79 | /** |
||
80 | * Can be used in a controller to check if the current request was made by ajax. |
||
81 | * @var bool |
||
82 | */ |
||
83 | protected $isAjaxRequest = null; |
||
84 | |||
85 | |||
86 | /** |
||
87 | * Name of method to use to create action |
||
88 | * @var string |
||
89 | */ |
||
90 | public $createActionMethod = 'createActionForTg'; |
||
91 | |||
92 | |||
93 | |||
94 | /** |
||
95 | * @param Func_App $app |
||
96 | */ |
||
97 | public function __construct(Func_App $app = null) |
||
98 | { |
||
99 | $this->setApp($app); |
||
100 | } |
||
101 | |||
102 | |||
103 | /** |
||
104 | * {@inheritDoc} |
||
105 | * @see app_Object::setApp() |
||
106 | */ |
||
107 | public function setApp(Func_App $app = null) |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * {@inheritDoc} |
||
115 | * @see app_Object::App() |
||
116 | */ |
||
117 | public function App() |
||
118 | { |
||
119 | return $this->app; |
||
120 | } |
||
121 | |||
122 | |||
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | protected function getControllerTg() |
||
129 | } |
||
130 | |||
131 | |||
132 | /** |
||
133 | * Can be used in a controller to check if the current request was made by ajax. |
||
134 | * @return bool |
||
135 | */ |
||
136 | public function isAjaxRequest() |
||
142 | } |
||
143 | |||
144 | |||
145 | /** |
||
146 | * Ensures that the user is logged in. |
||
147 | * |
||
148 | * Tries to use the appropriate authentication type depending on the situation. |
||
149 | */ |
||
150 | public function requireCredential($message = null) |
||
163 | } |
||
164 | |||
165 | |||
166 | |||
167 | |||
168 | /** |
||
169 | * Returns an instance of the specific controller class. |
||
170 | * If $proxy is true, a proxy to this controller is returned instead. |
||
171 | * |
||
172 | * @deprecated use $App->ControllerProxy() instead |
||
173 | * |
||
174 | * @param string $classname |
||
175 | * @param bool $proxy |
||
176 | * @return app_Controller |
||
177 | */ |
||
178 | public static function getInstance($classname, $proxy = true) |
||
179 | { |
||
180 | |||
181 | |||
182 | // If the app object was not specified (through the setApp() method) |
||
183 | // we try to select one according to the classname prefix. |
||
184 | list($prefix) = explode('_', __CLASS__); |
||
185 | $functionalityName = ucwords($prefix); |
||
186 | $App = @bab_functionality::get('App/' . $functionalityName); |
||
187 | |||
188 | if (!$App) |
||
189 | { |
||
190 | throw new app_Exception('Faild to autodetect functionality App/' . $functionalityName.', the getInstance method is deprecated, use $App->ControllerProxy() instead'); |
||
191 | } |
||
192 | |||
193 | $controller = $App->ControllerProxy($classname, $proxy); |
||
194 | |||
195 | return $controller; |
||
196 | } |
||
197 | |||
198 | |||
199 | /** |
||
200 | * Dynamically creates a proxy class for this controller with all public, non-final and non-static functions |
||
201 | * overriden so that they return an action (Widget_Action) corresponding to each of them. |
||
202 | * |
||
203 | * @param string $classname |
||
204 | * @return app_Controller |
||
205 | */ |
||
206 | static function getProxyInstance(Func_App $App, $classname) |
||
207 | { |
||
208 | $class = new ReflectionClass($classname); |
||
209 | $proxyClassname = $class->name . self::PROXY_CLASS_SUFFIX; |
||
210 | if (!class_exists($proxyClassname)) { |
||
211 | $classStr = 'class ' . $proxyClassname . ' extends ' . $class->name . ' {' . "\n"; |
||
212 | $methods = $class->getMethods(); |
||
213 | |||
214 | $classStr .= ' public function __construct(Func_App $App) { |
||
215 | $this->setApp($App); |
||
216 | }' . "\n"; |
||
217 | |||
218 | foreach ($methods as $method) { |
||
219 | if ($method->name === '__construct' || !$method->isPublic() || $method->isStatic() || $method->isFinal() || $method->name === 'setApp' || $method->name === 'App') { |
||
220 | continue; |
||
221 | } |
||
222 | |||
223 | |||
224 | $classStr .= ' public function ' . $method->name . '('; |
||
225 | $parameters = $method->getParameters(); |
||
226 | $parametersStr = array(); |
||
227 | foreach ($parameters as $parameter) { |
||
228 | |||
229 | if ($parameter->isDefaultValueAvailable()) { |
||
230 | $parametersStr[] = '$' . $parameter->name . ' = ' . var_export($parameter->getDefaultValue(), true); |
||
231 | } else { |
||
232 | $parametersStr[] = '$' . $parameter->name; |
||
233 | } |
||
234 | } |
||
235 | $classStr .= implode(', ', $parametersStr); |
||
236 | $classStr .= ') {' . "\n"; |
||
237 | $classStr .= ' $args = func_get_args();' . "\n"; |
||
238 | $classStr .= ' return $this->getMethodAction(__FUNCTION__, $args);' . "\n"; |
||
239 | $classStr .= ' }' . "\n"; |
||
240 | } |
||
241 | $classStr .= '}' . "\n"; |
||
242 | |||
243 | // We define the proxy class |
||
244 | eval($classStr); |
||
245 | } |
||
246 | |||
247 | $proxy = new $proxyClassname($App); |
||
248 | |||
249 | //$proxy = bab_getInstance($proxyClassname); |
||
250 | return $proxy; |
||
251 | } |
||
252 | |||
253 | |||
254 | /** |
||
255 | * @return self |
||
256 | */ |
||
257 | public function proxy() |
||
258 | { |
||
259 | return self::getProxyInstance($this->App(), get_class($this)); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Get proxy class with action from the new addon controller |
||
264 | * @return self |
||
265 | */ |
||
266 | protected function quickProxy() |
||
267 | { |
||
268 | $proxy = $this->proxy(); |
||
269 | $proxy->createActionMethod = 'createActionForAddon'; |
||
270 | return $proxy; |
||
271 | } |
||
272 | |||
273 | |||
274 | |||
275 | /** |
||
276 | * {@inheritDoc} |
||
277 | * @see bab_Controller::getObjectName() |
||
278 | */ |
||
279 | protected function getObjectName($classname) |
||
280 | { |
||
281 | list(, $objectname) = explode('_Ctrl', $classname); |
||
282 | return strtolower($objectname); |
||
283 | } |
||
284 | |||
285 | |||
286 | |||
287 | // /** |
||
288 | // * Returns the action object corresponding to the current object method $methodName |
||
289 | // * with the parameters $args. |
||
290 | // * |
||
291 | // * @param string $methodName |
||
292 | // * @param array $args |
||
293 | // * @return Widget_Action Or null on error. |
||
294 | // */ |
||
295 | // protected function getMethodAction($methodName, $args) |
||
296 | // { |
||
297 | // $classname = substr(get_class($this), 0, -strlen(self::PROXY_CLASS_SUFFIX)); |
||
298 | // if (!method_exists($classname, $methodName)) { |
||
299 | // throw new bab_InvalidActionException($classname . '::' . $methodName); |
||
300 | // } |
||
301 | // $cls = new ReflectionClass($classname); |
||
302 | // $filename = $cls->getFileName(); |
||
303 | |||
304 | // $method = new ReflectionMethod($classname, $methodName); |
||
305 | |||
306 | // $objectName = basename($filename, '.ctrl.php'); |
||
307 | // $parameters = $method->getParameters(); |
||
308 | // $actionParams = array(); |
||
309 | // $argNumber = 0; |
||
310 | // foreach ($parameters as $parameter) { |
||
311 | // $parameterName = $parameter->getName(); |
||
312 | // if (isset($args[$argNumber])) { |
||
313 | // $actionParams[$parameterName] = $args[$argNumber]; |
||
314 | // } elseif ($parameter->isDefaultValueAvailable()) { |
||
315 | // $actionParams[$parameterName] = $parameter->getDefaultValue(); |
||
316 | // } else { |
||
317 | // $actionParams[$parameterName] = null; |
||
318 | // } |
||
319 | // $argNumber++; |
||
320 | // } |
||
321 | |||
322 | // /* @var $action Widget_Action */ |
||
323 | // $action = $this->{$this->createActionMethod}($objectName. '.' . $methodName, $actionParams); |
||
324 | |||
325 | // $docComment = $method->getDocComment(); |
||
326 | |||
327 | // if (strpos($docComment, '@ajax') !== false) { |
||
328 | // $action->setAjax(true); |
||
329 | // } |
||
330 | // if (strpos($docComment, '@requireSaveMethod') !== false && method_exists($action, 'setRequireSaveMethod')) { |
||
331 | // $action->setRequireSaveMethod(true); |
||
332 | // } |
||
333 | // if (strpos($docComment, '@requireDeleteMethod') !== false && method_exists($action, 'setRequireDeleteMethod')) { |
||
334 | // $action->setRequireDeleteMethod(true); |
||
335 | // } |
||
336 | |||
337 | // return $action; |
||
338 | // } |
||
339 | |||
340 | |||
341 | /** |
||
342 | * @return Widget_Action |
||
343 | */ |
||
344 | protected function createActionForTg($idx, $actionParams) |
||
345 | { |
||
346 | $action = new Widget_Action(); |
||
347 | |||
348 | $action->setMethod($this->App()->controllerTg, $idx, $actionParams); |
||
349 | |||
350 | return $action; |
||
351 | } |
||
352 | |||
353 | |||
354 | /** |
||
355 | * @return Widget_Action |
||
356 | */ |
||
357 | protected function createActionForAddon($idx, $actionParams) |
||
358 | { |
||
359 | $App = $this->App(); |
||
360 | $action = new Widget_Action(); |
||
361 | |||
362 | $action->setParameters($actionParams); |
||
363 | |||
364 | |||
365 | list(,,$file) = explode('/', $App->controllerTg); |
||
366 | |||
367 | $action->setParameter('addon', $App->getAddonName().'.'.$file); |
||
368 | $action->setParameter('idx', $idx); |
||
369 | |||
370 | return $action; |
||
371 | } |
||
372 | |||
373 | |||
374 | |||
375 | /** |
||
376 | * Tries to dispatch the action to the correct sub-controller. |
||
377 | * |
||
378 | * @param Widget_Action $action |
||
379 | * @return mixed |
||
380 | */ |
||
381 | final public function execute(Widget_Action $action) |
||
541 | } |
||
542 | |||
543 | private function deletedItemPage(Widget_Action $action, app_DeletedRecordException $e) |
||
544 | { |
||
545 | if ($this->isAjaxRequest()) { |
||
546 | header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden', true, 403); |
||
547 | header('Cache-Control: no-cache, must-revalidate'); |
||
548 | header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); |
||
549 | header('Content-type: application/json'); |
||
550 | |||
551 | die( |
||
552 | app_json_encode( |
||
553 | |||
554 | array( |
||
555 | 'exception' => 'app_SaveException', |
||
556 | 'message' => bab_convertStringFromDatabase($e->getMessage(), 'UTF-8'), |
||
557 | 'errorMessage' => bab_convertStringFromDatabase($e->getMessage(), 'UTF-8'), |
||
558 | ) |
||
559 | ) |
||
560 | ); |
||
561 | |||
562 | } |
||
563 | header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found', true, 404); |
||
564 | |||
565 | $App = $this->App(); |
||
566 | $W = bab_Widgets(); |
||
567 | |||
568 | $page = $App->Ui()->Page(); |
||
569 | |||
570 | $dialog = $W->Frame(null, $W->VBoxLayout()->setVerticalSpacing(2, 'em')) |
||
571 | ->addClass(Func_Icons::ICON_LEFT_16); |
||
572 | |||
573 | $page->addItem($dialog); |
||
574 | |||
575 | //TRANSLATORS: %s can be task, contact, organization ... |
||
576 | $dialog->addItem($W->Title(sprintf(app_translate('This %s has been deleted'), $e->getObjectTitle()), 1)); |
||
577 | |||
578 | if ($e instanceof app_DeletedRecordException) { |
||
579 | $dialog->addItem( |
||
580 | $W->VBoxItems( |
||
581 | $e->getDeletedBy(), |
||
582 | $e->getDeletedOn() |
||
583 | ) |
||
584 | ); |
||
585 | } |
||
586 | |||
587 | $page->displayHtml(); |
||
588 | |||
589 | } |
||
590 | |||
591 | |||
592 | |||
593 | |||
594 | private function notFoundPage(Widget_Action $action, app_NotFoundException $e) |
||
622 | |||
623 | } |
||
624 | |||
625 | |||
626 | |||
627 | private function errorPage(Exception $e) |
||
628 | { |
||
641 | } |
||
642 | |||
643 | |||
644 | |||
645 | /** |
||
646 | * Method to call before saving |
||
647 | * @since 1.0.22 |
||
648 | * @return self |
||
649 | */ |
||
650 | public function requireSaveMethod() |
||
651 | { |
||
652 | if ('GET' === $_SERVER['REQUEST_METHOD']) { |
||
653 | throw new app_Exception('Method not allowed'); |
||
654 | } |
||
655 | |||
656 | return $this; |
||
657 | } |
||
658 | |||
659 | |||
660 | /** |
||
661 | * Method to call before deleting |
||
662 | * @since 1.0.22 |
||
663 | * @return self |
||
664 | */ |
||
665 | public function requireDeleteMethod() |
||
672 | } |
||
673 | |||
674 | |||
675 | /** |
||
676 | * Custom fields |
||
677 | * @return app_CtrlCustomField |
||
678 | */ |
||
679 | public function CustomField($proxy = true) |
||
680 | { |
||
681 | require_once APP_CTRL_PATH . 'customfield.ctrl.php'; |
||
682 | return $this->App()->ControllerProxy('app_CtrlCustomField', $proxy); |
||
683 | } |
||
684 | |||
685 | |||
686 | /** |
||
687 | * Custom sections |
||
688 | * @return app_CtrlCustomSection |
||
689 | */ |
||
690 | public function CustomSection($proxy = true) |
||
694 | } |
||
695 | } |
||
696 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: