| Conditions | 10 |
| Paths | 10 |
| Total Lines | 64 |
| 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 |
||
| 60 | public static function parse(mixed $definition): array |
||
| 61 | { |
||
| 62 | if (!is_array($definition)) { |
||
| 63 | return [$definition, []]; |
||
| 64 | } |
||
| 65 | |||
| 66 | // Dedicated definition |
||
| 67 | if (isset($definition[self::DEFINITION_META])) { |
||
| 68 | /** @var mixed $newDefinition */ |
||
| 69 | $newDefinition = $definition[self::DEFINITION_META]; |
||
| 70 | unset($definition[self::DEFINITION_META]); |
||
| 71 | |||
| 72 | return [$newDefinition, $definition]; |
||
| 73 | } |
||
| 74 | |||
| 75 | // Callable definition |
||
| 76 | if (is_callable($definition, true)) { |
||
| 77 | return [$definition, []]; |
||
| 78 | } |
||
| 79 | |||
| 80 | // Array definition |
||
| 81 | $meta = []; |
||
| 82 | $class = null; |
||
| 83 | $constructorArguments = []; |
||
| 84 | $methodsAndProperties = []; |
||
| 85 | /** @var mixed $value */ |
||
| 86 | foreach ($definition as $key => $value) { |
||
| 87 | if (is_string($key)) { |
||
| 88 | // Class |
||
| 89 | if ($key === ArrayDefinition::CLASS_NAME) { |
||
| 90 | /** @var mixed $class */ |
||
| 91 | $class = $value; |
||
| 92 | continue; |
||
| 93 | } |
||
| 94 | |||
| 95 | // Constructor arguments |
||
| 96 | if ($key === ArrayDefinition::CONSTRUCTOR) { |
||
| 97 | /** @var mixed $constructorArguments */ |
||
| 98 | $constructorArguments = $value; |
||
| 99 | continue; |
||
| 100 | } |
||
| 101 | |||
| 102 | // Methods and properties |
||
| 103 | if (count($methodArray = explode('()', $key, 2)) === 2) { |
||
| 104 | $methodsAndProperties[$key] = [ArrayDefinition::TYPE_METHOD, $methodArray[0], $value]; |
||
| 105 | continue; |
||
| 106 | } |
||
| 107 | if (count($propertyArray = explode('$', $key, 2)) === 2) { |
||
| 108 | $methodsAndProperties[$key] = [ArrayDefinition::TYPE_PROPERTY, $propertyArray[1], $value]; |
||
| 109 | continue; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | /** @var mixed */ |
||
| 114 | $meta[$key] = $value; |
||
| 115 | } |
||
| 116 | return [ |
||
| 117 | [ |
||
| 118 | 'class' => $class, |
||
| 119 | '__construct()' => $constructorArguments, |
||
| 120 | 'methodsAndProperties' => $methodsAndProperties, |
||
| 121 | self::IS_PREPARED_ARRAY_DEFINITION_DATA => true, |
||
| 122 | ], |
||
| 123 | $meta, |
||
| 124 | ]; |
||
| 127 |