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

Allowing Methods to be set or unset in ModelHooks #1198

Merged
merged 6 commits into from
Apr 3, 2021
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------------

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
```
Expand Down
14 changes: 13 additions & 1 deletion src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ public function setProperty($name, $type = null, $read = null, $write = null, $c
}
}

protected function setMethod($name, $type = '', $arguments = [], $comment = '')
public function setMethod($name, $type = '', $arguments = [], $comment = '')
Copy link
Collaborator

Choose a reason for hiding this comment

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

As with #945 (comment) , this will bring the same kind of breaking change.

So maybe in this case we should bump the major?

Copy link
Owner

Choose a reason for hiding this comment

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

I don't know, I think the impact is pretty small. The intended use case is not to provide an extensible package for all methods imho.

{
$methods = array_change_key_case($this->methods, CASE_LOWER);

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

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
Expand Down
18 changes: 18 additions & 0 deletions tests/Console/ModelsCommand/ModelHooks/Hooks/CustomMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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\Builder;
use Illuminate\Database\Eloquent\Model;

class CustomMethod implements ModelHookInterface
{
public function run(ModelsCommand $command, Model $model): void
{
$command->setMethod('custom', $command->getMethodType($model, Builder::class), ['$custom']);
}
}
17 changes: 17 additions & 0 deletions tests/Console/ModelsCommand/ModelHooks/Hooks/UnsetMethod.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 UnsetMethod implements ModelHookInterface
{
public function run(ModelsCommand $command, Model $model): void
{
$command->unsetMethod('query');
}
}
6 changes: 5 additions & 1 deletion tests/Console/ModelsCommand/ModelHooks/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

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;

Expand All @@ -24,6 +26,8 @@ protected function getEnvironmentSetUp($app)
],
'model_hooks' => [
CustomProperty::class,
CustomMethod::class,
UnsetMethod::class,
],
]);
}
Expand Down Expand Up @@ -71,9 +75,9 @@ public function test(): void
*
* @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
*/
Expand Down