Skip to content
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

Add unsetProperty in hooks #1308

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Expand Up @@ -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']);
Expand Down
9 changes: 8 additions & 1 deletion src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.

}

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
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
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');
});
}
}