Skip to content

Commit f838156

Browse files
barryvdhlaravel-ide-helper
andauthored
Add more metadata (#1646)
* Add more metadata * composer fix-style * Parse .env * composer fix-style * Check if .env exists --------- Co-authored-by: laravel-ide-helper <laravel-ide-helper@users.noreply.github.com>
1 parent cffcd07 commit f838156

File tree

5 files changed

+213
-34
lines changed

5 files changed

+213
-34
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"orchestra/testbench": "^9.2",
3939
"phpunit/phpunit": "^10.5",
4040
"spatie/phpunit-snapshot-assertions": "^4 || ^5",
41-
"vimeo/psalm": "^5.4"
41+
"vimeo/psalm": "^5.4",
42+
"vlucas/phpdotenv": "^5"
4243
},
4344
"suggest": {
4445
"illuminate/events": "Required for automatic helper generation (^6|^7|^8|^9|^10|^11)."

php-templates/auth.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
return collect(Illuminate\Support\Facades\Gate::abilities())
4+
->map(function ($policy, $key) {
5+
$reflection = new ReflectionFunction($policy);
6+
7+
$policyClass = null;
8+
9+
if (get_class($reflection->getClosureThis()) === Illuminate\Auth\Access\Gate::class) {
10+
$vars = $reflection->getClosureUsedVariables();
11+
12+
if (isset($vars['callback'])) {
13+
[$policyClass, $method] = explode('@', $vars['callback']);
14+
15+
$reflection = new ReflectionMethod($policyClass, $method);
16+
}
17+
}
18+
return [
19+
'key' => $key,
20+
'uri' => $reflection->getFileName(),
21+
'policy_class' => $policyClass,
22+
'lineNumber' => $reflection->getStartLine(),
23+
];
24+
})
25+
->merge(
26+
collect(Illuminate\Support\Facades\Gate::policies())->flatMap(function ($policy, $model) {
27+
$methods = (new ReflectionClass($policy))->getMethods();
28+
29+
return collect($methods)->map(function (ReflectionMethod $method) use ($policy) {
30+
return [
31+
'key' => $method->getName(),
32+
'uri' => $method->getFileName(),
33+
'policy_class' => $policy,
34+
'lineNumber' => $method->getStartLine(),
35+
];
36+
})->filter(function ($ability) {
37+
return !in_array($ability['key'], ['allow', 'deny']);
38+
});
39+
}),
40+
)
41+
->values()
42+
->groupBy('key');

php-templates/middleware.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
return collect(app("Illuminate\Contracts\Http\Kernel")->getMiddlewareGroups())
4+
->merge(app("Illuminate\Contracts\Http\Kernel")->getRouteMiddleware())
5+
->map(function ($middleware, $key) {
6+
$result = [
7+
'class' => null,
8+
'uri' => null,
9+
'startLine' => null,
10+
'parameters' => null,
11+
'groups' => [],
12+
];
13+
14+
if (is_array($middleware)) {
15+
$result['groups'] = collect($middleware)->map(function ($m) {
16+
if (!class_exists($m)) {
17+
return [
18+
'class' => $m,
19+
'uri' => null,
20+
'startLine' => null,
21+
];
22+
}
23+
24+
$reflected = new ReflectionClass($m);
25+
$reflectedMethod = $reflected->getMethod('handle');
26+
27+
return [
28+
'class' => $m,
29+
'uri' => $reflected->getFileName(),
30+
'startLine' =>
31+
$reflectedMethod->getFileName() === $reflected->getFileName()
32+
? $reflectedMethod->getStartLine()
33+
: null,
34+
];
35+
})->all();
36+
37+
return $result;
38+
}
39+
40+
$reflected = new ReflectionClass($middleware);
41+
$reflectedMethod = $reflected->getMethod('handle');
42+
43+
$result = array_merge($result, [
44+
'class' => $middleware,
45+
'uri' => $reflected->getFileName(),
46+
'startLine' => $reflectedMethod->getStartLine(),
47+
]);
48+
49+
$parameters = collect($reflectedMethod->getParameters())
50+
->filter(function ($rc) {
51+
return $rc->getName() !== 'request' && $rc->getName() !== 'next';
52+
})
53+
->map(function ($rc) {
54+
return $rc->getName() . ($rc->isVariadic() ? '...' : '');
55+
});
56+
57+
if ($parameters->isEmpty()) {
58+
return $result;
59+
}
60+
61+
return array_merge($result, [
62+
'parameters' => $parameters->implode(','),
63+
]);
64+
});

resources/views/meta.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,19 @@
8888
<?php endif ?>
8989

9090
<?php if (isset($expectedArguments)) : ?>
91-
<?php foreach ($expectedArguments as $function => $arguments) : ?>
92-
<?php foreach ($arguments as $index => $argumentSet) : ?>
91+
<?php foreach ($expectedArguments as $arguments) : ?>
92+
<?php
93+
$classes = isset($arguments['class']) ? (array) $arguments['class'] : [null];
94+
$index = $arguments['index'] ?? 0;
95+
$argumentSet = $arguments['argumentSet'];
96+
$functions = [];
97+
foreach ($classes as $class) {
98+
foreach ((array) $arguments['method'] as $method) {
99+
$functions[] = '\\' . ($class ? ltrim($class, '\\') . '::' : '') . $method . '()';
100+
}
101+
}
102+
?>
103+
<?php foreach ($functions as $function) : ?>
93104
expectedArguments(<?= $function ?>, <?= $index ?>, argumentsSet('<?= $argumentSet ?>'));
94105
<?php endforeach; ?>
95106
<?php endforeach; ?>

src/Console/MetaCommand.php

Lines changed: 92 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Barryvdh\LaravelIdeHelper\Console;
1313

1414
use Barryvdh\LaravelIdeHelper\Factories;
15+
use Dotenv\Parser\Entry;
16+
use Dotenv\Parser\Parser;
1517
use Illuminate\Console\Command;
1618
use Illuminate\Contracts\Config\Repository;
1719
use Illuminate\Contracts\View\Factory;
@@ -68,9 +70,7 @@ class MetaCommand extends Command
6870
protected $configMethods = [
6971
'\config()',
7072
'\Illuminate\Config\Repository::get()',
71-
'\Illuminate\Config\Repository::set()',
7273
'\Illuminate\Support\Facades\Config::get()',
73-
'\Illuminate\Support\Facades\Config::set()',
7474
];
7575

7676
protected $userMethods = [
@@ -202,59 +202,105 @@ protected function registerClassAutoloadExceptions(): callable
202202
protected function getExpectedArgumentSets()
203203
{
204204
return [
205+
'auth' => $this->loadTemplate('auth')->keys()->filter()->toArray(),
205206
'configs' => $this->loadTemplate('configs')->pluck('name')->filter()->toArray(),
207+
'middleware' => $this->loadTemplate('middleware')->keys()->filter()->toArray(),
206208
'routes' => $this->loadTemplate('routes')->pluck('name')->filter()->toArray(),
207209
'views' => $this->loadTemplate('views')->pluck('key')->filter()->map(function ($value) {
208210
return (string) $value;
209211
})->toArray(),
210212
'translations' => $this->loadTemplate('translations')->filter()->keys()->toArray(),
213+
'env' => $this->getEnv(),
211214
];
212215
}
213216

214217
protected function getExpectedArguments()
215218
{
216219
return [
217-
'\config()' => [
218-
0 => 'configs',
220+
[
221+
'class' => '\Illuminate\Support\Facades\Gate',
222+
'method' => [
223+
'has',
224+
'allows',
225+
'denies',
226+
'check',
227+
'any',
228+
'none',
229+
'authorize',
230+
'inspect',
231+
],
232+
'argumentSet' => 'auth',
219233
],
220-
'\Illuminate\Config\Repository::get()' => [
221-
0 => 'configs',
234+
[
235+
'class' => ['\Illuminate\Support\Facades\Route', '\Illuminate\Support\Facades\Auth'],
236+
'method' => ['can', 'cannot'],
237+
'argumentSet' => 'auth',
222238
],
223-
'\Illuminate\Config\Repository::set()' => [
224-
0 => 'configs',
239+
[
240+
'method' => 'config',
241+
'argumentSet' => 'configs',
225242
],
226-
'\Illuminate\Support\Facades\Config::get()' => [
227-
0 => 'configs',
243+
[
244+
'class' => ['\Illuminate\Config\Repository', '\Illuminate\Support\Facades\Config'],
245+
'method' => [
246+
'get',
247+
'getMany',
248+
'set',
249+
'string',
250+
'integer',
251+
'boolean',
252+
'float',
253+
'array',
254+
'prepend',
255+
'push',
256+
],
257+
'argumentSet' => 'configs',
228258
],
229-
'\Illuminate\Support\Facades\Config::set()' => [
230-
0 => 'configs',
259+
[
260+
'class' => ['\Illuminate\Support\Facades\Route', '\Illuminate\Routing\Router'],
261+
'method' => ['middleware', 'withoutMiddleware'],
262+
'argumentSet' => 'middleware',
231263
],
232-
'\route()' => [
233-
0 => 'routes',
264+
[
265+
'method' => ['route', 'to_route', 'signedRoute'],
266+
'argumentSet' => 'routes',
234267
],
235-
'\Illuminate\Support\Facades\Route::get()' => [
236-
0 => 'routes',
268+
[
269+
'class' => [
270+
'\Illuminate\Support\Facades\Redirect',
271+
'\Illuminate\Support\Facades\URL',
272+
'\Illuminate\Routing\Redirector',
273+
'\Illuminate\Routing\UrlGenerator',
274+
],
275+
'method' => ['route', 'signedRoute', 'temporarySignedRoute'],
276+
'argumentSet' => 'routes',
237277
],
238-
'\Illuminate\Routing\Router::get()' => [
239-
0 => 'routes',
278+
[
279+
'method' => 'view',
280+
'argumentSet' => 'views',
240281
],
241-
'\view()' => [
242-
0 => 'views',
282+
[
283+
'class' => ['\Illuminate\Support\Facades\View', '\Illuminate\View\Factory'],
284+
'method' => 'make',
285+
'argumentSet' => 'views',
243286
],
244-
'\Illuminate\Support\Facades\View::make()' => [
245-
0 => 'views',
287+
[
288+
'method' => ['__', 'trans'],
289+
'argumentSet' => 'translations',
246290
],
247-
'\Illuminate\View\Factory::make()' => [
248-
0 => 'views',
291+
[
292+
'class' => ['\Illuminate\Contracts\Translation\Translator'],
293+
'method' => ['get'],
294+
'argumentSet' => 'translations',
249295
],
250-
'\__()' => [
251-
0 => 'translations',
296+
[
297+
'method' => 'env',
298+
'argumentSet' => 'env',
252299
],
253-
'\trans()' => [
254-
0 => 'translations',
255-
],
256-
'\Illuminate\Contracts\Translation\Translator::get()' => [
257-
0 => 'translations',
300+
[
301+
'class' => '\Illuminate\Support\Env',
302+
'method' => 'get',
303+
'argumentSet' => 'env',
258304
],
259305
];
260306
}
@@ -290,6 +336,21 @@ protected function getOptions()
290336
];
291337
}
292338

339+
protected function getEnv()
340+
{
341+
$envPath = base_path('.env');
342+
if (!file_exists($envPath)) {
343+
return [];
344+
}
345+
346+
$parser = new Parser();
347+
$entries = $parser->parse(file_get_contents($envPath));
348+
349+
return collect($entries)->map(function (Entry $entry) {
350+
return $entry->getName();
351+
});
352+
}
353+
293354
/**
294355
* Remove our custom autoloader that we pushed onto the autoload stack
295356
*

0 commit comments

Comments
 (0)