Skip to content

Commit

Permalink
Merge pull request #211 from dereuromark/behavior-remove
Browse files Browse the repository at this point in the history
Add behavior remove autocomplete.
  • Loading branch information
dereuromark authored Oct 2, 2020
2 parents 7d4936b + 55dab9c commit 5345205
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/Generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Note: Using Configure key `'IdeHelper.preemptive'` set to `true` you can be a bi
The following is now auto-completed, for example:
```php
$this->addBehavior('Tools.Slugged');
$this->removeBehavior('Slugged'); // Note the alias without plugin prefix
```

#### Components
Expand Down
29 changes: 22 additions & 7 deletions src/Generator/Task/BehaviorTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,41 @@ class BehaviorTask implements TaskInterface {
/**
* @var int[]
*/
protected $aliases = [
protected $addAliases = [
'\\' . self::CLASS_TABLE . '::addBehavior()' => 0,
];

/**
* @var int[]
*/
protected $removeAliases = [
'\\' . self::CLASS_TABLE . '::removeBehavior()' => 0,
];

/**
* @return \IdeHelper\Generator\Directive\BaseDirective[]
*/
public function collect(): array {
$list = [];

$addList = $removeList = [];
$behaviors = $this->collectBehaviors();
foreach ($behaviors as $name => $className) {
$list[$name] = StringName::create($name);
$addList[$name] = StringName::create($name);
if (strpos($name, '.') !== false) {
[, $name] = pluginSplit($name);
}
$removeList[$name] = StringName::create($name);
}

ksort($list);
ksort($addList);
ksort($removeList);

$result = [];
foreach ($this->aliases as $alias => $position) {
$directive = new ExpectedArguments($alias, $position, $list);
foreach ($this->addAliases as $alias => $position) {
$directive = new ExpectedArguments($alias, $position, $addList);
$result[$directive->key()] = $directive;
}
foreach ($this->removeAliases as $alias => $position) {
$directive = new ExpectedArguments($alias, $position, $removeList);
$result[$directive->key()] = $directive;
}

Expand Down
14 changes: 13 additions & 1 deletion tests/TestCase/Generator/Task/BehaviorTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function setUp(): void {
public function testCollect() {
$result = $this->task->collect();

$this->assertCount(1, $result);
$this->assertCount(2, $result);

/** @var \IdeHelper\Generator\Directive\Override $directive */
$directive = array_shift($result);
Expand All @@ -40,6 +40,18 @@ public function testCollect() {

$expected = '\'Shim.Nullable\'';
$this->assertSame($expected, (string)$list['Shim.Nullable']);

/** @var \IdeHelper\Generator\Directive\Override $directive */
$directive = array_shift($result);
$this->assertSame('\Cake\ORM\Table::removeBehavior()', $directive->toArray()['method']);

$list = $directive->toArray()['list'];

$expected = '\'Timestamp\'';
$this->assertSame($expected, (string)$list['Timestamp']);

$expected = '\'Nullable\'';
$this->assertSame($expected, (string)$list['Nullable']);
}

}
11 changes: 11 additions & 0 deletions tests/test_files/meta/phpstorm/.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,17 @@
])
);

expectedArguments(
\Cake\ORM\Table::removeBehavior(),
0,
'CounterCache',
'My',
'Nullable',
'Timestamp',
'Translate',
'Tree'
);

override(
\Cake\ORM\TableRegistry::get(0),
map([
Expand Down

0 comments on commit 5345205

Please sign in to comment.