| Conditions | 12 |
| Paths | 193 |
| Total Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | <?php |
||
| 83 | public function createUrl( |
||
| 84 | $action, |
||
| 85 | $model, |
||
| 86 | $key, |
||
| 87 | $index, |
||
| 88 | $appendReturnUrl = null, |
||
| 89 | $url_append = null, |
||
| 90 | $keyParam = 'id', |
||
| 91 | $attrs = [] |
||
| 92 | ) { |
||
| 93 | if ($this->urlCreator instanceof Closure) { |
||
| 94 | return call_user_func($this->urlCreator, $action, $model, $key, $index); |
||
| 95 | } else { |
||
| 96 | $params = []; |
||
| 97 | if (is_array($key)) { |
||
| 98 | $params = $key; |
||
| 99 | } else { |
||
| 100 | if (is_null($keyParam) === false) { |
||
| 101 | $params = [$keyParam => (string)$key]; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | $params[0] = $this->controller ? $this->controller . '/' . $action : $action; |
||
| 105 | foreach ($attrs as $attrName) { |
||
| 106 | if ($attrName === 'model') { |
||
| 107 | $params['model'] = $model; |
||
| 108 | } elseif ($attrName === 'mainCategory.category_group_id' && $model->getMainCategory()) { |
||
| 109 | $params['category_group_id'] = $model->getMainCategory()->category_group_id; |
||
| 110 | } else { |
||
| 111 | $params[$attrName] = $model->getAttribute($attrName); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | if (is_null($appendReturnUrl) === true) { |
||
| 115 | $appendReturnUrl = $this->appendReturnUrl; |
||
| 116 | } |
||
| 117 | if (is_null($url_append) === true) { |
||
| 118 | $url_append = $this->url_append; |
||
| 119 | } |
||
| 120 | if ($appendReturnUrl) { |
||
| 121 | $params['returnUrl'] = Helper::getReturnUrl(); |
||
| 122 | } |
||
| 123 | return Url::toRoute($params) . $url_append; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 173 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.