| Conditions | 29 |
| Paths | > 20000 |
| Total Lines | 175 |
| Code Lines | 116 |
| 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 |
||
| 225 | function documentMethod(ReflectionMethod $method, array &$output, string $class, string $instanceVariableName, ?string $facadeName = null): void |
||
| 226 | { |
||
| 227 | $template = <<<'MARKDOWN' |
||
| 228 | #### `{{ $methodName }}()` |
||
| 229 | |||
| 230 | {{ $description }} |
||
| 231 | |||
| 232 | ```php |
||
| 233 | // torchlight! {"lineNumbers": false} |
||
| 234 | {{ $signature }}({{ $argList }}): {{ $returnType }} |
||
| 235 | ``` |
||
| 236 | |||
| 237 | MARKDOWN; |
||
| 238 | |||
| 239 | $staticSignatureTemplate = '{{ $className }}::{{ $methodName }}'; |
||
| 240 | $instanceSignatureTemplate = '{{ $instanceVariableName }}->{{ $methodName }}'; |
||
| 241 | $facadeSignatureTemplate = '{{ $facadeName }}::{{ $methodName }}'; |
||
| 242 | |||
| 243 | $signatureTemplate = $method->isStatic() ? $staticSignatureTemplate : $instanceSignatureTemplate; |
||
| 244 | |||
| 245 | if ($facadeName !== null) { |
||
| 246 | $signatureTemplate = $facadeSignatureTemplate; |
||
| 247 | } |
||
| 248 | |||
| 249 | if ($method->getName() === '__construct') { |
||
| 250 | $signatureTemplate = '{{ $instanceVariableName }} = new {{ $className }}'; |
||
| 251 | } |
||
| 252 | |||
| 253 | $methodName = $method->getName(); |
||
| 254 | |||
| 255 | // If method uses inheritdoc, use the parent method's docblock |
||
| 256 | if ($method->getDocComment() !== false && str_contains(strtolower($method->getDocComment()), '@inheritdoc')) { |
||
| 257 | try { |
||
| 258 | $parentMethod = $method->getPrototype(); |
||
| 259 | $docComment = $parentMethod->getDocComment(); |
||
| 260 | } catch (ReflectionException $e) { |
||
| 261 | // if method is for constructor, getPrototype() will throw an exception, |
||
| 262 | // so we check if exception is for constructor and if so, we use the parent class's constructor |
||
| 263 | if ($method->getName() === '__construct') { |
||
| 264 | $parentClass = $method->getDeclaringClass()->getParentClass(); |
||
| 265 | $parentMethod = $parentClass->getMethod('__construct'); |
||
| 266 | $docComment = $parentMethod->getDocComment(); |
||
| 267 | } else { |
||
| 268 | $docComment = null; |
||
| 269 | } |
||
| 270 | } |
||
| 271 | } else { |
||
| 272 | $docComment = $method->getDocComment(); |
||
| 273 | } |
||
| 274 | |||
| 275 | $PHPDocs = parsePHPDocs($docComment ?: ''); |
||
| 276 | $description = $PHPDocs['description']; |
||
| 277 | |||
| 278 | $className = class_basename($class); |
||
| 279 | |||
| 280 | $parameters = array_map(function (ReflectionParameter $parameter) { |
||
| 281 | $name = '$'.$parameter->getName(); |
||
| 282 | if ($parameter->getType()) { |
||
| 283 | if ($parameter->getType() instanceof ReflectionUnionType) { |
||
| 284 | $type = implode('|', array_map(function (ReflectionNamedType $type) { |
||
| 285 | return $type->getName(); |
||
| 286 | }, $parameter->getType()->getTypes())); |
||
| 287 | } else { |
||
| 288 | $type = $parameter->getType()->getName(); |
||
| 289 | } |
||
| 290 | } else { |
||
| 291 | $type = 'mixed'; |
||
| 292 | } |
||
| 293 | |||
| 294 | return trim($type.' '.$name); |
||
| 295 | }, $method->getParameters()); |
||
| 296 | |||
| 297 | // If return is union type |
||
| 298 | if ($method->getReturnType() instanceof ReflectionUnionType) { |
||
| 299 | $returnType = implode('|', array_map(function (ReflectionNamedType $type) { |
||
| 300 | return $type->getName(); |
||
| 301 | }, $method->getReturnType()->getTypes())); |
||
| 302 | } else { |
||
| 303 | $returnType = $method->getReturnType() ? $method->getReturnType()->getName() : 'void'; |
||
| 304 | } |
||
| 305 | |||
| 306 | // If higher specificity return type is provided in docblock, use that instead |
||
| 307 | if (isset($PHPDocs['properties']['return'])) { |
||
| 308 | $returnValue = $PHPDocs['properties']['return']; |
||
| 309 | // If there is a description, put it in a comment |
||
| 310 | if (str_contains($returnValue, ' ')) { |
||
| 311 | $exploded = explode(' ', $returnValue, 2); |
||
| 312 | // If is not generic |
||
| 313 | if (! str_contains($exploded[0], '<')) { |
||
| 314 | $type = $exploded[0]; |
||
| 315 | $comment = ' // '.$exploded[1]; |
||
| 316 | $returnValue = $type; |
||
| 317 | } else { |
||
| 318 | $comment = null; |
||
| 319 | } |
||
| 320 | } else { |
||
| 321 | $comment = null; |
||
| 322 | } |
||
| 323 | $returnType = $returnValue.($comment ?? ''); |
||
| 324 | } |
||
| 325 | |||
| 326 | $parameterDocs = []; |
||
| 327 | // Map docblock params |
||
| 328 | if (isset($PHPDocs['properties']['params'])) { |
||
| 329 | $newParams = array_map(function (string $param) use (&$parameterDocs) { |
||
| 330 | $param = str_replace(' ', ' ', trim($param)); |
||
| 331 | $comment = $param; |
||
| 332 | $param = explode(' ', $param, 3); |
||
| 333 | $type = $param[0]; |
||
| 334 | $name = $param[1]; |
||
| 335 | if (isset($param[2])) { |
||
| 336 | $parameterDocs[$type] = $comment; |
||
| 337 | } |
||
| 338 | |||
| 339 | return trim($type.' '.$name); |
||
| 340 | }, $PHPDocs['properties']['params']); |
||
| 341 | } |
||
| 342 | // If higher specificity argument types are provided in docblock, merge them with the actual types |
||
| 343 | if (isset($newParams)) { |
||
| 344 | foreach ($newParams as $index => $newParam) { |
||
| 345 | if (isset($parameters[$index])) { |
||
| 346 | $parameters[$index] = $newParam; |
||
| 347 | } |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | $argList = implode(', ', $parameters); |
||
| 352 | |||
| 353 | $before = null; |
||
| 354 | $beforeSignature = null; |
||
| 355 | if ($parameterDocs) { |
||
| 356 | if (count($parameterDocs) > 1) { |
||
| 357 | foreach ($parameterDocs as $type => $param) { |
||
| 358 | $name = explode(' ', $param, 3)[1]; |
||
| 359 | $desc = explode(' ', $param, 3)[2]; |
||
| 360 | $before .= "- **Parameter $name:** $desc \n"; |
||
| 361 | } |
||
| 362 | } else { |
||
| 363 | $param = array_values($parameterDocs)[0]; |
||
| 364 | $beforeSignature = "/** @param $param */"; |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | $signature = ($beforeSignature ? $beforeSignature."\n" : '').str_replace( |
||
| 369 | ['{{ $instanceVariableName }}', '{{ $methodName }}', '{{ $className }}'], |
||
| 370 | [$instanceVariableName, $methodName, $className], |
||
| 371 | $signatureTemplate |
||
| 372 | ); |
||
| 373 | |||
| 374 | $description = $description.($before ? "\n".$before : ''); |
||
| 375 | $replacements = [ |
||
| 376 | '{{ $signature }}' => $signature, |
||
| 377 | '{{ $methodName }}' => e($methodName), |
||
| 378 | '{{ $description }}' => e($description), |
||
| 379 | '{{ $className }}' => e($className), |
||
| 380 | '{{ $argList }}' => e($argList), |
||
| 381 | '{{ $returnType }}' => $returnType, |
||
| 382 | '{{ $facadeName }}' => $facadeName, |
||
| 383 | ]; |
||
| 384 | $markdown = str_replace(array_keys($replacements), array_values($replacements), $template); |
||
| 385 | |||
| 386 | // Throws |
||
| 387 | if (isset($PHPDocs['properties']['throws'])) { |
||
| 388 | $markdown .= "\n"; |
||
| 389 | foreach ($PHPDocs['properties']['throws'] as $throw) { |
||
| 390 | $markdown .= e("- **Throws:** $throw\n"); |
||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | // Debug breakpoint |
||
| 395 | if (str_contains($markdown, 'foo')) { |
||
| 396 | // dd($markdown); |
||
| 397 | } |
||
| 398 | |||
| 399 | $output[] = $markdown; |
||
| 400 | } |
||
| 449 |