Conditions | 10 |
Paths | 40 |
Total Lines | 41 |
Code Lines | 23 |
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 |
||
52 | public function set($pathParts, $value, &$element = null, array $callbackArguments = []): WalkableArray { |
||
53 | if (!is_array($pathParts)) { |
||
54 | $pathParts = explode(self::TOKEN_NESTING, $pathParts); |
||
55 | } |
||
56 | |||
57 | end($pathParts); |
||
58 | $lastPathKey = key($pathParts); |
||
59 | reset($pathParts); |
||
60 | |||
61 | if (!$element) { |
||
62 | $element = &$this->array; |
||
63 | } |
||
64 | |||
65 | foreach ($pathParts as $key => $pathPart) { |
||
66 | if ($pathPart === self::TOKEN_REPETITION && is_array($element)) { |
||
67 | $partialPath = array_splice($pathParts, $key + 1); |
||
68 | |||
69 | foreach ($element as $itemKey => &$item) { |
||
70 | $this->set($partialPath, $value, $item, [new self($item), $itemKey]); |
||
71 | } |
||
72 | |||
73 | return $this; |
||
74 | } |
||
75 | |||
76 | if (!isset($element[$pathPart])) { |
||
77 | $element[$pathPart] = $key === $lastPathKey ? null : []; |
||
78 | } |
||
79 | |||
80 | $element = &$element[$pathPart]; |
||
81 | } |
||
82 | |||
83 | if ($value instanceof \Closure) { |
||
84 | $element = call_user_func_array($value, $callbackArguments); |
||
85 | } else { |
||
86 | $element = $value; |
||
87 | } |
||
88 | |||
89 | unset($element); |
||
90 | |||
91 | return $this; |
||
92 | } |
||
93 | |||
143 |