From 2607d599b4edac7f7da4ffcee7a1cec32b7f1eb1 Mon Sep 17 00:00:00 2001 From: pataar Date: Mon, 31 Mar 2025 16:13:44 +0200 Subject: [PATCH 1/5] feat(ModelsCommand): add configuration option to disable model query methods --- config/ide-helper.php | 11 ++ src/Console/ModelsCommand.php | 16 +- .../QueryMethods/Models/Post.php | 11 ++ .../ModelsCommand/QueryMethods/Test.php | 33 ++++ .../__snapshots__/Test__test__1.php | 158 ++++++++++++++++++ 5 files changed, 223 insertions(+), 6 deletions(-) create mode 100644 tests/Console/ModelsCommand/QueryMethods/Models/Post.php create mode 100644 tests/Console/ModelsCommand/QueryMethods/Test.php create mode 100644 tests/Console/ModelsCommand/QueryMethods/__snapshots__/Test__test__1.php diff --git a/config/ide-helper.php b/config/ide-helper.php index 1500783aa..9353d94ff 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -61,6 +61,17 @@ 'include_factory_builders' => false, + /* + |-------------------------------------------------------------------------- + | Write model query methods + |-------------------------------------------------------------------------- + | + | Set to false to disable the 'query()', 'newQuery()' and 'newModelQuery()' methods. + | + */ + + 'write_query_methods' => true, + /* |-------------------------------------------------------------------------- | Write model magic methods diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index aa389ec9a..879d351e4 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -689,13 +689,17 @@ public function getPropertiesFromMethods($model) ); $this->setMethod($name, $builder . '|' . $modelName, $args, $comment); } - } elseif (in_array($method, ['query', 'newQuery', 'newModelQuery'])) { - $builder = $this->getClassNameInDestinationFile($model, get_class($model->newModelQuery())); + } elseif (in_array($method, ['query', 'newQuery', 'newModelQuery']) + ) { + if($this->laravel['config']->get('ide-helper.write_query_methods', true)){ - $this->setMethod( - $method, - $builder . '|' . $this->getClassNameInDestinationFile($model, get_class($model)) - ); + $builder = $this->getClassNameInDestinationFile($model, get_class($model->newModelQuery())); + + $this->setMethod( + $method, + $builder . '|' . $this->getClassNameInDestinationFile($model, get_class($model)) + ); + } if ($this->write_model_external_builder_methods) { $this->writeModelExternalBuilderMethods($model); diff --git a/tests/Console/ModelsCommand/QueryMethods/Models/Post.php b/tests/Console/ModelsCommand/QueryMethods/Models/Post.php new file mode 100644 index 000000000..918f77e41 --- /dev/null +++ b/tests/Console/ModelsCommand/QueryMethods/Models/Post.php @@ -0,0 +1,11 @@ +set('ide-helper.write_query_methods', false); + } + + public function test(): void + { + $command = $this->app->make(ModelsCommand::class); + + $tester = $this->runCommand($command, [ + '--write' => true, + ]); + + $this->assertSame(0, $tester->getStatusCode()); + $this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay()); + $this->assertMatchesMockedSnapshot(); + $this->assertStringNotContainsString("@method static \Illuminate\Database\Eloquent\Builder|Post query()", $this->mockFilesystemOutput); + } +} diff --git a/tests/Console/ModelsCommand/QueryMethods/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/QueryMethods/__snapshots__/Test__test__1.php new file mode 100644 index 000000000..5a059be50 --- /dev/null +++ b/tests/Console/ModelsCommand/QueryMethods/__snapshots__/Test__test__1.php @@ -0,0 +1,158 @@ +|Post whereBigIntegerNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereBigIntegerNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereBinaryNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereBinaryNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereBooleanNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereBooleanNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereCharNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereCharNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereCreatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereDateNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereDateNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereDatetimeNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereDatetimeNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereDatetimetzNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereDatetimetzNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereDecimalNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereDecimalNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereDoubleNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereDoubleNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereEnumNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereEnumNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereFloatNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereFloatNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereIntegerNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereIntegerNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereIpaddressNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereIpaddressNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereJsonNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereJsonNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereJsonbNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereJsonbNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereLongTextNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereLongTextNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereMacaddressNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereMacaddressNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereMediumIntegerNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereMediumIntegerNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereMediumTextNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereMediumTextNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereSmallIntegerNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereSmallIntegerNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereStringNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereStringNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereTextNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereTextNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereTimeNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereTimeNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereTimestampNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereTimestampNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereTimestamptzNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereTimestamptzNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereTimetzNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereTimetzNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereTinyIntegerNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereTinyIntegerNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereUnsignedBigIntegerNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereUnsignedBigIntegerNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereUnsignedIntegerNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereUnsignedIntegerNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereUnsignedMediumIntegerNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereUnsignedMediumIntegerNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereUnsignedSmallIntegerNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereUnsignedSmallIntegerNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereUnsignedTinyIntegerNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereUnsignedTinyIntegerNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereUpdatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereUuidNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereUuidNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereYearNotNullable($value) + * @method static \Illuminate\Database\Eloquent\Builder|Post whereYearNullable($value) + * @mixin \Eloquent + */ +class Post extends Model +{ +} From c9d0d1f0787bf0d5d02fb2ae23a1a03ee447b8b8 Mon Sep 17 00:00:00 2001 From: laravel-ide-helper Date: Mon, 31 Mar 2025 14:14:30 +0000 Subject: [PATCH 2/5] composer fix-style --- src/Console/ModelsCommand.php | 3 +-- tests/Console/ModelsCommand/QueryMethods/Test.php | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 879d351e4..fcdf05615 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -691,8 +691,7 @@ public function getPropertiesFromMethods($model) } } elseif (in_array($method, ['query', 'newQuery', 'newModelQuery']) ) { - if($this->laravel['config']->get('ide-helper.write_query_methods', true)){ - + if ($this->laravel['config']->get('ide-helper.write_query_methods', true)) { $builder = $this->getClassNameInDestinationFile($model, get_class($model->newModelQuery())); $this->setMethod( diff --git a/tests/Console/ModelsCommand/QueryMethods/Test.php b/tests/Console/ModelsCommand/QueryMethods/Test.php index 624034eb9..002e366bf 100644 --- a/tests/Console/ModelsCommand/QueryMethods/Test.php +++ b/tests/Console/ModelsCommand/QueryMethods/Test.php @@ -6,7 +6,6 @@ use Barryvdh\LaravelIdeHelper\Console\ModelsCommand; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand; -use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\QueryMethods\Models\Post; class Test extends AbstractModelsCommand { From 2a017a5e621614df98fea161bd8b3b1a0d81ac19 Mon Sep 17 00:00:00 2001 From: pataar Date: Mon, 31 Mar 2025 16:25:43 +0200 Subject: [PATCH 3/5] update comment --- config/ide-helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/ide-helper.php b/config/ide-helper.php index 9353d94ff..2ad711032 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -66,7 +66,7 @@ | Write model query methods |-------------------------------------------------------------------------- | - | Set to false to disable the 'query()', 'newQuery()' and 'newModelQuery()' methods. + | Set to false to disable generated docs for the 'query()', 'newQuery()' and 'newModelQuery()' methods. | */ From a3fa3027467962ed221ec9f7dde65086235eb08f Mon Sep 17 00:00:00 2001 From: pataar Date: Thu, 3 Apr 2025 16:02:29 +0200 Subject: [PATCH 4/5] revert whitespace changes --- resources/views/helper.php | 4 ++-- resources/views/meta.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/views/helper.php b/resources/views/helper.php index 575af25b8..74af90c0e 100644 --- a/resources/views/helper.php +++ b/resources/views/helper.php @@ -29,7 +29,7 @@ $aliases) : ?> namespace { -getDocComment($s1)) . "\n{$s1}" . $alias->getClassType() ?> getExtendsClass() ?>shouldExtendParentClass()): ?> extends getParentClass() ?> { +getDocComment($s1)) . "\n{$s1}" . $alias->getClassType() ?> getExtendsClass() ?>shouldExtendParentClass()): ?> extends getParentClass() ?> { getMethods() as $method) : ?> getDocComment($s2)) . "\n{$s2}" ?>public static function getName() ?>(getParamsWithDefault() ?>) {getDeclaringClass() !== $method->getRoot()) : ?> @@ -76,7 +76,7 @@ - + namespace { /** diff --git a/resources/views/meta.php b/resources/views/meta.php index 54854fbdc..ea934d022 100644 --- a/resources/views/meta.php +++ b/resources/views/meta.php @@ -81,7 +81,7 @@ $argumentsList) : ?> - registerArgumentsSet('', $arg) : ?>', $arg) : ?>,); From ea7f807fe5825e082802aaa29c884f01ed13cc5f Mon Sep 17 00:00:00 2001 From: laravel-ide-helper Date: Fri, 11 Apr 2025 12:08:08 +0000 Subject: [PATCH 5/5] composer fix-style --- resources/views/helper.php | 4 ++-- resources/views/meta.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/views/helper.php b/resources/views/helper.php index 74af90c0e..575af25b8 100644 --- a/resources/views/helper.php +++ b/resources/views/helper.php @@ -29,7 +29,7 @@ $aliases) : ?> namespace { -getDocComment($s1)) . "\n{$s1}" . $alias->getClassType() ?> getExtendsClass() ?>shouldExtendParentClass()): ?> extends getParentClass() ?> { +getDocComment($s1)) . "\n{$s1}" . $alias->getClassType() ?> getExtendsClass() ?>shouldExtendParentClass()): ?> extends getParentClass() ?> { getMethods() as $method) : ?> getDocComment($s2)) . "\n{$s2}" ?>public static function getName() ?>(getParamsWithDefault() ?>) {getDeclaringClass() !== $method->getRoot()) : ?> @@ -76,7 +76,7 @@ - + namespace { /** diff --git a/resources/views/meta.php b/resources/views/meta.php index ea934d022..54854fbdc 100644 --- a/resources/views/meta.php +++ b/resources/views/meta.php @@ -81,7 +81,7 @@ $argumentsList) : ?> - registerArgumentsSet('', $arg) : ?>', $arg) : ?>,);