Skip to content

Commit

Permalink
Merge pull request #44 from dereuromark/feature/trait
Browse files Browse the repository at this point in the history
Add Trait metas, too.
  • Loading branch information
dereuromark authored Sep 7, 2017
2 parents b4734d5 + 52b6605 commit 4ba3126
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 16 deletions.
2 changes: 2 additions & 0 deletions docs/Generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ With the generated meta file this becomes not necessary anymore.
It will automatically detect this static factory call in the map and hint `$users` as `\App\Model\Table\UsersTable`, making
`doSomething()` available in the IDE for method argument checking and following.

This task also annotates the dynamic model factory calls (e.g. `$this->getTableLocator()->get('Users')`).

### Adding your own tasks
Just create your own Task class:
```php
Expand Down
5 changes: 3 additions & 2 deletions src/Generator/PhpstormGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public function generate() {
* @return string
*/
protected function build(array $map) {
$overrides = '';
$overrides = [];
foreach ($map as $method => $array) {
$mapDefinitions = $this->buildMapDefinitions($array);

$overrides .= <<<TXT
$overrides[] = <<<TXT
override(
$method,
map([
Expand All @@ -47,6 +47,7 @@ protected function build(array $map) {
TXT;

}
$overrides = implode(PHP_EOL . PHP_EOL, $overrides);

$template = <<<TXT
<?php
Expand Down
16 changes: 15 additions & 1 deletion src/Generator/Task/ModelTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@

class ModelTask implements TaskInterface {

/**
* @var array
*/
protected $aliases = [
'\Cake\ORM\TableRegistry::get(0)',
'\Cake\ORM\Locator\LocatorInterface::get(0)',
'\Cake\Datasource\ModelAwareTrait::loadModel(0)'
];

/**
* @return array
*/
Expand All @@ -18,7 +27,12 @@ public function collect() {
$map[$model] = '\\' . $className . '::class';
}

return ['\Cake\ORM\TableRegistry::get(0)' => $map];
$result = [];
foreach ($this->aliases as $alias) {
$result[$alias] = $map;
}

return $result;
}

/**
Expand Down
26 changes: 13 additions & 13 deletions tests/TestCase/Generator/Task/ModelTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ public function setUp() {
public function testCollect() {
$result = $this->task->collect();

$expected = [
'\Cake\ORM\TableRegistry::get(0)' => [
'BarBars' => '\App\Model\Table\BarBarsTable::class',
'Cars' => '\App\Model\Table\CarsTable::class',
'Exceptions' => '\App\Model\Table\ExceptionsTable::class',
'Foo' => '\App\Model\Table\FooTable::class',
'WheelsExtra' => '\App\Model\Table\WheelsExtraTable::class',
'Wheels' => '\App\Model\Table\WheelsTable::class',
'Awesome.Houses' => '\Awesome\Model\Table\HousesTable::class',
'Awesome.Windows' => '\Awesome\Model\Table\WindowsTable::class'
]
$this->assertCount(3, $result);

$expectedMap = [
'BarBars' => '\App\Model\Table\BarBarsTable::class',
'Cars' => '\App\Model\Table\CarsTable::class',
'Exceptions' => '\App\Model\Table\ExceptionsTable::class',
'Foo' => '\App\Model\Table\FooTable::class',
'WheelsExtra' => '\App\Model\Table\WheelsExtraTable::class',
'Wheels' => '\App\Model\Table\WheelsTable::class',
'Awesome.Houses' => '\Awesome\Model\Table\HousesTable::class',
'Awesome.Windows' => '\Awesome\Model\Table\WindowsTable::class'
];

$this->assertSame($result, $expected);
$map = array_shift($result);
$this->assertSame($expectedMap, $map);
}

}
28 changes: 28 additions & 0 deletions tests/test_files/meta/phpstorm/.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,32 @@
])
);

override(
\Cake\ORM\Locator\LocatorInterface::get(0),
map([
'BarBars' => \App\Model\Table\BarBarsTable::class,
'Cars' => \App\Model\Table\CarsTable::class,
'Exceptions' => \App\Model\Table\ExceptionsTable::class,
'Foo' => \App\Model\Table\FooTable::class,
'WheelsExtra' => \App\Model\Table\WheelsExtraTable::class,
'Wheels' => \App\Model\Table\WheelsTable::class,
'Awesome.Houses' => \Awesome\Model\Table\HousesTable::class,
'Awesome.Windows' => \Awesome\Model\Table\WindowsTable::class,
])
);

override(
\Cake\Datasource\ModelAwareTrait::loadModel(0),
map([
'BarBars' => \App\Model\Table\BarBarsTable::class,
'Cars' => \App\Model\Table\CarsTable::class,
'Exceptions' => \App\Model\Table\ExceptionsTable::class,
'Foo' => \App\Model\Table\FooTable::class,
'WheelsExtra' => \App\Model\Table\WheelsExtraTable::class,
'Wheels' => \App\Model\Table\WheelsTable::class,
'Awesome.Houses' => \Awesome\Model\Table\HousesTable::class,
'Awesome.Windows' => \Awesome\Model\Table\WindowsTable::class,
])
);

}

0 comments on commit 4ba3126

Please sign in to comment.