Skip to content

Commit 2007b7f

Browse files
authored
[9.x] Support dynamic params for Facade docblock generation (#45352)
* Support dynamic parameters * Fix facades * Code style
1 parent 91f01cf commit 2007b7f

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

bin/facades.php

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,18 @@ function resolveDocMixins($class)
318318
->flatMap(fn ($mixin) => [$mixin, ...resolveDocMixins($mixin)]);
319319
}
320320

321+
/**
322+
* Resolve the classes referenced methods in the @methods docblocks.
323+
*
324+
* @param \ReflectionMethodDecorator $method
325+
* @return \Illuminate\Support\Collection<int, string>
326+
*/
327+
function resolveDocParameters($method)
328+
{
329+
return resolveDocTags($method->getDocComment() ?: '', '@param')
330+
->map(fn ($tag) => Str::squish($tag));
331+
}
332+
321333
/**
322334
* Determine if the method is magic.
323335
*
@@ -428,7 +440,7 @@ function normaliseDetails($method)
428440
{
429441
return is_string($method) ? $method : [
430442
'name' => $method->getName(),
431-
'parameters' => collect($method->getParameters())
443+
'parameters' => resolveParameters($method)
432444
->map(fn ($parameter) => [
433445
'name' => '$'.$parameter->getName(),
434446
'optional' => $parameter->isOptional() && ! $parameter->isVariadic(),
@@ -442,6 +454,21 @@ function normaliseDetails($method)
442454
];
443455
}
444456

457+
/**
458+
* Resolve the parameters for the method.
459+
*
460+
* @param \ReflectionMethodDecorator $method
461+
* @return \Illuminate\Support\Collection<int, \ReflectionParameter|\DynamicParameter>
462+
*/
463+
function resolveParameters($method)
464+
{
465+
$dynamicParameters = resolveDocParameters($method)
466+
->skip($method->getNumberOfParameters())
467+
->mapInto(DynamicParameter::class);
468+
469+
return collect($method->getParameters())->merge($dynamicParameters);
470+
}
471+
445472
/**
446473
* Resolve the default value for the parameter.
447474
*
@@ -505,3 +532,57 @@ public function sourceClass()
505532
return new ReflectionClass($this->sourceClass);
506533
}
507534
}
535+
536+
class DynamicParameter
537+
{
538+
/**
539+
* @param string $definition
540+
*/
541+
public function __construct(private $definition)
542+
{
543+
//
544+
}
545+
546+
/**
547+
* @return string
548+
*/
549+
public function getName()
550+
{
551+
return Str::of($this->definition)
552+
->after('$')
553+
->before(' ')
554+
->toString();
555+
}
556+
557+
/**
558+
* @return bool
559+
*/
560+
public function isOptional()
561+
{
562+
return true;
563+
}
564+
565+
/**
566+
* @return bool
567+
*/
568+
public function isVariadic()
569+
{
570+
return Str::contains($this->definition, " ...\${$this->getName()}");
571+
}
572+
573+
/**
574+
* @return bool
575+
*/
576+
public function isDefaultValueAvailable()
577+
{
578+
return true;
579+
}
580+
581+
/**
582+
* @return null
583+
*/
584+
public function getDefaultValue()
585+
{
586+
return null;
587+
}
588+
}

src/Illuminate/Support/Facades/Http.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
* @method static \Illuminate\Http\Client\PendingRequest withMiddleware(callable $middleware)
5050
* @method static \Illuminate\Http\Client\PendingRequest beforeSending(callable $callback)
5151
* @method static \Illuminate\Http\Client\PendingRequest throw(callable|null $callback = null)
52-
* @method static \Illuminate\Http\Client\PendingRequest throwIf(callable|bool $condition)
52+
* @method static \Illuminate\Http\Client\PendingRequest throwIf(callable|bool $condition, callable|null $throwCallback = null)
5353
* @method static \Illuminate\Http\Client\PendingRequest throwUnless(bool $condition)
5454
* @method static \Illuminate\Http\Client\PendingRequest dump()
5555
* @method static \Illuminate\Http\Client\PendingRequest dd()

0 commit comments

Comments
 (0)