Conditions | 18 |
Paths | 18 |
Total Lines | 38 |
Code Lines | 33 |
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:
1 | <?php |
||
162 | protected function castValue($value) |
||
163 | { |
||
164 | if (is_null($value)) { |
||
165 | return $value; |
||
166 | } |
||
167 | |||
168 | switch ($this->getCast()) { |
||
169 | case 'int': |
||
170 | case 'integer': |
||
171 | return (int) $value; |
||
172 | case 'real': |
||
173 | case 'float': |
||
174 | case 'double': |
||
175 | return (float) $value; |
||
176 | case 'string': |
||
177 | return (string) $value; |
||
178 | case 'bool': |
||
179 | case 'boolean': |
||
180 | return (bool) $value; |
||
181 | case 'object': |
||
182 | return $this->fromJson($value, true); |
||
183 | case 'array': |
||
184 | case 'json': |
||
185 | return $this->fromJson($value); |
||
186 | case 'collection': |
||
187 | return new Collection($this->fromJson($value)); |
||
188 | case 'comma': |
||
189 | return $this->fromCommaSeparated($value); |
||
190 | case 'date': |
||
191 | return $this->asDate($value); |
||
192 | case 'datetime': |
||
193 | return $this->asDateTime($value); |
||
194 | case 'timestamp': |
||
195 | return $this->asTimestamp($value); |
||
196 | default: |
||
197 | return $value; |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.