| Conditions | 11 |
| Paths | 13 |
| Total Lines | 43 |
| Lines | 6 |
| Ratio | 13.95 % |
| 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:
| 1 | <?php |
||
| 115 | public function apply($query) |
||
| 116 | { |
||
| 117 | $value = $this->value(); |
||
| 118 | if (is_null($value) || $value == self::NO_INPUT_APPLIED) { |
||
| 119 | return; |
||
| 120 | } |
||
| 121 | |||
| 122 | if ($this->field()->isRelationField()) { |
||
| 123 | $values = is_array($value) ? $value : [$value]; |
||
| 124 | $model = $this->field()->getModel(); |
||
| 125 | $model = new $model; |
||
| 126 | |||
| 127 | foreach ($this->field()->getRelations() as $index => $relation) { |
||
| 128 | $groupValues = $values; |
||
| 129 | if ($this->field()->isGroupedRelation()) { |
||
| 130 | $groupValues = []; |
||
| 131 | View Code Duplication | foreach ($values as $value) { |
|
| 132 | list($groupHash, $groupValue) = explode('~~~', $value); |
||
| 133 | if ($groupHash == crc32($relation['group'])) { |
||
| 134 | $groupValues[] = $groupValue; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | if ($groupValues) { |
||
| 140 | $this->applyRelationValues($model, $query, $groupValues, $index); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | return; |
||
| 145 | } |
||
| 146 | |||
| 147 | if ($this->field()->isMultiple()) { |
||
| 148 | $query->whereIn($this->field()->name(), $value); |
||
| 149 | return; |
||
| 150 | } |
||
| 151 | |||
| 152 | $query->where( |
||
| 153 | $this->field()->name(), |
||
| 154 | $this->sign, |
||
| 155 | $value |
||
| 156 | ); |
||
| 157 | } |
||
| 158 | |||
| 170 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: