Conditions | 13 |
Paths | 20 |
Total Lines | 106 |
Code Lines | 81 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
140 | protected static function generate( |
||
141 | Config $annotation, |
||
142 | string $className, |
||
143 | string $classNamespace, |
||
144 | string $canonicalClassName, |
||
145 | string $targetClassNamespace, |
||
146 | string $targetCanonicalClassName, |
||
147 | int $time, |
||
148 | int &$subClassIteration = 0 |
||
149 | ): string { |
||
150 | // a suffix of _0, _1, _2 etc. is added to generated sub-classes |
||
151 | $suffix = 0 < $subClassIteration ? '_' . $subClassIteration : ''; |
||
152 | |||
153 | $effectiveClassName = $className . $suffix; |
||
154 | $effectiveTargetCanonicalClassName = $targetCanonicalClassName . $suffix; |
||
155 | |||
156 | $generator = new ClassGenerator($annotation, $effectiveClassName, $targetClassNamespace, $canonicalClassName); |
||
157 | |||
158 | /** |
||
159 | * @var string $key |
||
160 | * @var ConfigEntryInterface $entry |
||
161 | */ |
||
162 | foreach ($annotation->value as $key => $entry) { |
||
163 | switch (true) { |
||
164 | case $entry instanceof ConfigString: |
||
165 | case $entry instanceof ConfigInteger: |
||
166 | case $entry instanceof ConfigFloat: |
||
167 | case $entry instanceof ConfigBoolean: |
||
168 | case $entry instanceof ConfigObject: |
||
169 | $type = $entry->getType(); |
||
170 | $generator |
||
171 | ->generateProperty($key, $type, isset($entry->default) ? $entry->default : null) |
||
172 | ->generateGet($key, $type) |
||
173 | ->generateSet($key, $type) |
||
174 | ->generateIsset($key) |
||
175 | ->generateUnset($key); |
||
176 | break; |
||
177 | |||
178 | case $entry instanceof ConfigList: |
||
179 | $type = isset($entry->value) ? $entry->value->getType() : 'mixed'; |
||
180 | $generator |
||
181 | ->generateProperty($key, $type . '[]', isset($entry->default) ? |
||
|
|||
182 | array_values($entry->default) : null) |
||
183 | ->generateGet($key, $type . '[]') |
||
184 | ->generateListSet($key, $type . '[]') |
||
185 | ->generateListGetAt($key, $type) |
||
186 | ->generateListSetAt($key, $type) |
||
187 | ->generateListPush($key, $type) |
||
188 | ->generateListUnshift($key, $type) |
||
189 | ->generateArrayPop($key, $type) |
||
190 | ->generateArrayShift($key, $type) |
||
191 | ->generateArrayClear($key) |
||
192 | ->generateIsset($key) |
||
193 | ->generateUnset($key); |
||
194 | break; |
||
195 | |||
196 | case $entry instanceof Config: |
||
197 | $subClassIteration++; |
||
198 | $entryCanonicalClassName = static::generate( |
||
199 | $entry, |
||
200 | $className, |
||
201 | $classNamespace, |
||
202 | $canonicalClassName, |
||
203 | $targetClassNamespace, |
||
204 | $targetCanonicalClassName, |
||
205 | $time, |
||
206 | $subClassIteration |
||
207 | ); |
||
208 | $generator |
||
209 | ->generateProperty($key, $entryCanonicalClassName) |
||
210 | ->generateConfigGet($key, $entryCanonicalClassName) |
||
211 | ->generateConfigSet($key) |
||
212 | ->generateConfigIsset($key) |
||
213 | ->generateConfigUnset($key); |
||
214 | break; |
||
215 | |||
216 | default: |
||
217 | throw new \RuntimeException(sprintf( |
||
218 | 'Invalid or unsupported configuration entry type: "%s".', |
||
219 | get_class($entry) |
||
220 | )); |
||
221 | } |
||
222 | } |
||
223 | |||
224 | $generator |
||
225 | ->generateMagicGet() |
||
226 | ->generateMagicSet() |
||
227 | ->generateMagicIsset() |
||
228 | ->generateMagicUnset(); |
||
229 | |||
230 | $targetDir = static::getCachePath() . '/' . str_replace('\\', '/', $classNamespace); |
||
231 | $targetPath = $targetDir . '/' . $effectiveClassName . '.php'; |
||
232 | |||
233 | static::createDirectories($targetDir); |
||
234 | |||
235 | file_put_contents($targetPath, (string) $generator); |
||
236 | touch($targetPath, $time); |
||
237 | clearstatcache(); |
||
238 | |||
239 | // as optimization measure composer's autoloader remembers that a class does not exist on the first requested |
||
240 | // it will refuse to autoload the class even if it subsequently becomes available |
||
241 | // for this reason we need to manually load the newly generated class |
||
242 | include_once $targetPath; |
||
243 | |||
244 | return $effectiveTargetCanonicalClassName; |
||
245 | } |
||
246 | |||
348 | } |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.