| Conditions | 11 |
| Paths | 4 |
| Total Lines | 43 |
| Code Lines | 25 |
| 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 |
||
| 15 | public function fetchArray(): array |
||
| 16 | { |
||
| 17 | $entity = $this; |
||
| 18 | $entityMethods = get_class_methods($this); |
||
| 19 | $params = []; |
||
| 20 | |||
| 21 | array_walk($entityMethods, function ($method) use (&$entity, &$params) { |
||
| 22 | if (substr($method, 0, 3) == 'get') { |
||
| 23 | $entityProperty = lcfirst(substr($method, 3)); |
||
| 24 | if (isset($entity->$entityProperty)) { |
||
| 25 | $params[$entityProperty] = (string)$entity->$method(); |
||
| 26 | |||
| 27 | $propIndex = array_search($entityProperty, $entity->requiredFields, true); |
||
| 28 | if($propIndex > -1 && !empty($params[$entityProperty])) { |
||
| 29 | unset($entity->requiredFields[$propIndex]); |
||
| 30 | } |
||
| 31 | |||
| 32 | if (!empty($entity->requiredBetweenFields)) { |
||
| 33 | foreach ($entity->requiredBetweenFields as $index => $condition) { |
||
| 34 | if (in_array($entityProperty, $condition)) { |
||
| 35 | unset($entity->requiredBetweenFields[$index]); |
||
| 36 | break; |
||
| 37 | } |
||
| 38 | } |
||
| 39 | } |
||
| 40 | } |
||
| 41 | } |
||
| 42 | }); |
||
| 43 | |||
| 44 | if (!empty($this->requiredFields)) { |
||
| 45 | throw new \Exception("You must specify the following parameters: " . implode(',', $this->requiredFields)); |
||
| 46 | } |
||
| 47 | |||
| 48 | if (!empty($this->requiredBetweenFields)) { |
||
| 49 | $paramsString = ''; |
||
| 50 | foreach ($this->requiredBetweenFields as $fieldArray) { |
||
| 51 | $paramsString .= implode(' or ', $fieldArray); |
||
| 52 | } |
||
| 53 | $params = $paramsString; |
||
|
|
|||
| 54 | |||
| 55 | throw new \Exception("One of two parameters must be specified {$paramsString}"); |
||
| 56 | } |
||
| 57 | return $params; |
||
| 58 | } |
||
| 71 | } |