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