| Conditions | 12 |
| Paths | 21 |
| Total Lines | 39 |
| Code Lines | 19 |
| 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 |
||
| 18 | * @return array |
||
| 19 | * @throws \ReflectionException |
||
| 20 | */ |
||
| 21 | public function getArrayCopy() |
||
| 22 | { |
||
| 23 | $data = []; |
||
| 24 | $properties = get_object_vars($this); |
||
| 25 | $reflectionClass = new \Zend_Reflection_Class($this); |
||
| 26 | foreach ($properties as $property => $value) { |
||
| 27 | // Skip property if it is not accessible |
||
| 28 | if (! $reflectionClass->hasProperty($property)) { |
||
| 29 | continue; |
||
| 30 | } |
||
| 31 | |||
| 32 | // Only process properties that aren't null |
||
| 33 | if (! is_null($value)) { |
||
| 34 | $object = (is_object($value) && ! $this->$property instanceof Expr); |
||
| 35 | |||
| 36 | // Prepend 'get' to the getter method. |
||
| 37 | $getter = 'get' . ucfirst($property); |
||
| 38 | if (! method_exists($this, $getter)) { |
||
| 39 | // If 'getSomething' doesn't exist, try to use 'isSomething' |
||
| 40 | $getter = 'is' . ucfirst($property); |
||
| 41 | } |
||
| 42 | |||
| 43 | // Finally simply use the property |
||
| 44 | if (! method_exists($this, $getter)) { |
||
| 45 | $getter = $property; |
||
| 46 | } |
||
| 47 | |||
| 48 | // Abort if none of the above methods exit |
||
| 49 | if (! method_exists($this, $getter)) { |
||
| 50 | continue; |
||
| 51 | } |
||
| 52 | |||
| 53 | // Assign the contents of the property to the data array |
||
| 54 | $data[$property] = $object ? $this->$getter()->getArrayCopy() : $this->$getter(); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 107 |