Skip to content

Commit

Permalink
Merge pull request #201 from dereuromark/3.x-backport
Browse files Browse the repository at this point in the history
3.x backport more tasks from 4.x
  • Loading branch information
dereuromark authored May 1, 2020
2 parents cb20655 + a24d579 commit 5a2c006
Show file tree
Hide file tree
Showing 61 changed files with 2,404 additions and 31 deletions.
10 changes: 5 additions & 5 deletions docs/Generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![Model Typehinting](img/model_typehinting.png)

![Model Autocomplete](img/model_autocomplete.png)
![Model Autocomplete](img/model_autocomplete.png)

## Phpstorm
This command will generate your `.ide-helper.meta.php` in your app's `ROOT/.phpstorm.meta.php/` directory:
Expand Down Expand Up @@ -116,7 +116,7 @@ class MyTask implements TaskInterface {
public function collect() {
...
}

}
```

Expand Down Expand Up @@ -158,10 +158,10 @@ $directive = new Override($method, $map);

##### ExpectedArguments
With this you can set default values to chose from for method arguments.
Specify the parameter count as 0-based value.
Specify the parameter count as 0-based value.
```php
$method = '\Namespace\PackageName\MyFactory::create()';
$position = 0;
$position = 0;
$list = [
'alpha',
'beta',
Expand Down Expand Up @@ -191,7 +191,7 @@ $list = [
];
$directive = new RegisterArgumentsSet($set, $list);
```
Now you can use it as list value `argumentsSet("mySet")` inside the others.
Now you can use it as list value `argumentsSet('mySet')` inside the others.
For this just pass the `$directive` object itself to the list, which then contains only this one element.

#### Example
Expand Down
63 changes: 63 additions & 0 deletions src/Generator/Directive/ExitPoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace IdeHelper\Generator\Directive;

/**
* Helps to annotate expected exit point methods.
*
* This is available since PhpStorm 2020.01
*
* ### Example
*
* exitPoint(\Cake\Console\ConsoleIo::abort());
*
* @see https://www.jetbrains.com/help/phpstorm/ide-advanced-metadata.html#define-exit-points
*/
class ExitPoint extends BaseDirective {

const NAME = 'exitPoint';

/**
* @var string
*/
protected $method;

/**
* @param string $method
*/
public function __construct($method) {
$this->method = $method;
}

/**
* Key for sorting inside collection.
*
* @return string
*/
public function key() {
return $this->method . '@' . static::NAME;
}

/**
* @return array
*/
public function toArray() {
return [
'method' => $this->method,
];
}

/**
* @return string
*/
public function build() {
$method = $this->method;

$result = <<<TXT
exitPoint($method);
TXT;

return $result;
}

}
2 changes: 1 addition & 1 deletion src/Generator/Directive/ExpectedArguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* expectedArguments(
* \MyClass::getFlags(),
* 0,
* argumentsSet("myFileObjectFlags")
* argumentsSet('myFileObjectFlags')
* );
*/
class ExpectedArguments extends BaseDirective {
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Directive/ExpectedReturnValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
* expectedReturnValues(
* \MyClass::getFlags(),
* argumentsSet("myFileObjectFlags")
* argumentsSet('myFileObjectFlags')
* );
*/
class ExpectedReturnValues extends BaseDirective {
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/Directive/RegisterArgumentsSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* \MyClass::REQUIRED
* );
*
* Then it can be used in other places as argumentsSet("mySet").
* Then it can be used in other places as argumentsSet('mySet').
*/
class RegisterArgumentsSet extends BaseDirective {

Expand Down Expand Up @@ -78,7 +78,7 @@ public function build() {
* @return string
*/
public function __toString() {
return 'argumentsSet("' . $this->set . '")';
return 'argumentsSet(\'' . $this->set . '\')';
}

}
67 changes: 67 additions & 0 deletions src/Generator/Task/CacheTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace IdeHelper\Generator\Task;

use Cake\Cache\Cache;
use IdeHelper\Generator\Directive\ExpectedArguments;
use IdeHelper\Generator\Directive\RegisterArgumentsSet;
use IdeHelper\ValueObject\StringName;

class CacheTask implements TaskInterface {

const CLASS_CACHE = Cache::class;

const SET_CACHE_ENGINES = 'cacheEngines';

/**
* @var int[]
*/
protected $aliases = [
'\\' . self::CLASS_CACHE . '::clear()' => 0,
'\\' . self::CLASS_CACHE . '::read()' => 1,
'\\' . self::CLASS_CACHE . '::readMany()' => 1,
'\\' . self::CLASS_CACHE . '::delete()' => 1,
'\\' . self::CLASS_CACHE . '::deleteMany()' => 1,
'\\' . self::CLASS_CACHE . '::clearGroup()' => 1,
'\\' . self::CLASS_CACHE . '::add()' => 2,
'\\' . self::CLASS_CACHE . '::write()' => 2,
'\\' . self::CLASS_CACHE . '::increment()' => 2,
'\\' . self::CLASS_CACHE . '::decrement()' => 2,
'\\' . self::CLASS_CACHE . '::remember()' => 2,
];

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

$list = $this->collectCacheEngines();
$registerArgumentsSet = new RegisterArgumentsSet(static::SET_CACHE_ENGINES, $list);
$result[$registerArgumentsSet->key()] = $registerArgumentsSet;

foreach ($this->aliases as $alias => $position) {
$directive = new ExpectedArguments($alias, $position, [$registerArgumentsSet]);
$result[$directive->key()] = $directive;
}

return $result;
}

/**
* @return \IdeHelper\ValueObject\StringName[]
*/
protected function collectCacheEngines() {
$cacheEngines = Cache::configured();

$result = [];
foreach ($cacheEngines as $cacheEngine) {
$result[$cacheEngine] = StringName::create($cacheEngine);
}

ksort($result);

return $result;
}

}
43 changes: 43 additions & 0 deletions src/Generator/Task/ConnectionTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace IdeHelper\Generator\Task;

use Cake\Datasource\ConnectionManager;
use IdeHelper\Generator\Directive\ExpectedArguments;
use IdeHelper\ValueObject\StringName;

class ConnectionTask extends ModelTask {

const METHOD_GET = '\\' . ConnectionManager::class . '::get()';

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

$keys = $this->connectionKeys();

ksort($keys);

$directive = new ExpectedArguments(static::METHOD_GET, 0, $keys);
$result[$directive->key()] = $directive;

return $result;
}

/**
* @return \IdeHelper\ValueObject\StringName[]
*/
protected function connectionKeys() {
$configured = ConnectionManager::configured();

$list = [];
foreach ($configured as $key) {
$list[$key] = StringName::create($key);
}

return $list;
}

}
24 changes: 24 additions & 0 deletions src/Generator/Task/ConsoleTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace IdeHelper\Generator\Task;

use Cake\Console\ConsoleIo;
use IdeHelper\Generator\Directive\ExitPoint;

class ConsoleTask implements TaskInterface {

const METHOD_ABORT = '\\' . ConsoleIo::class . '::abort()';

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

$directive = new ExitPoint(static::METHOD_ABORT);
$result[$directive->key()] = $directive;

return $result;
}

}
82 changes: 82 additions & 0 deletions src/Generator/Task/DatabaseTableColumnNameTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace IdeHelper\Generator\Task;

use Cake\Datasource\ConnectionManager;
use IdeHelper\Generator\Directive\ExpectedArguments;
use IdeHelper\Generator\Directive\RegisterArgumentsSet;
use IdeHelper\ValueObject\StringName;

/**
* This task is useful when using Migrations plugin and creating Migration files.
*/
class DatabaseTableColumnNameTask extends DatabaseTableTask {

const SET_TABLE_NAMES = 'tableNames';

/**
* @var string[]
*/
protected $aliases = [
'\Migrations\Table::addColumn()',
'\Migrations\Table::changeColumn()',
'\Migrations\Table::removeColumn()',
'\Migrations\Table::renameColumn()',
'\Migrations\Table::hasColumn()',
];

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

$names = $this->collectTableColumnNames();
foreach ($names as $type) {
$list[$type] = StringName::create($type);
}

ksort($list);

$result = [];
$registerArgumentsSet = new RegisterArgumentsSet(static::SET_TABLE_NAMES, $list);
$result[$registerArgumentsSet->key()] = $registerArgumentsSet;

foreach ($this->aliases as $alias) {
$directive = new ExpectedArguments($alias, 0, [$registerArgumentsSet]);
$result[$directive->key()] = $directive;
}

return $result;
}

/**
* @return string[]
*/
protected function collectTableColumnNames() {
$schema = $this->getConnection()->getSchemaCollection();

$tables = $this->collectTables();

$columns = [];
foreach ($tables as $table) {
$tableSchema = $schema->describe($table);
$columns = array_merge($columns, $tableSchema->columns());
}

return array_unique($columns);
}

/**
* @param string $name
*
* @return \Cake\Database\Connection
*/
protected function getConnection($name = 'default') {
/** @var \Cake\Database\Connection $connection */
$connection = ConnectionManager::get($name);

return $connection;
}

}
Loading

0 comments on commit 5a2c006

Please sign in to comment.