Skip to content

Commit

Permalink
Merge pull request #16 from TheDragonCode/1.x
Browse files Browse the repository at this point in the history
Fixed dock block typing for extending the Response object
  • Loading branch information
andrey-helldar authored Aug 25, 2024
2 parents 42bf445 + 803445e commit e9c7f19
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
57 changes: 31 additions & 26 deletions src/Commands/GenerateHelperCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ReflectionParameter;
use ReflectionUnionType;

use function array_map;
use function base_path;
use function class_exists;
use function collect;
Expand All @@ -33,12 +34,23 @@ class GenerateHelperCommand extends Command

protected $description = 'Generates correct PHPDocs for Http facade macros';

protected array $docBlocks = [
'request' => [
' * @method $this %s(%s)',
' * @method static $this %s(%s)',
],
'response' => [
' * @method mixed %s(%s)',
' * @method static mixed %s(%s)',
],
];

public function handle(): void
{
$this->sections()->map(function (array $macros, string $section) {
intro($section);

return $this->macros($macros);
return $this->macros($macros, $section);
})
->tap(fn () => outro('storing'))
->tap(fn () => $this->cleanUp())
Expand All @@ -53,13 +65,13 @@ protected function cleanUp(): void
$this->components->task('clean up', fn () => Directory::ensureDelete($this->directory()));
}

protected function macros(array $macros): Collection
protected function macros(array $macros, string $section): Collection
{
return collect($macros)->map(function (Macro|string $macro, int|string $name) {
return collect($macros)->map(function (Macro|string $macro, int|string $name) use ($section) {
$name = $this->resolveName($macro, $name);

$this->components->task($name, function () use ($macro, $name, &$result) {
$result = $this->prepare($name, $this->reflectionCallback($macro::callback())->getParameters());
$this->components->task($name, function () use ($macro, $name, $section, &$result) {
$result = $this->prepare($section, $name, $macro::callback());
});

return $result;
Expand All @@ -83,37 +95,30 @@ protected function compileBlocks(string $section, Collection $blocks): string
);
}

protected function prepare(string $name, array $functions): array
protected function prepare(string $section, string $name, Closure $callback): array
{
return $this->docBlock($name, $this->docBlockParameters($functions));
return $this->docBlock($section, $name, $this->docBlockParameters($callback));
}

protected function docBlock(string $name, string $parameters): array
protected function docBlock(string $section, string $name, string $parameters): array
{
return [
sprintf(' * @method $this %s(%s)', $name, $parameters),
sprintf(' * @method static $this %s(%s)', $name, $parameters),
];
return array_map(fn (string $template) => sprintf($template, $name, $parameters), $this->docBlocks[$section]);
}

/**
* @param array<ReflectionParameter> $functions
*
* @return Collection
*/
protected function docBlockParameters(array $functions): string
protected function docBlockParameters(Closure $callback): string
{
return collect($functions)->map(function (ReflectionParameter $parameter) {
$result = $parameter->hasType() ? $this->compactTypes($parameter->getType()) : 'mixed';
return collect($this->reflectionCallback($callback)->getParameters())
->map(function (ReflectionParameter $parameter) {
$result = $parameter->hasType() ? $this->compactTypes($parameter->getType()) : 'mixed';

$result .= ' $' . $parameter->getName();
$result .= ' $' . $parameter->getName();

if ($parameter->isDefaultValueAvailable()) {
$result .= ' = ' . var_export($parameter->getDefaultValue(), true);
}
if ($parameter->isDefaultValueAvailable()) {
$result .= ' = ' . var_export($parameter->getDefaultValue(), true);
}

return $result;
})->implode(', ');
return $result;
})->implode(', ');
}

protected function compactTypes(ReflectionNamedType|ReflectionUnionType $type): string
Expand Down
12 changes: 6 additions & 6 deletions tests/Snapshots/response
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Illuminate\Http\Client {
/**
* @method $this toData(\Closure|string $class, string|int|null $key = NULL)
* @method static $this toData(\Closure|string $class, string|int|null $key = NULL)
* @method $this toDataCollection(\Closure|string $class, string|int|null $key = NULL)
* @method static $this toDataCollection(\Closure|string $class, string|int|null $key = NULL)
* @method $this toFoo(\Closure|string $class, string|int|null $key = NULL)
* @method static $this toFoo(\Closure|string $class, string|int|null $key = NULL)
* @method mixed toData(\Closure|string $class, string|int|null $key = NULL)
* @method static mixed toData(\Closure|string $class, string|int|null $key = NULL)
* @method mixed toDataCollection(\Closure|string $class, string|int|null $key = NULL)
* @method static mixed toDataCollection(\Closure|string $class, string|int|null $key = NULL)
* @method mixed toFoo(\Closure|string $class, string|int|null $key = NULL)
* @method static mixed toFoo(\Closure|string $class, string|int|null $key = NULL)
*/
class Response {}
}

0 comments on commit e9c7f19

Please sign in to comment.