| Conditions | 11 |
| Paths | 170 |
| Total Lines | 49 |
| Code Lines | 29 |
| Lines | 7 |
| Ratio | 14.29 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php /** MicroController */ |
||
| 33 | public function action($name = 'index') |
||
|
|
|||
| 34 | { |
||
| 35 | // Set widgetStack for widgets |
||
| 36 | if (empty($GLOBALS['widgetStack'])) { |
||
| 37 | $GLOBALS['widgetStack'] = []; |
||
| 38 | } |
||
| 39 | |||
| 40 | $actionClass = false; |
||
| 41 | |||
| 42 | if (!method_exists($this, 'action'.ucfirst($name))) { |
||
| 43 | $actionClass = $this->getActionClassByName($name); |
||
| 44 | |||
| 45 | if (!$actionClass) { |
||
| 46 | throw new Exception('Action `'.$name.'` not found into '.get_class($this)); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | $filters = method_exists($this, 'filters') ? $this->filters() : []; |
||
| 51 | $result = $this->applyFilters($name, true, $filters, null); |
||
| 52 | |||
| 53 | if ($result instanceof ResponseInterface) { |
||
| 54 | return $result; |
||
| 55 | } |
||
| 56 | |||
| 57 | View Code Duplication | if ($actionClass) { |
|
| 58 | /** @var \Micro\mvc\Action $cl */ |
||
| 59 | $cl = new $actionClass(); |
||
| 60 | $view = $cl->run(); |
||
| 61 | } else { |
||
| 62 | $view = $this->{'action'.ucfirst($name)}(); |
||
| 63 | } |
||
| 64 | |||
| 65 | if (is_object($view)) { |
||
| 66 | $view->module = get_class($this->module); |
||
| 67 | $view->layout = $view->layout ?: $this->layout; |
||
| 68 | $view->view = $view->view ?: $name; |
||
| 69 | $view->path = get_called_class(); |
||
| 70 | $view = $view->render(); |
||
| 71 | } |
||
| 72 | |||
| 73 | $response = (new ResponseInjector)->build(); |
||
| 74 | if (!$response) { |
||
| 75 | throw new Exception('Component `response` not configured'); |
||
| 76 | } |
||
| 77 | $stream = $response->getBody(); |
||
| 78 | $stream->write($this->applyFilters($name, false, $filters, $view)); |
||
| 79 | |||
| 80 | return $response->withBody($stream); |
||
| 81 | } |
||
| 82 | |||
| 112 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: