Skip to content

Commit

Permalink
[9.x] Support dynamic params for Facade docblock generation (#45352)
Browse files Browse the repository at this point in the history
* Support dynamic parameters

* Fix facades

* Code style
  • Loading branch information
timacdonald committed Dec 19, 2022
1 parent 91f01cf commit 2007b7f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
83 changes: 82 additions & 1 deletion bin/facades.php
Expand Up @@ -318,6 +318,18 @@ function resolveDocMixins($class)
->flatMap(fn ($mixin) => [$mixin, ...resolveDocMixins($mixin)]);
}

/**
* Resolve the classes referenced methods in the @methods docblocks.
*
* @param \ReflectionMethodDecorator $method
* @return \Illuminate\Support\Collection<int, string>
*/
function resolveDocParameters($method)
{
return resolveDocTags($method->getDocComment() ?: '', '@param')
->map(fn ($tag) => Str::squish($tag));
}

/**
* Determine if the method is magic.
*
Expand Down Expand Up @@ -428,7 +440,7 @@ function normaliseDetails($method)
{
return is_string($method) ? $method : [
'name' => $method->getName(),
'parameters' => collect($method->getParameters())
'parameters' => resolveParameters($method)
->map(fn ($parameter) => [
'name' => '$'.$parameter->getName(),
'optional' => $parameter->isOptional() && ! $parameter->isVariadic(),
Expand All @@ -442,6 +454,21 @@ function normaliseDetails($method)
];
}

/**
* Resolve the parameters for the method.
*
* @param \ReflectionMethodDecorator $method
* @return \Illuminate\Support\Collection<int, \ReflectionParameter|\DynamicParameter>
*/
function resolveParameters($method)
{
$dynamicParameters = resolveDocParameters($method)
->skip($method->getNumberOfParameters())
->mapInto(DynamicParameter::class);

return collect($method->getParameters())->merge($dynamicParameters);
}

/**
* Resolve the default value for the parameter.
*
Expand Down Expand Up @@ -505,3 +532,57 @@ public function sourceClass()
return new ReflectionClass($this->sourceClass);
}
}

class DynamicParameter
{
/**
* @param string $definition
*/
public function __construct(private $definition)
{
//
}

/**
* @return string
*/
public function getName()
{
return Str::of($this->definition)
->after('$')
->before(' ')
->toString();
}

/**
* @return bool
*/
public function isOptional()
{
return true;
}

/**
* @return bool
*/
public function isVariadic()
{
return Str::contains($this->definition, " ...\${$this->getName()}");
}

/**
* @return bool
*/
public function isDefaultValueAvailable()
{
return true;
}

/**
* @return null
*/
public function getDefaultValue()
{
return null;
}
}
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Facades/Http.php
Expand Up @@ -49,7 +49,7 @@
* @method static \Illuminate\Http\Client\PendingRequest withMiddleware(callable $middleware)
* @method static \Illuminate\Http\Client\PendingRequest beforeSending(callable $callback)
* @method static \Illuminate\Http\Client\PendingRequest throw(callable|null $callback = null)
* @method static \Illuminate\Http\Client\PendingRequest throwIf(callable|bool $condition)
* @method static \Illuminate\Http\Client\PendingRequest throwIf(callable|bool $condition, callable|null $throwCallback = null)
* @method static \Illuminate\Http\Client\PendingRequest throwUnless(bool $condition)
* @method static \Illuminate\Http\Client\PendingRequest dump()
* @method static \Illuminate\Http\Client\PendingRequest dd()
Expand Down

0 comments on commit 2007b7f

Please sign in to comment.