Skip to content

Commit 6d6d7b3

Browse files
committed
Add option to models command
1 parent 68d89af commit 6d6d7b3

File tree

8 files changed

+255
-13
lines changed

8 files changed

+255
-13
lines changed

src/Console/ModelsCommand.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
namespace Barryvdh\LaravelIdeHelper\Console;
1313

1414
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
15+
use Barryvdh\LaravelIdeHelper\Generator;
1516
use Barryvdh\LaravelIdeHelper\Parsers\PhpDocReturnTypeParser;
1617
use Barryvdh\Reflection\DocBlock;
1718
use Barryvdh\Reflection\DocBlock\Context;
1819
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
1920
use Barryvdh\Reflection\DocBlock\Tag;
2021
use Composer\ClassMapGenerator\ClassMapGenerator;
2122
use Illuminate\Console\Command;
23+
use Illuminate\Contracts\Config\Repository;
2224
use Illuminate\Contracts\Database\Eloquent\Castable;
2325
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
2426
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
@@ -45,7 +47,9 @@
4547
use Illuminate\Filesystem\Filesystem;
4648
use Illuminate\Support\Arr;
4749
use Illuminate\Support\Collection;
50+
use Illuminate\Support\Facades\Artisan;
4851
use Illuminate\Support\Str;
52+
use Illuminate\View\Factory as ViewFactory;
4953
use phpDocumentor\Reflection\Types\ContextFactory;
5054
use ReflectionClass;
5155
use ReflectionNamedType;
@@ -82,6 +86,14 @@ class ModelsCommand extends Command
8286
*/
8387
protected $files;
8488

89+
/**
90+
* @var Repository
91+
*/
92+
protected $config;
93+
94+
/** @var ViewFactory */
95+
protected $view;
96+
8597
/**
8698
* The console command name.
8799
*
@@ -131,10 +143,12 @@ class ModelsCommand extends Command
131143
/**
132144
* @param Filesystem $files
133145
*/
134-
public function __construct(Filesystem $files)
146+
public function __construct(Filesystem $files, Repository $config, ViewFactory $view)
135147
{
136-
parent::__construct();
148+
$this->config = $config;
137149
$this->files = $files;
150+
$this->view = $view;
151+
parent::__construct();
138152
}
139153

140154
/**
@@ -187,6 +201,27 @@ public function handle()
187201
$this->error("Failed to write model information to $filename");
188202
}
189203
}
204+
205+
$helperFilename = $this->config->get('ide-helper.filename');
206+
$writeHelper = $this->option('write-eloquent-helper');
207+
208+
if (!$writeHelper && !$this->files->exists($helperFilename) && ($this->write || $this->write_mixin)) {
209+
if ($this->confirm("{$helperFilename} does not exist.
210+
Do you want to generate a minimal helper to generate the Eloquent methods?")) {
211+
$writeHelper = true;
212+
}
213+
}
214+
215+
if ($writeHelper) {
216+
$generator = new Generator($this->config, $this->view, $this->getOutput());
217+
$content = $generator->generateEloquent();
218+
$written = $this->files->put($helperFilename, $content);
219+
if ($written !== false) {
220+
$this->info("Eloquent helper was written to $helperFilename");
221+
} else {
222+
$this->error("Failed to write eloquent helper to $helperFilename");
223+
}
224+
}
190225
}
191226

192227

@@ -217,6 +252,9 @@ protected function getOptions()
217252
['write-mixin', 'M', InputOption::VALUE_NONE,
218253
"Write models to {$this->filename} and adds @mixin to each model, avoiding IDE duplicate declaration warnings",
219254
],
255+
['write-eloquent-helper', 'E', InputOption::VALUE_NONE,
256+
"Write Eloquent helper file to _ide_helper.php",
257+
],
220258
['nowrite', 'N', InputOption::VALUE_NONE, 'Don\'t write to Model file'],
221259
['reset', 'R', InputOption::VALUE_NONE, 'Remove the original phpdocs instead of appending'],
222260
['smart-reset', 'r', InputOption::VALUE_NONE, 'Retained for compatibility, while it no longer has any effect'],

src/Generator.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Barryvdh\LaravelIdeHelper;
1313

14+
use Illuminate\Database\Eloquent\Model;
1415
use Illuminate\Foundation\AliasLoader;
1516
use Illuminate\Support\Collection;
1617
use Illuminate\Support\Facades\Facade;
@@ -91,16 +92,17 @@ public function generate()
9192

9293
public function generateEloquent()
9394
{
94-
$aliasesByNamespace = $this->getAliasesByAliasNamespace();
95-
96-
$eloquentAliases = [];
97-
foreach ($aliasesByNamespace as $namespace => $aliases) {
98-
foreach ($aliases as $alias) {
99-
if ($alias->getExtendsNamespace() === '\Illuminate\Database\Eloquent') {
100-
$eloquentAliases[$namespace] = $eloquentAliases[$namespace] ?? [];
101-
$eloquentAliases[$namespace][] = $alias;
102-
}
95+
$name = 'Eloquent';
96+
$facade = Model::class;
97+
$magicMethods = array_key_exists($name, $this->magic) ? $this->magic[$name] : [];
98+
$alias = new Alias($this->config, $name, $facade, $magicMethods, $this->interfaces);
99+
if ($alias->isValid()) {
100+
//Add extra methods, from other classes (magic static calls)
101+
if (array_key_exists($name, $this->extra)) {
102+
$alias->addClass($this->extra[$name]);
103103
}
104+
105+
$eloquentAliases['_root'] = [$alias];
104106
}
105107

106108
$app = app();

src/IdeHelperServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function register()
6565
$configPath = __DIR__ . '/../config/ide-helper.php';
6666
$this->mergeConfigFrom($configPath, 'ide-helper');
6767

68-
$this->app->when([GeneratorCommand::class, MetaCommand::class])
68+
$this->app->when([GeneratorCommand::class, MetaCommand::class, ModelsCommand::class ])
6969
->needs(\Illuminate\Contracts\View\Factory::class)
7070
->give(function () {
7171
return $this->createLocalViewFactory();

tests/Console/ModelsCommand/GenerateBasicPhpdoc/Test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public function test(): void
1919

2020
$this->assertSame(0, $tester->getStatusCode());
2121
$this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay());
22+
$this->assertStringContainsString('Do you want to generate a minimal helper to generate the Eloquent methods?', $tester->getDisplay());
23+
2224
$this->assertMatchesMockedSnapshot();
2325
}
2426
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateBasicPhpdocWithEloquentHelper\Models;
6+
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class Post extends Model
10+
{
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateBasicPhpdocWithEloquentHelper;
6+
7+
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
8+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
9+
10+
class Test extends AbstractModelsCommand
11+
{
12+
public function test(): void
13+
{
14+
$command = $this->app->make(ModelsCommand::class);
15+
16+
$tester = $this->runCommand($command, [
17+
'--write' => true,
18+
'--write-eloquent-helper' => true,
19+
]);
20+
21+
$this->assertSame(0, $tester->getStatusCode());
22+
$this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay());
23+
$this->assertStringNotContainsString('Do you want to generate a minimal helper to generate the Eloquent methods?', $tester->getDisplay());
24+
$this->assertStringContainsString('Eloquent helper was written to _ide_helper.php', $tester->getDisplay());
25+
26+
// $this->assertMatchesMockedSnapshot();
27+
}
28+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateBasicPhpdocWithEloquentHelper\Models;
6+
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
/**
10+
*
11+
*
12+
* @property int $id
13+
* @property string|null $char_nullable
14+
* @property string $char_not_nullable
15+
* @property string|null $string_nullable
16+
* @property string $string_not_nullable
17+
* @property string|null $text_nullable
18+
* @property string $text_not_nullable
19+
* @property string|null $medium_text_nullable
20+
* @property string $medium_text_not_nullable
21+
* @property string|null $long_text_nullable
22+
* @property string $long_text_not_nullable
23+
* @property int|null $integer_nullable
24+
* @property int $integer_not_nullable
25+
* @property int|null $tiny_integer_nullable
26+
* @property int $tiny_integer_not_nullable
27+
* @property int|null $small_integer_nullable
28+
* @property int $small_integer_not_nullable
29+
* @property int|null $medium_integer_nullable
30+
* @property int $medium_integer_not_nullable
31+
* @property int|null $big_integer_nullable
32+
* @property int $big_integer_not_nullable
33+
* @property int|null $unsigned_integer_nullable
34+
* @property int $unsigned_integer_not_nullable
35+
* @property int|null $unsigned_tiny_integer_nullable
36+
* @property int $unsigned_tiny_integer_not_nullable
37+
* @property int|null $unsigned_small_integer_nullable
38+
* @property int $unsigned_small_integer_not_nullable
39+
* @property int|null $unsigned_medium_integer_nullable
40+
* @property int $unsigned_medium_integer_not_nullable
41+
* @property int|null $unsigned_big_integer_nullable
42+
* @property int $unsigned_big_integer_not_nullable
43+
* @property float|null $float_nullable
44+
* @property float $float_not_nullable
45+
* @property float|null $double_nullable
46+
* @property float $double_not_nullable
47+
* @property string|null $decimal_nullable
48+
* @property string $decimal_not_nullable
49+
* @property int|null $boolean_nullable
50+
* @property int $boolean_not_nullable
51+
* @property string|null $enum_nullable
52+
* @property string $enum_not_nullable
53+
* @property string|null $json_nullable
54+
* @property string $json_not_nullable
55+
* @property string|null $jsonb_nullable
56+
* @property string $jsonb_not_nullable
57+
* @property string|null $date_nullable
58+
* @property string $date_not_nullable
59+
* @property string|null $datetime_nullable
60+
* @property string $datetime_not_nullable
61+
* @property string|null $datetimetz_nullable
62+
* @property string $datetimetz_not_nullable
63+
* @property string|null $time_nullable
64+
* @property string $time_not_nullable
65+
* @property string|null $timetz_nullable
66+
* @property string $timetz_not_nullable
67+
* @property string|null $timestamp_nullable
68+
* @property string $timestamp_not_nullable
69+
* @property string|null $timestamptz_nullable
70+
* @property string $timestamptz_not_nullable
71+
* @property int|null $year_nullable
72+
* @property int $year_not_nullable
73+
* @property string|null $binary_nullable
74+
* @property string $binary_not_nullable
75+
* @property string|null $uuid_nullable
76+
* @property string $uuid_not_nullable
77+
* @property string|null $ipaddress_nullable
78+
* @property string $ipaddress_not_nullable
79+
* @property string|null $macaddress_nullable
80+
* @property string $macaddress_not_nullable
81+
* @property \Illuminate\Support\Carbon|null $created_at
82+
* @property \Illuminate\Support\Carbon|null $updated_at
83+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post newModelQuery()
84+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post newQuery()
85+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post query()
86+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereBigIntegerNotNullable($value)
87+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereBigIntegerNullable($value)
88+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereBinaryNotNullable($value)
89+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereBinaryNullable($value)
90+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereBooleanNotNullable($value)
91+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereBooleanNullable($value)
92+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereCharNotNullable($value)
93+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereCharNullable($value)
94+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereCreatedAt($value)
95+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereDateNotNullable($value)
96+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereDateNullable($value)
97+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereDatetimeNotNullable($value)
98+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereDatetimeNullable($value)
99+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereDatetimetzNotNullable($value)
100+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereDatetimetzNullable($value)
101+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereDecimalNotNullable($value)
102+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereDecimalNullable($value)
103+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereDoubleNotNullable($value)
104+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereDoubleNullable($value)
105+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereEnumNotNullable($value)
106+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereEnumNullable($value)
107+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereFloatNotNullable($value)
108+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereFloatNullable($value)
109+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereId($value)
110+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereIntegerNotNullable($value)
111+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereIntegerNullable($value)
112+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereIpaddressNotNullable($value)
113+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereIpaddressNullable($value)
114+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereJsonNotNullable($value)
115+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereJsonNullable($value)
116+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereJsonbNotNullable($value)
117+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereJsonbNullable($value)
118+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereLongTextNotNullable($value)
119+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereLongTextNullable($value)
120+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereMacaddressNotNullable($value)
121+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereMacaddressNullable($value)
122+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereMediumIntegerNotNullable($value)
123+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereMediumIntegerNullable($value)
124+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereMediumTextNotNullable($value)
125+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereMediumTextNullable($value)
126+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereSmallIntegerNotNullable($value)
127+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereSmallIntegerNullable($value)
128+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereStringNotNullable($value)
129+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereStringNullable($value)
130+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereTextNotNullable($value)
131+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereTextNullable($value)
132+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereTimeNotNullable($value)
133+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereTimeNullable($value)
134+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereTimestampNotNullable($value)
135+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereTimestampNullable($value)
136+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereTimestamptzNotNullable($value)
137+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereTimestamptzNullable($value)
138+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereTimetzNotNullable($value)
139+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereTimetzNullable($value)
140+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereTinyIntegerNotNullable($value)
141+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereTinyIntegerNullable($value)
142+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereUnsignedBigIntegerNotNullable($value)
143+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereUnsignedBigIntegerNullable($value)
144+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereUnsignedIntegerNotNullable($value)
145+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereUnsignedIntegerNullable($value)
146+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereUnsignedMediumIntegerNotNullable($value)
147+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereUnsignedMediumIntegerNullable($value)
148+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereUnsignedSmallIntegerNotNullable($value)
149+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereUnsignedSmallIntegerNullable($value)
150+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereUnsignedTinyIntegerNotNullable($value)
151+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereUnsignedTinyIntegerNullable($value)
152+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereUpdatedAt($value)
153+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereUuidNotNullable($value)
154+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereUuidNullable($value)
155+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereYearNotNullable($value)
156+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post whereYearNullable($value)
157+
* @mixin \Eloquent
158+
*/
159+
class Post extends Model
160+
{
161+
}

tests/Console/ModelsCommand/ModelHooks/Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function test(): void
3636
{
3737
$actualContent = null;
3838

39-
$mockFilesystem = Mockery::mock(Filesystem::class);
39+
$mockFilesystem = Mockery::mock(Filesystem::class)->makePartial();
4040
$mockFilesystem
4141
->shouldReceive('get')
4242
->andReturn(file_get_contents(__DIR__ . '/Models/Simple.php'))

0 commit comments

Comments
 (0)