diff --git a/README.md b/README.md index 866e05055..a7a502107 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,16 @@ Or can be ignored by setting the `ignored_models` config ], ``` +#### Ignore Models Properties + +Models properties can be ignored by setting the `ignored_models_properties` config + +```php +'ignored_models_properties' => [ + App\Post::class => [ 'hash' ], +], +``` + #### Magic `where*` methods Eloquent allows calling `where` on your models, e.g. `Post::whereTitle(…)` and automatically translates this to e.g. `Post::where('title', '=', '…')`. @@ -301,6 +311,7 @@ class MyCustomHook implements ModelHookInterface return; } + $command->unsetProperty('hash'); $command->setProperty('custom', 'string', true, false, 'My custom property'); $command->unsetMethod('method'); $command->setMethod('method', $command->getMethodType($model, '\Some\Class'), ['$param']); diff --git a/config/ide-helper.php b/config/ide-helper.php index 51fcdf24a..1479db7fa 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -155,6 +155,21 @@ ], + /* + |-------------------------------------------------------------------------- + | Models properties to ignore + |-------------------------------------------------------------------------- + | + | Define which properties should be ignored per model. The key of the array + | is the canonical class name of the model and the values the properties + | name, e.g. `Model::class => [ 'ignored_column' ]` + | + */ + + 'ignored_models_properties' => [ + + ], + /* |-------------------------------------------------------------------------- | Models hooks diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 92f7ebd36..e1bf646db 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -489,9 +489,14 @@ public function getPropertiesFromTable($model) return; } + $ignoredProperties = array_values($this->laravel['config']->get('ide-helper.ignored_models_properties.' . get_class($model), [])); + $this->setForeignKeys($schema, $table); foreach ($columns as $column) { $name = $column->getName(); + if (in_array($name, $ignoredProperties)) { + continue; + } if (in_array($name, $model->getDates())) { $type = $this->dateClass; } else { @@ -816,6 +821,13 @@ public function setProperty($name, $type = null, $read = null, $write = null, $c } } + public function unsetProperty($name) + { + unset($this->properties[$name]); + + $this->unsetMethod(Str::camel('where_' . $name)); + } + public function setMethod($name, $type = '', $arguments = [], $comment = '') { $methods = array_change_key_case($this->methods, CASE_LOWER); @@ -830,7 +842,7 @@ public function setMethod($name, $type = '', $arguments = [], $comment = '') public function unsetMethod($name) { - unset($this->methods[strtolower($name)]); + unset($this->methods[$name]); } public function getMethodType(Model $model, string $classType) diff --git a/tests/Console/ModelsCommand/Attributes/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Attributes/__snapshots__/Test__test__1.php index eabeffcbb..a80a2936d 100644 --- a/tests/Console/ModelsCommand/Attributes/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Attributes/__snapshots__/Test__test__1.php @@ -11,11 +11,13 @@ * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Attributes\Models\Simple * * @property integer $id + * @property string $unset * @property string|null $name * @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple query() * @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Simple whereUnset($value) * @mixin \Eloquent */ class Simple extends Model diff --git a/tests/Console/ModelsCommand/Comment/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Comment/__snapshots__/Test__test__1.php index 4f530e299..161b06a22 100644 --- a/tests/Console/ModelsCommand/Comment/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Comment/__snapshots__/Test__test__1.php @@ -13,6 +13,7 @@ * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Comment\Models\Simple * * @property integer $id + * @property string $unset * @property string $both_same_name I'm a getter * @property string $both_without_getter_comment * @property-read string $faker_comment @@ -31,6 +32,7 @@ * @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple query() * @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Simple whereUnset($value) * @mixin \Eloquent */ class Simple extends Model diff --git a/tests/Console/ModelsCommand/CustomCollection/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/CustomCollection/__snapshots__/Test__test__1.php index 10818ac0a..20c1fefe1 100644 --- a/tests/Console/ModelsCommand/CustomCollection/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/CustomCollection/__snapshots__/Test__test__1.php @@ -12,6 +12,7 @@ * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\CustomCollection\Models\Simple * * @property integer $id + * @property string $unset * @property-read SimpleCollection|Simple[] $relationHasMany * @property-read int|null $relation_has_many_count * @method static SimpleCollection|static[] all($columns = ['*']) @@ -20,6 +21,7 @@ * @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple query() * @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Simple whereUnset($value) * @mixin \Eloquent */ class Simple extends Model diff --git a/tests/Console/ModelsCommand/Getter/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Getter/__snapshots__/Test__test__1.php index ab67d5444..e64f359ae 100644 --- a/tests/Console/ModelsCommand/Getter/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Getter/__snapshots__/Test__test__1.php @@ -11,6 +11,7 @@ * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Getter\Models\Simple * * @property integer $id + * @property string $unset * @property-read int|null $attribute_return_type_int_or_null * @property-read array $attribute_returns_array * @property-read bool $attribute_returns_bool @@ -35,6 +36,7 @@ * @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple query() * @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Simple whereUnset($value) * @mixin \Eloquent */ class Simple extends Model diff --git a/tests/Console/ModelsCommand/Ignored/Test.php b/tests/Console/ModelsCommand/Ignored/Test.php index 58aa6b046..ea6e022be 100644 --- a/tests/Console/ModelsCommand/Ignored/Test.php +++ b/tests/Console/ModelsCommand/Ignored/Test.php @@ -7,6 +7,7 @@ use Barryvdh\LaravelIdeHelper\Console\ModelsCommand; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Ignored\Models\Ignored; +use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Ignored\Models\NotIgnored; class Test extends AbstractModelsCommand { @@ -17,8 +18,13 @@ protected function getEnvironmentSetUp($app) $app['config']->set('ide-helper.ignored_models', [ Ignored::class, ]); + + $app['config']->set('ide-helper.ignored_models_properties', [ + NotIgnored::class => ['ignored'], + ]); } + public function test(): void { $command = $this->app->make(ModelsCommand::class); diff --git a/tests/Console/ModelsCommand/Ignored/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Ignored/__snapshots__/Test__test__1.php index 43f6607ce..81f005918 100644 --- a/tests/Console/ModelsCommand/Ignored/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Ignored/__snapshots__/Test__test__1.php @@ -9,9 +9,11 @@ /** * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Ignored\Models\NotIgnored * + * @property integer $id * @method static \Illuminate\Database\Eloquent\Builder|NotIgnored newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|NotIgnored newQuery() * @method static \Illuminate\Database\Eloquent\Builder|NotIgnored query() + * @method static \Illuminate\Database\Eloquent\Builder|NotIgnored whereId($value) * @mixin \Eloquent */ class NotIgnored extends Model diff --git a/tests/Console/ModelsCommand/ModelHooks/Hooks/UnsetProperty.php b/tests/Console/ModelsCommand/ModelHooks/Hooks/UnsetProperty.php new file mode 100644 index 000000000..83a9a45f8 --- /dev/null +++ b/tests/Console/ModelsCommand/ModelHooks/Hooks/UnsetProperty.php @@ -0,0 +1,17 @@ +unsetProperty('unset'); + } +} diff --git a/tests/Console/ModelsCommand/ModelHooks/Test.php b/tests/Console/ModelsCommand/ModelHooks/Test.php index 05ffea2ce..847563d29 100644 --- a/tests/Console/ModelsCommand/ModelHooks/Test.php +++ b/tests/Console/ModelsCommand/ModelHooks/Test.php @@ -9,6 +9,7 @@ use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Hooks\CustomMethod; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Hooks\CustomProperty; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Hooks\UnsetMethod; +use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Hooks\UnsetProperty; use Illuminate\Filesystem\Filesystem; use Mockery; @@ -28,6 +29,7 @@ protected function getEnvironmentSetUp($app) CustomProperty::class, CustomMethod::class, UnsetMethod::class, + UnsetProperty::class, ], ]); } diff --git a/tests/Console/ModelsCommand/PHPStormNoInspection/__snapshots__/Test__testNoinspectionNotPresent__1.php b/tests/Console/ModelsCommand/PHPStormNoInspection/__snapshots__/Test__testNoinspectionNotPresent__1.php index 25c0e3b6d..cbedddca4 100644 --- a/tests/Console/ModelsCommand/PHPStormNoInspection/__snapshots__/Test__testNoinspectionNotPresent__1.php +++ b/tests/Console/ModelsCommand/PHPStormNoInspection/__snapshots__/Test__testNoinspectionNotPresent__1.php @@ -10,10 +10,12 @@ * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\PHPStormNoInspection\Models\Simple * * @property integer $id + * @property string $unset * @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple query() * @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Simple whereUnset($value) * @mixin \Eloquent */ class Simple extends Model diff --git a/tests/Console/ModelsCommand/PHPStormNoInspection/__snapshots__/Test__testNoinspectionPresent__1.php b/tests/Console/ModelsCommand/PHPStormNoInspection/__snapshots__/Test__testNoinspectionPresent__1.php index 9cb1f276a..0543990ad 100644 --- a/tests/Console/ModelsCommand/PHPStormNoInspection/__snapshots__/Test__testNoinspectionPresent__1.php +++ b/tests/Console/ModelsCommand/PHPStormNoInspection/__snapshots__/Test__testNoinspectionPresent__1.php @@ -10,10 +10,12 @@ * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\PHPStormNoInspection\Models\Simple * * @property integer $id + * @property string $unset * @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple query() * @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Simple whereUnset($value) * @mixin \Eloquent * @noinspection PhpFullyQualifiedNameUsageInspection * @noinspection PhpUnnecessaryFullyQualifiedNameInspection diff --git a/tests/Console/ModelsCommand/Relations/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Relations/__snapshots__/Test__test__1.php index 445901d9d..8b20af4cc 100644 --- a/tests/Console/ModelsCommand/Relations/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Relations/__snapshots__/Test__test__1.php @@ -73,6 +73,7 @@ public function nullableColumnWithNoForeignKeyConstraint(): BelongsTo * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple * * @property integer $id + * @property string $unset * @property-read Simple|null $relationBelongsTo * @property-read AnotherModel|null $relationBelongsToInAnotherNamespace * @property-read \Illuminate\Database\Eloquent\Collection|Simple[] $relationBelongsToMany @@ -99,6 +100,7 @@ public function nullableColumnWithNoForeignKeyConstraint(): BelongsTo * @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple query() * @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Simple whereUnset($value) * @mixin \Eloquent */ class Simple extends Model diff --git a/tests/Console/ModelsCommand/ResetAndSmartReset/__snapshots__/Test__testNoReset__1.php b/tests/Console/ModelsCommand/ResetAndSmartReset/__snapshots__/Test__testNoReset__1.php index 6e5b18017..b9b6e730d 100644 --- a/tests/Console/ModelsCommand/ResetAndSmartReset/__snapshots__/Test__testNoReset__1.php +++ b/tests/Console/ModelsCommand/ResetAndSmartReset/__snapshots__/Test__testNoReset__1.php @@ -11,10 +11,12 @@ * * @property string $foo * @property integer $id + * @property string $unset * @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple query() * @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Simple whereUnset($value) * @mixin \Eloquent */ class Simple extends Model diff --git a/tests/Console/ModelsCommand/ResetAndSmartReset/__snapshots__/Test__testReset__1.php b/tests/Console/ModelsCommand/ResetAndSmartReset/__snapshots__/Test__testReset__1.php index 08f1d063c..9e534b9a6 100644 --- a/tests/Console/ModelsCommand/ResetAndSmartReset/__snapshots__/Test__testReset__1.php +++ b/tests/Console/ModelsCommand/ResetAndSmartReset/__snapshots__/Test__testReset__1.php @@ -10,10 +10,12 @@ * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ResetAndSmartReset\Models\Simple * * @property integer $id + * @property string $unset * @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple query() * @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Simple whereUnset($value) * @mixin \Eloquent */ class Simple extends Model diff --git a/tests/Console/ModelsCommand/ResetAndSmartReset/__snapshots__/Test__testSmartReset__1.php b/tests/Console/ModelsCommand/ResetAndSmartReset/__snapshots__/Test__testSmartReset__1.php index 3ba9c2b3d..fbaac0e91 100644 --- a/tests/Console/ModelsCommand/ResetAndSmartReset/__snapshots__/Test__testSmartReset__1.php +++ b/tests/Console/ModelsCommand/ResetAndSmartReset/__snapshots__/Test__testSmartReset__1.php @@ -10,10 +10,12 @@ * Text of existing phpdoc * * @property integer $id + * @property string $unset * @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple query() * @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Simple whereUnset($value) * @mixin \Eloquent */ class Simple extends Model diff --git a/tests/Console/ModelsCommand/SoftDeletes/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/SoftDeletes/__snapshots__/Test__test__1.php index 167fa0ccf..1abd6cfc5 100644 --- a/tests/Console/ModelsCommand/SoftDeletes/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/SoftDeletes/__snapshots__/Test__test__1.php @@ -11,11 +11,13 @@ * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\SoftDeletes\Models\Simple * * @property integer $id + * @property string $unset * @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery() * @method static \Illuminate\Database\Query\Builder|Simple onlyTrashed() * @method static \Illuminate\Database\Eloquent\Builder|Simple query() * @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Simple whereUnset($value) * @method static \Illuminate\Database\Query\Builder|Simple withTrashed() * @method static \Illuminate\Database\Query\Builder|Simple withoutTrashed() * @mixin \Eloquent diff --git a/tests/Console/ModelsCommand/Variadic/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Variadic/__snapshots__/Test__test__1.php index dd1a3d3d7..f2bcc1a86 100644 --- a/tests/Console/ModelsCommand/Variadic/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Variadic/__snapshots__/Test__test__1.php @@ -11,11 +11,13 @@ * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Variadic\Models\Simple * * @property integer $id + * @property string $unset * @method static Builder|Simple newModelQuery() * @method static Builder|Simple newQuery() * @method static Builder|Simple query() * @method static Builder|Simple whereId($value) * @method static Builder|Simple whereTypedVariadic(int ...$values) + * @method static Builder|Simple whereUnset($value) * @method static Builder|Simple whereVariadic(...$values) * @mixin \Eloquent */ diff --git a/tests/Console/ModelsCommand/migrations/____not_ignored.php b/tests/Console/ModelsCommand/migrations/____not_ignored.php new file mode 100644 index 000000000..98afb987d --- /dev/null +++ b/tests/Console/ModelsCommand/migrations/____not_ignored.php @@ -0,0 +1,18 @@ +bigIncrements('id'); + $table->string('ignored'); + }); + } +} diff --git a/tests/Console/ModelsCommand/migrations/____simple_table.php b/tests/Console/ModelsCommand/migrations/____simple_table.php index 4547d3abb..008917918 100644 --- a/tests/Console/ModelsCommand/migrations/____simple_table.php +++ b/tests/Console/ModelsCommand/migrations/____simple_table.php @@ -12,6 +12,7 @@ public function up(): void { Schema::create('simples', function (Blueprint $table) { $table->bigIncrements('id'); + $table->string('unset'); }); } }