Conditions | 14 |
Paths | 12 |
Total Lines | 59 |
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 |
||
70 | private function prepareActualParameters(array $formalParameters, array $parameters): array |
||
71 | { |
||
72 | $result = []; |
||
73 | |||
74 | // Handle named parameters |
||
75 | if ($this->isNamedArray($parameters)) { |
||
76 | |||
77 | foreach ($formalParameters as $formalParameter) { |
||
78 | /** @var \ReflectionParameter $formalParameter */ |
||
79 | |||
80 | $formalType = (string) $formalParameter->getType(); |
||
81 | $name = $formalParameter->name; |
||
82 | |||
83 | if ($formalParameter->isOptional()) { |
||
84 | if (!array_key_exists($name, $parameters)) { |
||
85 | $result[$name] = $formalParameter->getDefaultValue(); |
||
86 | continue; |
||
87 | } |
||
88 | |||
89 | if ($formalParameter->allowsNull() && $parameters[$name] === null) { |
||
90 | $result[$name] = $parameters[$name]; |
||
91 | continue; |
||
92 | } |
||
93 | |||
94 | $result[$name] = $formalParameter->getClass() !== null |
||
95 | ? $this->toObject($formalParameter->getClass()->name, $parameters[$name]) |
||
96 | : $this->matchType($formalType, $parameters[$name]); |
||
97 | } |
||
98 | |||
99 | if (!array_key_exists($name, $parameters)) { |
||
100 | throw new InvalidParamsException('Named parameter error'); |
||
101 | } |
||
102 | |||
103 | $result[$name] = $this->matchType($formalType, $parameters[$name]); |
||
104 | } |
||
105 | |||
106 | return $result; |
||
107 | } |
||
108 | |||
109 | // Handle positional parameters |
||
110 | foreach ($formalParameters as $position => $formalParameter) { |
||
111 | /** @var \ReflectionParameter $formalParameter */ |
||
112 | |||
113 | if ($formalParameter->isOptional() && !isset($parameters[$position])) { |
||
114 | break; |
||
115 | } |
||
116 | |||
117 | if (!isset($parameters[$position])) { |
||
118 | throw new InvalidParamsException('Positional parameter error'); |
||
119 | } |
||
120 | |||
121 | $formalType = (string) $formalParameter->getType(); |
||
122 | $result[] = $formalParameter->getClass() !== null |
||
123 | ? $this->toObject($formalParameter->getClass()->name, $parameters[$position]) |
||
124 | : $this->matchType($formalType, $parameters[$position]); |
||
125 | } |
||
126 | |||
127 | return $result; |
||
128 | } |
||
129 | |||
224 |