diff --git a/CHANGELOG.md b/CHANGELOG.md index 9457220..9684aa1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. [Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v2.9.3...master) -------------- +### Added +- Allowing Methods to be set or unset in ModelHooks [\#1198 / jenga201](https://github.com/barryvdh/laravel-ide-helper/pull/1198)\ + Note: the visibility of `\Barryvdh\LaravelIdeHelper\Console\ModelsCommand::setMethod` has been changed to **public**! + 2021-04-02, 2.9.3 ----------------- diff --git a/README.md b/README.md index 410aed3..c227fe7 100644 --- a/README.md +++ b/README.md @@ -301,6 +301,8 @@ class MyCustomHook implements ModelHookInterface } $command->setProperty('custom', 'string', true, false, 'My custom property'); + $command->unsetMethod('method'); + $command->setMethod('method', $command->getMethodType($model, '\Some\Class'), ['$param']); } } ``` diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 3447f60..35c1419 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -749,7 +749,7 @@ class ModelsCommand extends Command } } - protected function setMethod($name, $type = '', $arguments = [], $comment = '') + public function setMethod($name, $type = '', $arguments = [], $comment = '') { $methods = array_change_key_case($this->methods, CASE_LOWER); @@ -761,6 +761,18 @@ class ModelsCommand extends Command } } + public function unsetMethod($name) + { + unset($this->methods[strtolower($name)]); + } + + public function getMethodType(Model $model, string $classType) + { + $modelName = $this->getClassNameInDestinationFile($model, get_class($model)); + $builder = $this->getClassNameInDestinationFile($model, $classType); + return $builder . '|' . $modelName; + } + /** * @param string $class * @return string diff --git a/tests/Console/ModelsCommand/ModelHooks/Hooks/CustomMethod.php b/tests/Console/ModelsCommand/ModelHooks/Hooks/CustomMethod.php new file mode 100644 index 0000000..c9b36ea --- /dev/null +++ b/tests/Console/ModelsCommand/ModelHooks/Hooks/CustomMethod.php @@ -0,0 +1,18 @@ +setMethod('custom', $command->getMethodType($model, Builder::class), ['$custom']); + } +} diff --git a/tests/Console/ModelsCommand/ModelHooks/Hooks/UnsetMethod.php b/tests/Console/ModelsCommand/ModelHooks/Hooks/UnsetMethod.php new file mode 100644 index 0000000..6dfad54 --- /dev/null +++ b/tests/Console/ModelsCommand/ModelHooks/Hooks/UnsetMethod.php @@ -0,0 +1,17 @@ +unsetMethod('query'); + } +} diff --git a/tests/Console/ModelsCommand/ModelHooks/Test.php b/tests/Console/ModelsCommand/ModelHooks/Test.php index 057cfd2..05ffea2 100644 --- a/tests/Console/ModelsCommand/ModelHooks/Test.php +++ b/tests/Console/ModelsCommand/ModelHooks/Test.php @@ -6,7 +6,9 @@ namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks; use Barryvdh\LaravelIdeHelper\Console\ModelsCommand; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand; +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 Illuminate\Filesystem\Filesystem; use Mockery; @@ -24,6 +26,8 @@ class Test extends AbstractModelsCommand ], 'model_hooks' => [ CustomProperty::class, + CustomMethod::class, + UnsetMethod::class, ], ]); } @@ -71,9 +75,9 @@ use Illuminate\Database\Eloquent\Model; * * @property int $id * @property-read string $custom + * @method static \Illuminate\Database\Eloquent\Builder|Simple custom($custom) * @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) * @mixin \Eloquent */