Skip to content

Commit 663da76

Browse files
authored
Merge pull request #64 from dereuromark/feature/types
Allow Type autocomplete.
2 parents 2400fed + f3128fe commit 663da76

File tree

6 files changed

+153
-6
lines changed

6 files changed

+153
-6
lines changed

docs/Generator.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ Make sure it is indexed (maybe a restart of PhpStorm could be required).
1515
Note: We are using a directory here to allow custom and manually created meta files along with this generated file.
1616
Any file inside this directory will be parsed and used. Prefixing with a `.` dot is recommended for PHPCS to skip this file automatically.
1717

18-
### Models
18+
### Available tasks
19+
20+
#### Models
1921
```php
2022
/** @var \App\Model\Table\UsersTable $users */
2123
$users = TableRegistry::get('Users');
@@ -28,7 +30,7 @@ It will automatically detect this static factory call in the map and hint `$user
2830

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

31-
### TableAssociations
33+
#### TableAssociations
3234
The following is now auto-completed, for example:
3335
```php
3436
$this->belongsTo('Authors');
@@ -37,7 +39,7 @@ $this->hasMany('Articles');
3739
$this->belongsToMany('Tags.Tags');
3840
```
3941

40-
### TableFinders
42+
#### TableFinders
4143
The `'threaded'` string is now auto-completed, for example:
4244
```php
4345
$this->Posts->find('threaded')
@@ -46,24 +48,32 @@ $this->Posts->find('threaded')
4648
Note: Using Configure key `'IdeHelper.preemptive'` set to `true` you can be a bit more verbose and include all possible custom finders, including those from behaviors.
4749

4850

49-
### Behaviors
51+
#### Behaviors
5052
The following is now auto-completed, for example:
5153
```php
5254
$this->addBehavior('Tools.Slugged')
5355
```
5456

55-
### Components
57+
#### Components
5658
The following is now auto-completed, for example:
5759
```php
5860
$this->loadComponent('Security')
5961
```
6062

61-
### Helpers
63+
#### Helpers
6264
The following is now auto-completed, for example:
6365
```php
6466
$this->loadHelper('Tools.Tree')
6567
```
6668

69+
#### Types
70+
In your bootstrap (app, or plugin), you might add additional database Type classes, or you reconfigure existing ones:
71+
```php
72+
Type::build('date')->useLocaleParser()->setLocaleFormat('d.m.Y');;
73+
Type::build('datetime')->useLocaleParser()->setLocaleFormat('d.m.Y H:i');
74+
```
75+
The IDE will now recognize the returned type of class and allow auto-complete here, too.
76+
6777
### Adding your own tasks
6878
Just create your own Task class:
6979
```php
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace IdeHelper\Generator\Task;
3+
4+
use Cake\Database\Type;
5+
use Exception;
6+
7+
class DatabaseTypeTask implements TaskInterface {
8+
9+
const CLASS_TYPE = Type::class;
10+
11+
/**
12+
* @return array
13+
*/
14+
public function collect() {
15+
$result = [];
16+
17+
$types = $this->getTypes();
18+
$map = [];
19+
foreach ($types as $type => $className) {
20+
$map[$type] = '\\' . $className . '::class';
21+
}
22+
23+
$result['\\' . static::CLASS_TYPE . '::build(0)'] = $map;
24+
25+
return $result;
26+
}
27+
28+
/**
29+
* @return array
30+
*/
31+
protected function getTypes() {
32+
$types = [];
33+
34+
try {
35+
$allTypes = Type::buildAll();
36+
} catch (Exception $exception) {
37+
return $types;
38+
}
39+
40+
foreach ($allTypes as $key => $type) {
41+
$types[$key] = get_class($type);
42+
}
43+
44+
return $types;
45+
}
46+
47+
}

src/Generator/TaskCollection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Cake\Core\Configure;
55
use IdeHelper\Generator\Task\BehaviorTask;
66
use IdeHelper\Generator\Task\ComponentTask;
7+
use IdeHelper\Generator\Task\DatabaseTypeTask;
78
use IdeHelper\Generator\Task\HelperTask;
89
use IdeHelper\Generator\Task\ModelTask;
910
use IdeHelper\Generator\Task\TableAssociationTask;
@@ -23,6 +24,7 @@ class TaskCollection {
2324
HelperTask::class => HelperTask::class,
2425
TableAssociationTask::class => TableAssociationTask::class,
2526
TableFinderTask::class => TableFinderTask::class,
27+
DatabaseTypeTask::class => DatabaseTypeTask::class,
2628
];
2729

2830
/**
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace IdeHelper\Test\TestCase\Generator\Task;
4+
5+
use App\Database\Type\UuidType;
6+
use Cake\Database\Type;
7+
use IdeHelper\Generator\Task\DatabaseTypeTask;
8+
use Tools\TestSuite\TestCase;
9+
10+
class DatabaseTypeTaskTest extends TestCase {
11+
12+
/**
13+
* @var \IdeHelper\Generator\Task\DatabaseTypeTask
14+
*/
15+
protected $task;
16+
17+
/**
18+
* @return void
19+
*/
20+
public function setUp() {
21+
parent::setUp();
22+
23+
$this->task = new DatabaseTypeTask();
24+
}
25+
26+
/**
27+
* @return void
28+
*/
29+
public function testCollect() {
30+
Type::set('uuid', new UuidType());
31+
32+
$result = $this->task->collect();
33+
34+
$this->assertCount(1, $result);
35+
36+
$expected = '\Cake\Database\Type\BinaryType::class';
37+
$this->assertSame($expected, $result['\Cake\Database\Type::build(0)']['binary']);
38+
39+
$expected = '\App\Database\Type\UuidType::class';
40+
$this->assertSame($expected, $result['\Cake\Database\Type::build(0)']['uuid']);
41+
}
42+
43+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11+
* @link https://cakephp.org CakePHP(tm) Project
12+
* @since 3.0.0
13+
* @license https://opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace App\Database\Type;
16+
17+
use Cake\Database\Type\UuidType as CoreUuidType;
18+
19+
/**
20+
* Provides behavior for the UUID type
21+
*/
22+
class UuidType extends CoreUuidType {
23+
}

tests/test_files/meta/phpstorm/.meta.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,26 @@
180180
])
181181
);
182182

183+
override(
184+
\Cake\Database\Type::build(0),
185+
map([
186+
'tinyinteger' => \Cake\Database\Type\IntegerType::class,
187+
'smallinteger' => \Cake\Database\Type\IntegerType::class,
188+
'integer' => \Cake\Database\Type\IntegerType::class,
189+
'biginteger' => \Cake\Database\Type\IntegerType::class,
190+
'binary' => \Cake\Database\Type\BinaryType::class,
191+
'boolean' => \Cake\Database\Type\BoolType::class,
192+
'date' => \Cake\Database\Type\DateType::class,
193+
'datetime' => \Cake\Database\Type\DateTimeType::class,
194+
'decimal' => \Cake\Database\Type\DecimalType::class,
195+
'float' => \Cake\Database\Type\FloatType::class,
196+
'json' => \Cake\Database\Type\JsonType::class,
197+
'string' => \Cake\Database\Type\StringType::class,
198+
'text' => \Cake\Database\Type\StringType::class,
199+
'time' => \Cake\Database\Type\TimeType::class,
200+
'timestamp' => \Cake\Database\Type\DateTimeType::class,
201+
'uuid' => \Cake\Database\Type\UuidType::class,
202+
])
203+
);
204+
183205
}

0 commit comments

Comments
 (0)