Skip to content

Commit

Permalink
Ignore column properties via config and unset properties in hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
gabsource committed Jan 25, 2022
1 parent 999167d commit e63d191
Show file tree
Hide file tree
Showing 21 changed files with 109 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Attribute>` on your models, e.g. `Post::whereTitle(…)` and automatically translates this to e.g. `Post::where('title', '=', '…')`.
Expand Down Expand Up @@ -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']);
Expand Down
15 changes: 15 additions & 0 deletions config/ide-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ['*'])
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions tests/Console/ModelsCommand/Ignored/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions tests/Console/ModelsCommand/ModelHooks/Hooks/UnsetProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Hooks;

use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
use Illuminate\Database\Eloquent\Model;

class UnsetProperty implements ModelHookInterface
{
public function run(ModelsCommand $command, Model $model): void
{
$command->unsetProperty('unset');
}
}
2 changes: 2 additions & 0 deletions tests/Console/ModelsCommand/ModelHooks/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,6 +29,7 @@ protected function getEnvironmentSetUp($app)
CustomProperty::class,
CustomMethod::class,
UnsetMethod::class,
UnsetProperty::class,
],
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/Console/ModelsCommand/migrations/____not_ignored.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class NotIgnored extends Migration
{
public function up(): void
{
Schema::create('not_ignoreds', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('ignored');
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function up(): void
{
Schema::create('simples', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('unset');
});
}
}

0 comments on commit e63d191

Please sign in to comment.