| Conditions | 11 |
| Paths | 4 |
| Total Lines | 51 |
| Code Lines | 30 |
| 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 |
||
| 17 | public function array(): array |
||
| 18 | { |
||
| 19 | $entity = $this; |
||
| 20 | $entityMethods = get_class_methods($this); |
||
| 21 | $params = []; |
||
| 22 | |||
| 23 | array_walk($entityMethods, function ($method) use (&$entity, &$params) { |
||
| 24 | if (substr($method, 0, 3) == 'get') { |
||
| 25 | $entityProperty = lcfirst(substr($method, 3)); |
||
| 26 | if (isset($entity->$entityProperty)) { |
||
| 27 | |||
| 28 | if ($entity->$method() instanceof \DateTime) { |
||
| 29 | $ePropertyVal = DateTimeHelper::makeTimestampFromDateString($entity->$method()->format("Y-m-d H:i:s")); |
||
| 30 | } else { |
||
| 31 | $ePropertyVal = (string)$entity->$method(); |
||
| 32 | } |
||
| 33 | |||
| 34 | $params[$entityProperty] = $ePropertyVal; |
||
| 35 | |||
| 36 | $propIndex = array_search($entityProperty, $entity->requiredFields, true); |
||
| 37 | |||
| 38 | if($propIndex > -1) { |
||
| 39 | unset($entity->requiredFields[$propIndex]); |
||
| 40 | } |
||
| 41 | |||
| 42 | if (!empty($entity->requiredBetweenFields)) { |
||
| 43 | foreach ($entity->requiredBetweenFields as $index => $condition) { |
||
| 44 | if (in_array($entityProperty, $condition)) { |
||
| 45 | unset($entity->requiredBetweenFields[$index]); |
||
| 46 | break; |
||
| 47 | } |
||
| 48 | } |
||
| 49 | } |
||
| 50 | } |
||
| 51 | } |
||
| 52 | }); |
||
| 53 | |||
| 54 | if (!empty($this->requiredFields)) { |
||
| 55 | throw new SDKException(sprintf(SDKException::EXCEPTION_REQUIRED_FIELD_TEXT, implode(',', $this->requiredFields))); |
||
| 56 | } |
||
| 57 | |||
| 58 | if (!empty($this->requiredBetweenFields)) { |
||
| 59 | $paramsString = ''; |
||
| 60 | foreach ($this->requiredBetweenFields as $fieldArray) { |
||
| 61 | $paramsString .= implode(' or ', $fieldArray); |
||
| 62 | } |
||
| 63 | $params = $paramsString; |
||
|
|
|||
| 64 | |||
| 65 | throw new SDKException(sprintf(SDKException::EXCEPTION_REQUIRED_SPECIFY_BETWEEN_FIELDS, $paramsString)); |
||
| 66 | } |
||
| 67 | return $params; |
||
| 68 | } |
||
| 81 | } |