From 16a4297992a3a75ee46ac08f7a3752888fe1e5ec Mon Sep 17 00:00:00 2001 From: Rob Porter Date: Thu, 1 Apr 2021 15:55:14 -0400 Subject: [PATCH 1/5] Allowing Methods to be set in ModelHooks --- src/Console/ModelsCommand.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 8dcbb23..f9cc80c 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,16 @@ 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 From 9fe65e7ed3f5d694512fc0d0e959aa0d71658b0c Mon Sep 17 00:00:00 2001 From: Rob Porter Date: Thu, 1 Apr 2021 16:04:59 -0400 Subject: [PATCH 2/5] Updating readme with a ModelHooks set/unset Method example --- README.md | 2 ++ 1 file changed, 2 insertions(+) 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']); } } ``` From 057efb0ee76931bc89f888a405b19d0c9bc4db3f Mon Sep 17 00:00:00 2001 From: Rob Porter Date: Fri, 2 Apr 2021 15:25:22 -0400 Subject: [PATCH 3/5] Adding tests to Set / unset methods --- .../ModelHooks/Hooks/CustomMethod.php | 18 ++++++++++++++++++ .../ModelHooks/Hooks/UnsetMethod.php | 18 ++++++++++++++++++ .../Console/ModelsCommand/ModelHooks/Test.php | 6 +++++- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/Console/ModelsCommand/ModelHooks/Hooks/CustomMethod.php create mode 100644 tests/Console/ModelsCommand/ModelHooks/Hooks/UnsetMethod.php 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..17629ea --- /dev/null +++ b/tests/Console/ModelsCommand/ModelHooks/Hooks/UnsetMethod.php @@ -0,0 +1,18 @@ +unsetMethod('query'); + } +} diff --git a/tests/Console/ModelsCommand/ModelHooks/Test.php b/tests/Console/ModelsCommand/ModelHooks/Test.php index 057cfd2..1bb7231 100644 --- a/tests/Console/ModelsCommand/ModelHooks/Test.php +++ b/tests/Console/ModelsCommand/ModelHooks/Test.php @@ -7,6 +7,8 @@ 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\CustomProperty; +use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Hooks\CustomMethod; +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 */ From 39a96c3b950b6ab0d43761a68c0404c681f7a9b4 Mon Sep 17 00:00:00 2001 From: Rob Porter Date: Fri, 2 Apr 2021 15:30:15 -0400 Subject: [PATCH 4/5] run composer fix-style --- src/Console/ModelsCommand.php | 6 ++++-- .../Console/ModelsCommand/ModelHooks/Hooks/UnsetMethod.php | 1 - tests/Console/ModelsCommand/ModelHooks/Test.php | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index f9cc80c..932a05a 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -761,11 +761,13 @@ class ModelsCommand extends Command } } - public function unsetMethod($name) { + public function unsetMethod($name) + { unset($this->methods[strtolower($name)]); } - public function getMethodType(Model $model, string $classType) { + public function getMethodType(Model $model, string $classType) + { $modelName = $this->getClassNameInDestinationFile($model, get_class($model)); $builder = $this->getClassNameInDestinationFile($model, $classType); return $builder . '|' . $modelName; diff --git a/tests/Console/ModelsCommand/ModelHooks/Hooks/UnsetMethod.php b/tests/Console/ModelsCommand/ModelHooks/Hooks/UnsetMethod.php index 17629ea..6dfad54 100644 --- a/tests/Console/ModelsCommand/ModelHooks/Hooks/UnsetMethod.php +++ b/tests/Console/ModelsCommand/ModelHooks/Hooks/UnsetMethod.php @@ -6,7 +6,6 @@ namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Hooks use Barryvdh\LaravelIdeHelper\Console\ModelsCommand; use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; class UnsetMethod implements ModelHookInterface diff --git a/tests/Console/ModelsCommand/ModelHooks/Test.php b/tests/Console/ModelsCommand/ModelHooks/Test.php index 1bb7231..05ffea2 100644 --- a/tests/Console/ModelsCommand/ModelHooks/Test.php +++ b/tests/Console/ModelsCommand/ModelHooks/Test.php @@ -6,8 +6,8 @@ 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\CustomProperty; 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; @@ -27,7 +27,7 @@ class Test extends AbstractModelsCommand 'model_hooks' => [ CustomProperty::class, CustomMethod::class, - UnsetMethod::class + UnsetMethod::class, ], ]); } From dcbe6c953f7407b12b0c770703e60e75e2d206c1 Mon Sep 17 00:00:00 2001 From: Markus Podar Date: Sat, 3 Apr 2021 00:17:10 +0200 Subject: [PATCH 5/5] chore: update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) 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 -----------------