Skip to content

Commit c1ddd30

Browse files
authored
Merge pull request barryvdh#1198 from jenga201/master
Allowing Methods to be set or unset in ModelHooks
2 parents 1010cdb + dcbe6c9 commit c1ddd30

File tree

6 files changed

+59
-2
lines changed

6 files changed

+59
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
[Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v2.9.3...master)
66
--------------
77

8+
### Added
9+
- Allowing Methods to be set or unset in ModelHooks [\#1198 / jenga201](https://github.com/barryvdh/laravel-ide-helper/pull/1198)\
10+
Note: the visibility of `\Barryvdh\LaravelIdeHelper\Console\ModelsCommand::setMethod` has been changed to **public**!
11+
812
2021-04-02, 2.9.3
913
-----------------
1014

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ class MyCustomHook implements ModelHookInterface
301301
}
302302

303303
$command->setProperty('custom', 'string', true, false, 'My custom property');
304+
$command->unsetMethod('method');
305+
$command->setMethod('method', $command->getMethodType($model, '\Some\Class'), ['$param']);
304306
}
305307
}
306308
```

src/Console/ModelsCommand.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ public function setProperty($name, $type = null, $read = null, $write = null, $c
749749
}
750750
}
751751

752-
protected function setMethod($name, $type = '', $arguments = [], $comment = '')
752+
public function setMethod($name, $type = '', $arguments = [], $comment = '')
753753
{
754754
$methods = array_change_key_case($this->methods, CASE_LOWER);
755755

@@ -761,6 +761,18 @@ protected function setMethod($name, $type = '', $arguments = [], $comment = '')
761761
}
762762
}
763763

764+
public function unsetMethod($name)
765+
{
766+
unset($this->methods[strtolower($name)]);
767+
}
768+
769+
public function getMethodType(Model $model, string $classType)
770+
{
771+
$modelName = $this->getClassNameInDestinationFile($model, get_class($model));
772+
$builder = $this->getClassNameInDestinationFile($model, $classType);
773+
return $builder . '|' . $modelName;
774+
}
775+
764776
/**
765777
* @param string $class
766778
* @return string
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Hooks;
6+
7+
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
8+
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
9+
use Illuminate\Database\Eloquent\Builder;
10+
use Illuminate\Database\Eloquent\Model;
11+
12+
class CustomMethod implements ModelHookInterface
13+
{
14+
public function run(ModelsCommand $command, Model $model): void
15+
{
16+
$command->setMethod('custom', $command->getMethodType($model, Builder::class), ['$custom']);
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Hooks;
6+
7+
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
8+
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
9+
use Illuminate\Database\Eloquent\Model;
10+
11+
class UnsetMethod implements ModelHookInterface
12+
{
13+
public function run(ModelsCommand $command, Model $model): void
14+
{
15+
$command->unsetMethod('query');
16+
}
17+
}

tests/Console/ModelsCommand/ModelHooks/Test.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
88
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
9+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Hooks\CustomMethod;
910
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Hooks\CustomProperty;
11+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Hooks\UnsetMethod;
1012
use Illuminate\Filesystem\Filesystem;
1113
use Mockery;
1214

@@ -24,6 +26,8 @@ protected function getEnvironmentSetUp($app)
2426
],
2527
'model_hooks' => [
2628
CustomProperty::class,
29+
CustomMethod::class,
30+
UnsetMethod::class,
2731
],
2832
]);
2933
}
@@ -71,9 +75,9 @@ public function test(): void
7175
*
7276
* @property int $id
7377
* @property-read string $custom
78+
* @method static \Illuminate\Database\Eloquent\Builder|Simple custom($custom)
7479
* @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery()
7580
* @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery()
76-
* @method static \Illuminate\Database\Eloquent\Builder|Simple query()
7781
* @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value)
7882
* @mixin \Eloquent
7983
*/

0 commit comments

Comments
 (0)