Skip to content

Add unsetProperty in hooks #1308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -301,6 +301,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']);
9 changes: 8 additions & 1 deletion src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
@@ -816,6 +816,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 +837,7 @@ public function setMethod($name, $type = '', $arguments = [], $comment = '')

public function unsetMethod($name)
{
unset($this->methods[strtolower($name)]);
unset($this->methods[$name]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we should change this…

It's from the initial implementation https://github.com/barryvdh/laravel-ide-helper/pull/1198/files#diff-2df8ee218526540f71334c651990edfbfe5345cc53a521b0259c874ff24abd9aR766

ping @jenga201 if you remember why?

}

public function getMethodType(Model $model, string $classType)
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
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
@@ -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,
],
]);
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
*/
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ public function up(): void
{
Schema::create('simples', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('unset');
});
}
}