| Conditions | 9 |
| Paths | 9 |
| Total Lines | 57 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 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 |
||
| 57 | public static function parse($definition): array |
||
| 58 | { |
||
| 59 | if (!is_array($definition)) { |
||
| 60 | return [$definition, []]; |
||
| 61 | } |
||
| 62 | |||
| 63 | // Dedicated definition |
||
| 64 | if (isset($definition[self::DEFINITION_META])) { |
||
| 65 | $newDefinition = $definition[self::DEFINITION_META]; |
||
| 66 | unset($definition[self::DEFINITION_META]); |
||
| 67 | |||
| 68 | return [$newDefinition, $definition]; |
||
| 69 | } |
||
| 70 | |||
| 71 | // Callable definition |
||
| 72 | if (is_callable($definition, true)) { |
||
| 73 | return [$definition, []]; |
||
| 74 | } |
||
| 75 | |||
| 76 | // Array definition |
||
| 77 | $meta = []; |
||
| 78 | $class = null; |
||
| 79 | $constructorArguments = []; |
||
| 80 | $methodsAndProperties = []; |
||
| 81 | foreach ($definition as $key => $value) { |
||
| 82 | // Class |
||
| 83 | if ($key === ArrayDefinition::CLASS_NAME) { |
||
| 84 | $class = $value; |
||
| 85 | continue; |
||
| 86 | } |
||
| 87 | |||
| 88 | // Constructor arguments |
||
| 89 | if ($key === ArrayDefinition::CONSTRUCTOR) { |
||
| 90 | $constructorArguments = $value; |
||
| 91 | continue; |
||
| 92 | } |
||
| 93 | |||
| 94 | // Methods and properties |
||
| 95 | if (substr($key, -2) === '()') { |
||
| 96 | $methodsAndProperties[$key] = [ArrayDefinition::TYPE_METHOD, $key, $value]; |
||
|
|
|||
| 97 | continue; |
||
| 98 | } |
||
| 99 | if (strncmp($key, '$', 1) === 0) { |
||
| 100 | $methodsAndProperties[$key] = [ArrayDefinition::TYPE_PROPERTY, $key, $value]; |
||
| 101 | continue; |
||
| 102 | } |
||
| 103 | |||
| 104 | $meta[$key] = $value; |
||
| 105 | } |
||
| 106 | return [ |
||
| 107 | [ |
||
| 108 | $class, |
||
| 109 | $constructorArguments, |
||
| 110 | $methodsAndProperties, |
||
| 111 | self::IS_PREPARED_ARRAY_DEFINITION_DATA => true, |
||
| 112 | ], |
||
| 113 | $meta, |
||
| 114 | ]; |
||
| 117 |