Conditions | 10 |
Paths | 15 |
Total Lines | 38 |
Code Lines | 23 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
141 | public static function parse($definition, array $allowedMeta): array |
||
142 | { |
||
143 | if (!is_array($definition)) { |
||
144 | return [$definition, []]; |
||
145 | } |
||
146 | |||
147 | $meta = []; |
||
148 | if (isset($definition[self::DEFINITION_META])) { |
||
|
|||
149 | $newDefinition = $definition[self::DEFINITION_META]; |
||
150 | unset($definition[self::DEFINITION_META]); |
||
151 | $meta = array_filter($definition, function ($key) use ($allowedMeta) { |
||
152 | return in_array($key, $allowedMeta); |
||
153 | }, ARRAY_FILTER_USE_KEY); |
||
154 | $definition = $newDefinition; |
||
155 | } |
||
156 | |||
157 | foreach ($definition as $key => $value) { |
||
158 | // Method. |
||
159 | if ($key === ArrayDefinition::CLASS_NAME || $key === ArrayDefinition::CONSTRUCTOR) { |
||
160 | continue; |
||
161 | } elseif (substr($key, -2) === '()') { |
||
162 | if (!is_array($value)) { |
||
163 | throw new InvalidConfigException( |
||
164 | sprintf('Invalid definition: incorrect method arguments. Expected array, got %s.', self::getType($value)) |
||
165 | ); |
||
166 | } |
||
167 | // Not property = meta. |
||
168 | } elseif (substr($key, 0, 1) !== '@') { |
||
169 | |||
170 | if (!in_array($key, $allowedMeta, true)) { |
||
171 | throw new InvalidConfigException(sprintf('Invalid definition: metadata "%s" is not allowed. Did you mean "%s()" or "@%s"?', $key, $key, $key)); |
||
172 | } |
||
173 | $meta[$key] = $value; |
||
174 | unset($definition[$key]); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | return [$definition, $meta]; |
||
179 | } |
||
189 |