Skip to content

Commit 9ab78e4

Browse files
Merge pull request #6 from TheDragonCode/1.x
Added command to generate helper files for IDE
2 parents 38e334f + ce5c10e commit 9ab78e4

20 files changed

+225
-1
lines changed

.github/images/ide-helper.png

50 KB
Loading

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Http::get()->toData(...); // will be method not found exception
6161
```
6262

6363
> Note
64-
>
64+
>
6565
> Please note that IDE hints will not work in this case.
6666
6767
## Usage
@@ -176,6 +176,18 @@ class SomeData
176176
return Http::get()->toDataCollection(SomeData::class);
177177
```
178178

179+
### Generate IDE Helper files
180+
181+
You can generate helper files for the IDE using the console command:
182+
183+
```Bash
184+
php artisan http:macros-helper
185+
```
186+
187+
This will help your IDE suggest methods.
188+
189+
![IDE Helper](.github\images\ide-helper.png)
190+
179191
## License
180192

181193
This package is licensed under the [MIT License](LICENSE).

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
},
2525
"require": {
2626
"php": "^8.2",
27+
"dragon-code/support": "^6.13",
2728
"illuminate/http": "^10.0 || ^11.0",
2829
"illuminate/support": "^10.0 || ^11.0"
2930
},
3031
"require-dev": {
3132
"guzzlehttp/guzzle": "^7.8",
3233
"orchestra/testbench": "^8.0 || ^9.0",
3334
"pestphp/pest": "^2.34",
35+
"pestphp/pest-plugin-laravel": "^2.4",
3436
"spatie/laravel-data": "^4.8"
3537
},
3638
"minimum-stability": "stable",
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelHttpMacros\Commands;
6+
7+
use DragonCode\LaravelHttpMacros\Macros\Macro;
8+
use DragonCode\Support\Facades\Filesystem\Directory;
9+
use DragonCode\Support\Facades\Filesystem\File;
10+
use Illuminate\Console\Command;
11+
use Illuminate\Support\Str;
12+
13+
class GenerateHelperCommand extends Command
14+
{
15+
protected $signature = 'http:macros-helper';
16+
17+
protected $description = 'Generates correct PHPDocs for Http facade macros';
18+
19+
public function handle(): void
20+
{
21+
$names = $this->names();
22+
23+
$static = $this->make($names, true);
24+
$dynamic = $this->make($names);
25+
26+
$this->cleanUp();
27+
$this->store($static, true);
28+
$this->store($dynamic);
29+
}
30+
31+
protected function make(array $names, bool $isStatic = false): array
32+
{
33+
return array_map(
34+
fn (string $name) => sprintf(
35+
' * @method %s $this %s(\Closure|string $class, int|string|null $key = null)',
36+
$isStatic ? 'static' : '',
37+
$name
38+
),
39+
$names
40+
);
41+
}
42+
43+
protected function store(array $methods, bool $isStatic = false): void
44+
{
45+
File::store(
46+
$this->path($this->filename($isStatic)),
47+
$this->makeDocBlock($methods)
48+
);
49+
}
50+
51+
protected function makeDocBlock(array $methods): string
52+
{
53+
return Str::replace('{methods}', implode(PHP_EOL, $methods), $this->template());
54+
}
55+
56+
protected function names(): array
57+
{
58+
return collect($this->macros())->map(
59+
fn (Macro|string $macro, int|string $name) => is_string($name) ? $name : $macro::name()
60+
)->all();
61+
}
62+
63+
protected function path(?string $filename = null): string
64+
{
65+
return base_path('vendor/_http_macros/' . $filename);
66+
}
67+
68+
protected function filename(bool $isStatic): string
69+
{
70+
return $isStatic
71+
? '_ide_helper_macro_static.php'
72+
: '_ide_helper_macro.php';
73+
}
74+
75+
protected function cleanUp(): void
76+
{
77+
Directory::ensureDelete($this->path());
78+
}
79+
80+
protected function macros(): array
81+
{
82+
return config('http.macros.response', []);
83+
}
84+
85+
protected function template(): string
86+
{
87+
return file_get_contents(__DIR__ . '/../../stubs/helper.stub');
88+
}
89+
}

src/ServiceProvider.php

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

55
namespace DragonCode\LaravelHttpMacros;
66

7+
use DragonCode\LaravelHttpMacros\Commands\GenerateHelperCommand;
78
use Illuminate\Http\Client\Response;
89
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
910

@@ -18,6 +19,16 @@ public function boot(): void
1819
{
1920
$this->publishConfig();
2021
$this->bootMacros();
22+
$this->bootCommands();
23+
}
24+
25+
protected function bootCommands(): void
26+
{
27+
if ($this->app->runningInConsole()) {
28+
$this->commands([
29+
GenerateHelperCommand::class,
30+
]);
31+
}
2132
}
2233

2334
protected function bootMacros(): void

stubs/helper.stub

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
/** @noinspection all */
4+
5+
namespace Illuminate\Http\Client {
6+
/**
7+
{methods}
8+
*/
9+
class Response {}
10+
}

tests/Datasets/types.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
dataset('types', [
6+
['static', '_static'],
7+
['dynamic', ''],
8+
]);

tests/Helpers/content.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
function content(string $filename): string
6+
{
7+
return trim(file_get_contents($filename));
8+
}

tests/Snapshots/dynamic

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
/** @noinspection all */
4+
5+
namespace Illuminate\Http\Client {
6+
/**
7+
* @method $this toData(\Closure|string $class, int|string|null $key = null)
8+
* @method $this toDataCollection(\Closure|string $class, int|string|null $key = null)
9+
* @method $this toFoo(\Closure|string $class, int|string|null $key = null)
10+
*/
11+
class Response {}
12+
}

tests/Snapshots/static

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
/** @noinspection all */
4+
5+
namespace Illuminate\Http\Client {
6+
/**
7+
* @method static $this toData(\Closure|string $class, int|string|null $key = null)
8+
* @method static $this toDataCollection(\Closure|string $class, int|string|null $key = null)
9+
* @method static $this toFoo(\Closure|string $class, int|string|null $key = null)
10+
*/
11+
class Response {}
12+
}

0 commit comments

Comments
 (0)