| Conditions | 10 |
| Paths | 162 |
| Total Lines | 43 |
| Code Lines | 25 |
| Lines | 7 |
| Ratio | 16.28 % |
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 */ |
||
| 32 | public function action($name = 'index') |
||
|
|
|||
| 33 | { |
||
| 34 | // Set widgetStack for widgets |
||
| 35 | if (empty($GLOBALS['widgetStack'])) { |
||
| 36 | $GLOBALS['widgetStack'] = []; |
||
| 37 | } |
||
| 38 | |||
| 39 | $view = null; |
||
| 40 | $actionClass = false; |
||
| 41 | |||
| 42 | |||
| 43 | if (!method_exists($this, 'action' . ucfirst($name))) { |
||
| 44 | $actionClass = $this->getActionClassByName($name); |
||
| 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 | |||
| 52 | $this->applyFilters($name, true, $filters, null); |
||
| 53 | |||
| 54 | View Code Duplication | if ($actionClass) { |
|
| 55 | /** @var \Micro\mvc\Action $cl */ |
||
| 56 | $cl = new $actionClass($this->container); |
||
| 57 | $view = $cl->run(); |
||
| 58 | } else { |
||
| 59 | $view = $this->{'action' . ucfirst($name)}(); |
||
| 60 | } |
||
| 61 | |||
| 62 | if (is_object($view)) { |
||
| 63 | $view->module = get_class($this->module); |
||
| 64 | $view->layout = $view->layout ?: $this->layout; |
||
| 65 | $view->view = $view->view ?: $name; |
||
| 66 | $view->path = get_called_class(); |
||
| 67 | $view = (string)$view; |
||
| 68 | } |
||
| 69 | |||
| 70 | $response = $this->container->response ?: new Response; |
||
| 71 | $response->setBody($this->applyFilters($name, false, $filters, $view)); |
||
| 72 | |||
| 73 | return $response; |
||
| 74 | } |
||
| 75 | |||
| 95 |
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: