Skip to content

Commit e9c7f19

Browse files
Merge pull request #16 from TheDragonCode/1.x
Fixed dock block typing for extending the Response object
2 parents 42bf445 + 803445e commit e9c7f19

File tree

2 files changed

+37
-32
lines changed

2 files changed

+37
-32
lines changed

src/Commands/GenerateHelperCommand.php

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ReflectionParameter;
1717
use ReflectionUnionType;
1818

19+
use function array_map;
1920
use function base_path;
2021
use function class_exists;
2122
use function collect;
@@ -33,12 +34,23 @@ class GenerateHelperCommand extends Command
3334

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

37+
protected array $docBlocks = [
38+
'request' => [
39+
' * @method $this %s(%s)',
40+
' * @method static $this %s(%s)',
41+
],
42+
'response' => [
43+
' * @method mixed %s(%s)',
44+
' * @method static mixed %s(%s)',
45+
],
46+
];
47+
3648
public function handle(): void
3749
{
3850
$this->sections()->map(function (array $macros, string $section) {
3951
intro($section);
4052

41-
return $this->macros($macros);
53+
return $this->macros($macros, $section);
4254
})
4355
->tap(fn () => outro('storing'))
4456
->tap(fn () => $this->cleanUp())
@@ -53,13 +65,13 @@ protected function cleanUp(): void
5365
$this->components->task('clean up', fn () => Directory::ensureDelete($this->directory()));
5466
}
5567

56-
protected function macros(array $macros): Collection
68+
protected function macros(array $macros, string $section): Collection
5769
{
58-
return collect($macros)->map(function (Macro|string $macro, int|string $name) {
70+
return collect($macros)->map(function (Macro|string $macro, int|string $name) use ($section) {
5971
$name = $this->resolveName($macro, $name);
6072

61-
$this->components->task($name, function () use ($macro, $name, &$result) {
62-
$result = $this->prepare($name, $this->reflectionCallback($macro::callback())->getParameters());
73+
$this->components->task($name, function () use ($macro, $name, $section, &$result) {
74+
$result = $this->prepare($section, $name, $macro::callback());
6375
});
6476

6577
return $result;
@@ -83,37 +95,30 @@ protected function compileBlocks(string $section, Collection $blocks): string
8395
);
8496
}
8597

86-
protected function prepare(string $name, array $functions): array
98+
protected function prepare(string $section, string $name, Closure $callback): array
8799
{
88-
return $this->docBlock($name, $this->docBlockParameters($functions));
100+
return $this->docBlock($section, $name, $this->docBlockParameters($callback));
89101
}
90102

91-
protected function docBlock(string $name, string $parameters): array
103+
protected function docBlock(string $section, string $name, string $parameters): array
92104
{
93-
return [
94-
sprintf(' * @method $this %s(%s)', $name, $parameters),
95-
sprintf(' * @method static $this %s(%s)', $name, $parameters),
96-
];
105+
return array_map(fn (string $template) => sprintf($template, $name, $parameters), $this->docBlocks[$section]);
97106
}
98107

99-
/**
100-
* @param array<ReflectionParameter> $functions
101-
*
102-
* @return Collection
103-
*/
104-
protected function docBlockParameters(array $functions): string
108+
protected function docBlockParameters(Closure $callback): string
105109
{
106-
return collect($functions)->map(function (ReflectionParameter $parameter) {
107-
$result = $parameter->hasType() ? $this->compactTypes($parameter->getType()) : 'mixed';
110+
return collect($this->reflectionCallback($callback)->getParameters())
111+
->map(function (ReflectionParameter $parameter) {
112+
$result = $parameter->hasType() ? $this->compactTypes($parameter->getType()) : 'mixed';
108113

109-
$result .= ' $' . $parameter->getName();
114+
$result .= ' $' . $parameter->getName();
110115

111-
if ($parameter->isDefaultValueAvailable()) {
112-
$result .= ' = ' . var_export($parameter->getDefaultValue(), true);
113-
}
116+
if ($parameter->isDefaultValueAvailable()) {
117+
$result .= ' = ' . var_export($parameter->getDefaultValue(), true);
118+
}
114119

115-
return $result;
116-
})->implode(', ');
120+
return $result;
121+
})->implode(', ');
117122
}
118123

119124
protected function compactTypes(ReflectionNamedType|ReflectionUnionType $type): string

tests/Snapshots/response

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace Illuminate\Http\Client {
66
/**
7-
* @method $this toData(\Closure|string $class, string|int|null $key = NULL)
8-
* @method static $this toData(\Closure|string $class, string|int|null $key = NULL)
9-
* @method $this toDataCollection(\Closure|string $class, string|int|null $key = NULL)
10-
* @method static $this toDataCollection(\Closure|string $class, string|int|null $key = NULL)
11-
* @method $this toFoo(\Closure|string $class, string|int|null $key = NULL)
12-
* @method static $this toFoo(\Closure|string $class, string|int|null $key = NULL)
7+
* @method mixed toData(\Closure|string $class, string|int|null $key = NULL)
8+
* @method static mixed toData(\Closure|string $class, string|int|null $key = NULL)
9+
* @method mixed toDataCollection(\Closure|string $class, string|int|null $key = NULL)
10+
* @method static mixed toDataCollection(\Closure|string $class, string|int|null $key = NULL)
11+
* @method mixed toFoo(\Closure|string $class, string|int|null $key = NULL)
12+
* @method static mixed toFoo(\Closure|string $class, string|int|null $key = NULL)
1313
*/
1414
class Response {}
1515
}

0 commit comments

Comments
 (0)