From f0cef721723e64e1ec6162f4ddc9385ca6faf20e Mon Sep 17 00:00:00 2001 From: Yevgeny Tomenko Date: Sat, 27 Apr 2019 22:51:15 +0300 Subject: [PATCH 01/10] update to cakephp 3.7 --- .semver | 2 +- .travis.yml | 17 +- clover.xml | 243 ++++++++++++++++++ composer.json | 22 +- phpstan.neon | 4 + psalm.xml | 126 +++++++++ src/Model/Behavior/EnumBehavior.php | 3 +- .../Behavior/Strategy/AbstractStrategy.php | 14 +- .../Behavior/Strategy/LookupStrategy.php | 6 +- .../Behavior/Strategy/StrategyInterface.php | 9 +- src/Plugin.php | 25 ++ .../Model/Behavior/EnumBehaviorTest.php | 7 +- tests/bootstrap.php | 3 +- 13 files changed, 450 insertions(+), 31 deletions(-) create mode 100644 clover.xml create mode 100644 phpstan.neon create mode 100644 psalm.xml create mode 100644 src/Plugin.php diff --git a/.semver b/.semver index bd15fca..971d0dd 100644 --- a/.semver +++ b/.semver @@ -1,6 +1,6 @@ --- :major: 1 -:minor: 2 +:minor: 5 :patch: 0 :special: '' :metadata: '' diff --git a/.travis.yml b/.travis.yml index 3f749e7..e2b597d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,9 @@ language: php php: - - 5.5 - 5.6 - - 7.0 - 7.1 + - 7.2 sudo: false @@ -21,22 +20,26 @@ matrix: fast_finish: true include: - - php: 5.6 + - php: 7.3 env: PHPCS=1 DEFAULT=0 before_script: - - if [[ $TRAVIS_PHP_VERSION != 7.0 ]]; then phpenv config-rm xdebug.ini; fi + - if [[ $TRAVIS_PHP_VERSION != 7.3 ]]; then phpenv config-rm xdebug.ini; fi - composer install --prefer-dist --no-interaction - if [ $DB = 'mysql' ]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi - if [ $DB = 'pgsql' ]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi + - if [[ $PHPSTAN = 1 ]]; then composer stan-setup; fi + script: - - if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.0 ]]; then vendor/bin/phpunit; fi - - if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then vendor/bin/phpunit --coverage-clover=clover.xml; fi + - if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.3 ]]; then composer test; fi + - if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.3 ]]; then composer coverage-test; fi - - if [ '$PHPCS' = '1' ]; then ./vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi + - if [[ $PHPCS = 1 ]]; then composer cs-check; fi + + - if [[ $PHPSTAN = 1 ]]; then composer stan; fi after_success: - if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then bash <(curl -s https://codecov.io/bash); fi diff --git a/clover.xml b/clover.xml new file mode 100644 index 0000000..ae46fa7 --- /dev/null +++ b/clover.xml @@ -0,0 +1,243 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/composer.json b/composer.json index a39d06e..57f8cd8 100644 --- a/composer.json +++ b/composer.json @@ -13,10 +13,12 @@ "issues": "https://github.com/cakedc/enum/issues", "source": "https://github.com/cakedc/enum" }, + "require": { + "cakephp/cakephp": "^3.7" + }, "require-dev": { - "cakephp/cakephp": "^3.6.0", - "cakephp/cakephp-codesniffer": "2.*", - "phpunit/phpunit": "<6.0" + "cakephp/cakephp-codesniffer": "dev-master", + "phpunit/phpunit": "^6" }, "autoload": { "psr-4": { @@ -27,5 +29,19 @@ "psr-4": { "CakeDC\\Enum\\Test\\": "tests" } + }, + "scripts": { + "check": [ + "@cs-check", + "@test", + "@stan" + ], + "cs-check": "phpcs -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/", + "cs-fix": "phpcbf --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/", + "test": "phpunit --stderr", + "stan": "phpstan analyse src/ && psalm --show-info=false", + "psalm": "psalm --show-info=false", + "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.11 vimeo/psalm:^3.0 && mv composer.backup composer.json", + "coverage-test": "phpunit --stderr --coverage-clover=clover.xml" } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..6aa4eed --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,4 @@ +parameters: + level: 4 + autoload_files: + - tests/bootstrap.php diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..f737bed --- /dev/null +++ b/psalm.xml @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Model/Behavior/EnumBehavior.php b/src/Model/Behavior/EnumBehavior.php index 69f767c..d2c1e14 100644 --- a/src/Model/Behavior/EnumBehavior.php +++ b/src/Model/Behavior/EnumBehavior.php @@ -120,11 +120,12 @@ public function strategy($alias, $strategy) return $strategy; } + $class = null; if (isset($this->_classMap[$strategy])) { $class = $this->_classMap[$strategy]; } - if (!class_exists($class)) { + if ($class === null || !class_exists($class)) { throw new MissingEnumStrategyException([$class]); } diff --git a/src/Model/Behavior/Strategy/AbstractStrategy.php b/src/Model/Behavior/Strategy/AbstractStrategy.php index d56f1c8..c1c07ce 100644 --- a/src/Model/Behavior/Strategy/AbstractStrategy.php +++ b/src/Model/Behavior/Strategy/AbstractStrategy.php @@ -36,6 +36,13 @@ abstract class AbstractStrategy implements StrategyInterface */ protected $_table; + /** + * Table alias. + * + * @var string + */ + protected $_alias; + /** * Constructor. * @@ -51,15 +58,10 @@ public function __construct($alias, Table $table) /** * {@inheritdoc} * - * @param mixed $config Strategy's configuration. * @return $this */ - public function initialize($config) + public function initialize(array $config) { - if (is_string($config)) { - $config = ['prefix' => $config]; - } - $prefix = $this->getConfig('prefix'); if (empty($config['prefix']) && empty($prefix)) { $config['prefix'] = $this->_generatePrefix(); diff --git a/src/Model/Behavior/Strategy/LookupStrategy.php b/src/Model/Behavior/Strategy/LookupStrategy.php index 470c1ed..9d7c229 100644 --- a/src/Model/Behavior/Strategy/LookupStrategy.php +++ b/src/Model/Behavior/Strategy/LookupStrategy.php @@ -13,6 +13,7 @@ namespace CakeDC\Enum\Model\Behavior\Strategy; use Cake\Datasource\ModelAwareTrait; +use Cake\ORM\Locator\LocatorAwareTrait; use Cake\ORM\Table; use Cake\ORM\TableRegistry; use Cake\Utility\Inflector; @@ -20,6 +21,7 @@ class LookupStrategy extends AbstractStrategy { + use LocatorAwareTrait; use ModelAwareTrait; /** @@ -32,7 +34,7 @@ public function __construct($alias, Table $table) { parent::__construct($alias, $table); $this->modelClass = 'CakeDC/Enum.Lookups'; - $this->modelFactory('Table', ['Cake\ORM\TableRegistry', 'get']); + $this->modelFactory('Table', [$this->getTableLocator(), 'get']); } /** @@ -67,7 +69,7 @@ public function enum(array $config = []) * @param array $config Strategy's configuration. * @return $this */ - public function initialize($config) + public function initialize(array $config) { $config = parent::initialize($config)->getConfig(); $assocName = Inflector::pluralize(Inflector::classify($this->_alias)); diff --git a/src/Model/Behavior/Strategy/StrategyInterface.php b/src/Model/Behavior/Strategy/StrategyInterface.php index d4e506b..41813f4 100644 --- a/src/Model/Behavior/Strategy/StrategyInterface.php +++ b/src/Model/Behavior/Strategy/StrategyInterface.php @@ -16,7 +16,7 @@ interface StrategyInterface { /** - * @param mixed $config Strategy's configuration. + * @param array $config Strategy's configuration. * @return array */ public function enum(array $config = []); @@ -25,14 +25,13 @@ public function enum(array $config = []); * @param array $config Configuration. * @return \CakeDC\Enum\Model\Behavior\Strategy\StrategyInterface */ - public function initialize($config); + public function initialize(array $config); /** * @param string|array|null $key The key to get/set, or a complete array of configs. - * @param mixed|null $value The value to set. - * @param bool $merge Whether to recursively merge or overwrite existing config, defaults to true. + * @param mixed|null $default The value to set. * @return mixed Config value being read, or the object itself on write operations. * @throws \Cake\Core\Exception\Exception When trying to set a key that is invalid. */ - public function config($key = null, $value = null, $merge = true); + public function getConfig($key = null, $default = null); } diff --git a/src/Plugin.php b/src/Plugin.php new file mode 100644 index 0000000..d858565 --- /dev/null +++ b/src/Plugin.php @@ -0,0 +1,25 @@ +Articles = TableRegistry::get('CakeDC/Enum.Articles', [ 'className' => 'CakeDC\Enum\Test\TestCase\Model\Behavior\ArticlesTable', diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 7a22191..b04f4c3 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -34,8 +34,7 @@ require $root . '/vendor/cakephp/cakephp/tests/bootstrap.php'; -\Cake\Core\Plugin::load('CakeDC/Enum', ['path' => dirname(dirname(__FILE__)) . DS]); - +\Cake\Core\Plugin::getCollection()->add(new \CakeDC\Enum\Plugin(['path' => dirname(dirname(__FILE__)) . DS])); I18n::config('default', function ($name, $locale) { $package = new Package('default'); $messages = [ From b19e05bb5d271a4452557b1bc4a5394a346c30f5 Mon Sep 17 00:00:00 2001 From: Yevgeny Tomenko Date: Sat, 27 Apr 2019 22:57:11 +0300 Subject: [PATCH 02/10] update docs --- .travis.yml | 18 +++++++++--------- README.md | 5 +++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index e2b597d..a6fcbff 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 5.6 - 7.1 - 7.2 @@ -20,29 +19,30 @@ matrix: fast_finish: true include: + - php: 7.3 + env: COVERAGE=1 DEFAULT=0 + - php: 7.3 env: PHPCS=1 DEFAULT=0 + - php: 7.3 + env: PHPSTAN=1 DEFAULT=0 + before_script: - if [[ $TRAVIS_PHP_VERSION != 7.3 ]]; then phpenv config-rm xdebug.ini; fi - - composer install --prefer-dist --no-interaction - - if [ $DB = 'mysql' ]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi - if [ $DB = 'pgsql' ]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi - - if [[ $PHPSTAN = 1 ]]; then composer stan-setup; fi script: - - if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.3 ]]; then composer test; fi - - if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.3 ]]; then composer coverage-test; fi - + - if [[ $DEFAULT = 1 ]]; then composer test; fi + - if [[ $COVERAGE = 1 ]]; then composer coverage-test; fi - if [[ $PHPCS = 1 ]]; then composer cs-check; fi - - if [[ $PHPSTAN = 1 ]]; then composer stan; fi after_success: - - if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then bash <(curl -s https://codecov.io/bash); fi + - if [[ $COVERAGE = 1 ]]; then bash <(curl -s https://codecov.io/bash); fi notifications: email: false diff --git a/README.md b/README.md index 2da54c2..3be0c12 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,9 @@ Versions and branches | CakePHP | CakeDC Enum Plugin | Tag | Notes | | :-------------: | :------------------------: | :--: | :---- | -| ^3.6 | [master](https://github.com/cakedc/enum/tree/master) | 1.4.0 | stable | -| ^3.6 | [develop](https://github.com/cakedc/enum/tree/develop) | - | unstable | +| ^3.7 | [master](https://github.com/cakedc/enum/tree/master) | 1.5.0 | stable | +| ^3.7 | [develop](https://github.com/cakedc/enum/tree/develop) | - | unstable | +| ^3.6 | [master](https://github.com/cakedc/enum/tree/1.4.0) | 1.4.0 | stable | | 3.1 - 3.5 | | 1.3.0 | stable | From 70e7dd2926a6e5b19f871c52ba8969cbb6551368 Mon Sep 17 00:00:00 2001 From: Yevgeny Tomenko Date: Sat, 27 Apr 2019 23:10:51 +0300 Subject: [PATCH 03/10] update docs --- README.md | 8 ++++---- src/Model/Behavior/EnumBehavior.php | 4 ++-- .../Exception/MissingEnumConfigurationException.php | 4 ++-- .../Behavior/Exception/MissingEnumStrategyException.php | 4 ++-- .../Exception/MissingEnumStrategyPrefixException.php | 4 ++-- src/Model/Behavior/Strategy/AbstractStrategy.php | 4 ++-- src/Model/Behavior/Strategy/ConfigStrategy.php | 4 ++-- src/Model/Behavior/Strategy/ConstStrategy.php | 4 ++-- src/Model/Behavior/Strategy/LookupStrategy.php | 4 ++-- src/Model/Behavior/Strategy/StrategyInterface.php | 4 ++-- src/Model/Table/LookupsTable.php | 4 ++-- src/Plugin.php | 4 ++-- tests/Fixture/ArticlesFixture.php | 4 ++-- tests/Fixture/LookupsFixture.php | 4 ++-- tests/TestCase/Model/Behavior/EnumBehaviorTest.php | 4 ++-- .../Model/Behavior/Strategy/ConfigStrategyTest.php | 4 ++-- .../Model/Behavior/Strategy/ConstStrategyTest.php | 4 ++-- .../Model/Behavior/Strategy/LookupStrategyTest.php | 4 ++-- tests/bootstrap.php | 4 ++-- 19 files changed, 40 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 3be0c12..a81f15a 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Install Using [Composer](http://getcomposer.org): ``` -composer require cakedc/enum:~1.0 +composer require cakedc/enum:1.5.0 ``` You then need to load the plugin. You can use the shell command: @@ -43,8 +43,8 @@ Plugin::load('CakeDC/Enum'); Requirements ------------ -* CakePHP 3.1+ -* PHP 5.4.16+ Note CakePHP 3.2 requires PHP 5.5 so 5.4 compatibility would be dropped sooner than later... +* CakePHP 3.7+ +* PHP 5.6+ Documentation ------------- @@ -66,6 +66,6 @@ This repository follows the [CakeDC Plugin Standard](http://cakedc.com/plugin-st License ------- -Copyright 2015 - 2018 Cake Development Corporation (CakeDC). All rights reserved. +Copyright 2015 - 2019 Cake Development Corporation (CakeDC). All rights reserved. Licensed under the [MIT](http://www.opensource.org/licenses/mit-license.php) License. Redistributions of the source code included in this repository must retain the copyright notice found in each file. diff --git a/src/Model/Behavior/EnumBehavior.php b/src/Model/Behavior/EnumBehavior.php index d2c1e14..e284f25 100644 --- a/src/Model/Behavior/EnumBehavior.php +++ b/src/Model/Behavior/EnumBehavior.php @@ -1,12 +1,12 @@ Date: Tue, 30 Apr 2019 13:26:50 +0300 Subject: [PATCH 04/10] initial cakephp 4.x version --- composer.json | 10 ++- rector.yml | 4 ++ src/Model/Behavior/EnumBehavior.php | 37 +++++----- .../MissingEnumConfigurationException.php | 2 +- .../MissingEnumStrategyException.php | 2 +- .../MissingEnumStrategyPrefixException.php | 2 +- .../Behavior/Strategy/AbstractStrategy.php | 14 ++-- .../Behavior/Strategy/ConfigStrategy.php | 7 +- src/Model/Behavior/Strategy/ConstStrategy.php | 9 ++- .../Behavior/Strategy/LookupStrategy.php | 17 ++--- .../Behavior/Strategy/StrategyInterface.php | 46 +++++++++--- src/Model/Table/LookupsTable.php | 5 +- src/Plugin.php | 2 +- tests/Fixture/ArticlesFixture.php | 2 +- .../Model/Behavior/EnumBehaviorTest.php | 72 +++++++++---------- .../Behavior/Strategy/ConfigStrategyTest.php | 6 +- .../Behavior/Strategy/ConstStrategyTest.php | 22 +++--- .../Behavior/Strategy/LookupStrategyTest.php | 5 +- tests/bootstrap.php | 4 +- 19 files changed, 145 insertions(+), 123 deletions(-) create mode 100644 rector.yml diff --git a/composer.json b/composer.json index 57f8cd8..f19d747 100644 --- a/composer.json +++ b/composer.json @@ -14,11 +14,12 @@ "source": "https://github.com/cakedc/enum" }, "require": { - "cakephp/cakephp": "^3.7" + "cakephp/cakephp": "4.x-dev as 4.0.0" }, "require-dev": { - "cakephp/cakephp-codesniffer": "dev-master", - "phpunit/phpunit": "^6" + "phpunit/phpunit": "^7", + "vlucas/phpdotenv": "^3.3", + "cakephp/cakephp-codesniffer": "dev-next" }, "autoload": { "psr-4": { @@ -30,6 +31,8 @@ "CakeDC\\Enum\\Test\\": "tests" } }, + "prefer-stable": true, + "minimum-stability": "dev", "scripts": { "check": [ "@cs-check", @@ -42,6 +45,7 @@ "stan": "phpstan analyse src/ && psalm --show-info=false", "psalm": "psalm --show-info=false", "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.11 vimeo/psalm:^3.0 && mv composer.backup composer.json", + "rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:^0.4.11 && mv composer.backup composer.json", "coverage-test": "phpunit --stderr --coverage-clover=clover.xml" } } diff --git a/rector.yml b/rector.yml new file mode 100644 index 0000000..eaa99e6 --- /dev/null +++ b/rector.yml @@ -0,0 +1,4 @@ +# rector.yaml +services: + Rector\Php\Rector\FunctionLike\ParamTypeDeclarationRector: ~ + Rector\Php\Rector\FunctionLike\ReturnTypeDeclarationRector: ~ diff --git a/src/Model/Behavior/EnumBehavior.php b/src/Model/Behavior/EnumBehavior.php index e284f25..764a91e 100644 --- a/src/Model/Behavior/EnumBehavior.php +++ b/src/Model/Behavior/EnumBehavior.php @@ -1,5 +1,5 @@ _normalizeConfig(); @@ -108,7 +108,7 @@ public function initialize(array $config) * @return \CakeDC\Enum\Model\Behavior\Strategy\StrategyInterface * @throws \CakeDC\Enum\Model\Behavior\Exception\MissingEnumStrategyException */ - public function strategy($alias, $strategy) + public function strategy(string $alias, $strategy): \CakeDC\Enum\Model\Behavior\Strategy\StrategyInterface { if (!empty($this->_strategies[$alias])) { return $this->_strategies[$alias]; @@ -137,7 +137,7 @@ public function strategy($alias, $strategy) * * @return void */ - protected function _normalizeConfig() + protected function _normalizeConfig(): void { $classMap = $this->getConfig('classMap'); $this->_classMap = array_merge($this->_classMap, $classMap); @@ -161,9 +161,9 @@ protected function _normalizeConfig() $config['strategy'] = $defaultStrategy; } - $lists[$alias] = $this->strategy($alias, $config['strategy']) - ->initialize($config) - ->getConfig(); + $strategy = $this->strategy($alias, $config['strategy']); + $strategy->initialize($config); + $lists[$alias] = $strategy->getConfig(); } $this->setConfig('lists', $lists, false); @@ -174,7 +174,7 @@ protected function _normalizeConfig() * @return array * @throws \CakeDC\Enum\Model\Behavior\Exception\MissingEnumConfigurationException */ - public function enum($alias = null) + public function enum($alias = null): array { if (is_string($alias)) { $config = $this->getConfig('lists.' . $alias); @@ -203,7 +203,7 @@ public function enum($alias = null) * @param array $config Config * @return array */ - protected function _enumList($alias, array $config) + protected function _enumList(string $alias, array $config): array { $return = $this->strategy($alias, $config['strategy'])->enum($config); if ($this->getConfig('translate')) { @@ -230,7 +230,7 @@ function (&$item, $val) { * @param array $list List. * @return array */ - protected function _translate(array $list) + protected function _translate(array $list): array { $domain = $this->getConfig('translationDomain'); @@ -244,7 +244,7 @@ protected function _translate(array $list) * @param \Cake\ORM\RulesChecker $rules Rules checker. * @return \Cake\ORM\RulesChecker */ - public function buildRules(Event $event, RulesChecker $rules) + public function buildRules(Event $event, RulesChecker $rules): \Cake\ORM\RulesChecker { foreach ($this->getConfig('lists') as $alias => $config) { if (Hash::get($config, 'applicationRules') === false) { @@ -254,7 +254,7 @@ public function buildRules(Event $event, RulesChecker $rules) $ruleName = 'isValid' . Inflector::camelize($alias); $rules->add([$this, $ruleName], $ruleName, [ 'errorField' => $config['field'], - 'message' => $config['errorMessage'] + 'message' => $config['errorMessage'], ]); } @@ -270,16 +270,17 @@ public function buildRules(Event $event, RulesChecker $rules) * @throws \BadMethodCallException * @throws \CakeDC\Enum\Model\Behavior\Exception\MissingEnumConfigurationException */ - public function __call($method, $args) + public function __call(string $method, array $args): bool { if (strpos($method, 'isValid') !== 0) { throw new BadMethodCallException(sprintf('Call to undefined method (%s)', $method)); } $alias = Inflector::underscore(str_replace('isValid', '', $method)); - list($entity, ) = $args; + [$entity, ] = $args; - if (!$config = $this->getConfig('lists.' . $alias)) { + $config = $this->getConfig('lists.' . $alias); + if ($config === null) { throw new MissingEnumConfigurationException([$alias]); } diff --git a/src/Model/Behavior/Exception/MissingEnumConfigurationException.php b/src/Model/Behavior/Exception/MissingEnumConfigurationException.php index a060bcf..18415cc 100644 --- a/src/Model/Behavior/Exception/MissingEnumConfigurationException.php +++ b/src/Model/Behavior/Exception/MissingEnumConfigurationException.php @@ -1,5 +1,5 @@ _alias = $alias; $this->_table = $table; @@ -58,9 +56,9 @@ public function __construct($alias, Table $table) /** * {@inheritdoc} * - * @return $this + * @return void */ - public function initialize(array $config) + public function initialize(array $config): void { $prefix = $this->getConfig('prefix'); if (empty($config['prefix']) && empty($prefix)) { @@ -76,8 +74,6 @@ public function initialize(array $config) } $this->setConfig($config); - - return $this; } /** @@ -85,7 +81,7 @@ public function initialize(array $config) * * @return string */ - protected function _generatePrefix() + protected function _generatePrefix(): string { $prefix = Inflector::underscore(Inflector::singularize($this->_table->getAlias())); $prefix .= '_' . $this->_alias; diff --git a/src/Model/Behavior/Strategy/ConfigStrategy.php b/src/Model/Behavior/Strategy/ConfigStrategy.php index a5b4d8f..ba3a3d8 100644 --- a/src/Model/Behavior/Strategy/ConfigStrategy.php +++ b/src/Model/Behavior/Strategy/ConfigStrategy.php @@ -1,5 +1,5 @@ getConfig('prefix')); } diff --git a/src/Model/Behavior/Strategy/ConstStrategy.php b/src/Model/Behavior/Strategy/ConstStrategy.php index a03cf6f..d275f04 100644 --- a/src/Model/Behavior/Strategy/ConstStrategy.php +++ b/src/Model/Behavior/Strategy/ConstStrategy.php @@ -1,5 +1,5 @@ _defaultConfig['prefix'] = strtoupper($alias); @@ -44,7 +43,7 @@ public function __construct($alias, Table $table) * @param array $config List of callable filters to limit items generated from list. * @return array */ - public function enum(array $config = []) + public function enum(array $config = []): array { $constants = $this->_getConstants(); $keys = array_keys($constants); @@ -67,7 +66,7 @@ public function enum(array $config = []) * * @return array */ - protected function _getConstants() + protected function _getConstants(): array { if (isset($this->_constants)) { return $this->_constants; diff --git a/src/Model/Behavior/Strategy/LookupStrategy.php b/src/Model/Behavior/Strategy/LookupStrategy.php index cb91d86..abd8663 100644 --- a/src/Model/Behavior/Strategy/LookupStrategy.php +++ b/src/Model/Behavior/Strategy/LookupStrategy.php @@ -1,5 +1,5 @@ modelClass = 'CakeDC/Enum.Lookups'; @@ -43,7 +41,7 @@ public function __construct($alias, Table $table) * @param array $config (unused in this case). * @return array */ - public function enum(array $config = []) + public function enum(array $config = []): array { $query = $this->loadModel() ->find('list', [ @@ -67,11 +65,12 @@ public function enum(array $config = []) * {@inheritdoc} * * @param array $config Strategy's configuration. - * @return $this + * @return void */ - public function initialize(array $config) + public function initialize(array $config): void { - $config = parent::initialize($config)->getConfig(); + parent::initialize($config); + $config = $this->getConfig(); $assocName = Inflector::pluralize(Inflector::classify($this->_alias)); $this->_table->belongsTo($assocName, [ @@ -80,7 +79,5 @@ public function initialize(array $config) 'bindingKey' => 'name', 'conditions' => [$assocName . '.prefix' => $config['prefix']], ]); - - return $this; } } diff --git a/src/Model/Behavior/Strategy/StrategyInterface.php b/src/Model/Behavior/Strategy/StrategyInterface.php index 463da4e..ea542fa 100644 --- a/src/Model/Behavior/Strategy/StrategyInterface.php +++ b/src/Model/Behavior/Strategy/StrategyInterface.php @@ -1,5 +1,5 @@ getConfig(); + * ``` + * + * Reading a specific value: + * + * ``` + * $this->getConfig('key'); + * ``` + * + * Reading a nested value: + * + * ``` + * $this->getConfig('some.nested.key'); + * ``` + * + * Reading with default value: + * + * ``` + * $this->getConfig('some-key', 'default-value'); + * ``` + * + * @param string|null $key The key to get or null for the whole config. + * @param mixed $default The return value when the key does not exist. + * @return mixed Config value being read. */ - public function getConfig($key = null, $default = null); + public function getConfig(?string $key = null, $default = null); } diff --git a/src/Model/Table/LookupsTable.php b/src/Model/Table/LookupsTable.php index 4111fd3..995681f 100644 --- a/src/Model/Table/LookupsTable.php +++ b/src/Model/Table/LookupsTable.php @@ -1,5 +1,5 @@ setTable('enum_lookups'); $this->setDisplayField('label'); diff --git a/src/Plugin.php b/src/Plugin.php index 774c8b9..6d80104 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -1,5 +1,5 @@ '', 'priority' => 'HIGH', 'status' => 'PUBLIC', - 'article_category' => 6 + 'article_category' => 6, ], ]; } diff --git a/tests/TestCase/Model/Behavior/EnumBehaviorTest.php b/tests/TestCase/Model/Behavior/EnumBehaviorTest.php index 8525878..b010a14 100644 --- a/tests/TestCase/Model/Behavior/EnumBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/EnumBehaviorTest.php @@ -1,5 +1,5 @@ addBehavior('CakeDC/Enum.Enum', ['lists' => [ 'priority' => ['errorMessage' => 'Invalid priority', 'prefix' => 'PRIORITY'], @@ -49,11 +49,11 @@ public function initialize(array $config) class ThirdPartyStrategy extends AbstractStrategy { - public function enum(array $config = []) + public function enum(array $config = []): array { return [ 1 => 'PHP', - 2 => 'CSS' + 2 => 'CSS', ]; } } @@ -78,7 +78,7 @@ public function setUp() $this->Articles = TableRegistry::get('CakeDC/Enum.Articles', [ 'className' => 'CakeDC\Enum\Test\TestCase\Model\Behavior\ArticlesTable', - 'table' => 'enum_articles' + 'table' => 'enum_articles', ]); } @@ -100,7 +100,7 @@ public function provideBasicConfiguration() 'strategy' => 'lookup', 'prefix' => 'PRIORITY', 'field' => 'priority', - 'errorMessage' => 'Invalid priority' + 'errorMessage' => 'Invalid priority', ], 'status' => [ 'strategy' => 'const', @@ -113,31 +113,31 @@ public function provideBasicConfiguration() 'strategy' => 'config', 'prefix' => 'ARTICLE_CATEGORY', 'field' => 'category', - 'errorMessage' => 'The provided value is invalid' + 'errorMessage' => 'The provided value is invalid', ], 'node_type' => [ 'strategy' => 'const', 'prefix' => 'NODE_TYPE', 'field' => 'node_type', 'errorMessage' => 'The provided value is invalid', - 'lowercase' => false + 'lowercase' => false, ], 'node_group' => [ 'strategy' => 'const', 'prefix' => 'NODE_GROUP', 'field' => 'node_group', 'errorMessage' => 'The provided value is invalid', - 'lowercase' => true + 'lowercase' => true, ], 'optional' => [ 'strategy' => 'const', 'prefix' => 'OPTIONAL', 'field' => 'optional', 'errorMessage' => 'The provided value is invalid', - 'lowercase' => true + 'lowercase' => true, ], ], - 'classMap' => [] + 'classMap' => [], ]; return [ @@ -152,7 +152,7 @@ public function provideBasicConfiguration() 'optional' => ['strategy' => 'const', 'lowercase' => true], ], ], - $expected + $expected, ], ]; } @@ -178,7 +178,7 @@ public function provideBasicLookups() 'URGENT' => 'Urgent', 'HIGH' => 'High', 'NORMAL' => 'Normal', - ] + ], ], [ 'status', @@ -186,33 +186,33 @@ public function provideBasicLookups() 'PUBLIC' => 'Published', 'DRAFT' => 'Drafted', 'ARCHIVE' => 'Archived', - ] + ], ], [ 'category', [ 'CakePHP', 'Open Source Software', - ] + ], ], [ 'node_type', [ 'PAGE' => 'Page', 'BLOG' => 'Blog', - ] + ], ], [ 'node_group', [ 'active' => 'Active', - ] + ], ], [ 'norules', [ 'FOO' => 'Foo', - ] + ], ], ]; } @@ -241,7 +241,7 @@ public function provideBuildRules() ], [ 'category' => ['isValidCategory' => 'The provided value is invalid'], - ] + ], ], [ [ @@ -256,8 +256,8 @@ public function provideBuildRules() 'priority' => ['isValidPriority' => 'Invalid priority'], 'status' => ['isValidStatus' => 'The provided value is invalid'], 'node_type' => ['isValidNodeType' => 'The provided value is invalid'], - ] - ] + ], + ], ]; } @@ -361,7 +361,7 @@ public function testTranslatedValues() $expected = [ 'node_group' => ['active' => 'Translated Active'], - 'norules' => ['FOO' => 'translated foo'] + 'norules' => ['FOO' => 'translated foo'], ]; $this->assertEquals($expected, $result); } @@ -373,14 +373,14 @@ public function provideThirdPartyStrategy() [ 'classMap' => ['third_party' => 'CakeDC\Enum\Test\TestCase\Model\Behavior\ThirdPartyStrategy'], 'lists' => [ - 'article_category' => ['strategy' => 'third_party'] - ] + 'article_category' => ['strategy' => 'third_party'], + ], ], [ 1 => 'PHP', - 2 => 'CSS' - ] - ] + 2 => 'CSS', + ], + ], ]; } diff --git a/tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php b/tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php index ad73a0e..d6e27eb 100644 --- a/tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php +++ b/tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php @@ -1,5 +1,5 @@ [ 'Published', 'Drafted', - 'Archived' + 'Archived', ], ]); $this->Strategy = new ConfigStrategy('status', new Table()); diff --git a/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php b/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php index 1c579ba..1cc8ebb 100644 --- a/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php +++ b/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php @@ -1,5 +1,5 @@ 'Translated Active', - 'Foo' => 'translated foo' + 'Foo' => 'translated foo', ]; $package->setMessages($messages); From d00bb76508c22c6c8c0895046cc72986d302a107 Mon Sep 17 00:00:00 2001 From: Yevgeny Tomenko Date: Fri, 3 May 2019 22:40:28 +0300 Subject: [PATCH 05/10] Fixing return type of setUp and tearDown methods according recent core changes --- .travis.yml | 2 +- tests/TestCase/Model/Behavior/EnumBehaviorTest.php | 4 ++-- tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php | 4 ++-- tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php | 4 ++-- tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index a6fcbff..cef3c1d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: php php: - - 7.1 - 7.2 + - 7.3 sudo: false diff --git a/tests/TestCase/Model/Behavior/EnumBehaviorTest.php b/tests/TestCase/Model/Behavior/EnumBehaviorTest.php index b010a14..fbba954 100644 --- a/tests/TestCase/Model/Behavior/EnumBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/EnumBehaviorTest.php @@ -67,7 +67,7 @@ class EnumBehaviorTest extends TestCase protected $Articles; - public function setUp() + public function setUp(): void { parent::setUp(); @@ -82,7 +82,7 @@ public function setUp() ]); } - public function tearDown() + public function tearDown(): void { parent::tearDown(); TableRegistry::clear(); diff --git a/tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php b/tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php index d6e27eb..2742e03 100644 --- a/tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php +++ b/tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php @@ -21,7 +21,7 @@ class ConfigStrategyTest extends TestCase { public $Strategy; - public function setUp() + public function setUp(): void { parent::setUp(); Configure::write(ConfigStrategy::KEY, [ @@ -35,7 +35,7 @@ public function setUp() $this->Strategy->initialize(['prefix' => 'STATUS']); } - public function tearDown() + public function tearDown(): void { parent::tearDown(); unset($this->Strategy); diff --git a/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php b/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php index 1cc8ebb..42a5594 100644 --- a/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php +++ b/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php @@ -41,7 +41,7 @@ class ConstStrategyTest extends TestCase public $StrategyEntity; - public function setUp() + public function setUp(): void { parent::setUp(); $this->StrategyTable = new ConstStrategy('status', new ArticlesTable()); @@ -55,7 +55,7 @@ public function setUp() ]); } - public function tearDown() + public function tearDown(): void { parent::tearDown(); unset($this->StrategyTable); diff --git a/tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php b/tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php index bc67728..7cfc0de 100644 --- a/tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php +++ b/tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php @@ -22,14 +22,14 @@ class LookupStrategyTest extends TestCase 'plugin.CakeDC/Enum.Lookups', ]; - public function setUp() + public function setUp(): void { parent::setUp(); $this->Strategy = new LookupStrategy('priority', new Table()); $this->Strategy->initialize(['prefix' => 'PRIORITY']); } - public function tearDown() + public function tearDown(): void { parent::tearDown(); unset($this->Strategy); From d68d6a92b90985fb7c7c0ae0b38e18b70cbf02fc Mon Sep 17 00:00:00 2001 From: Yevgeny Tomenko Date: Fri, 3 May 2019 22:45:14 +0300 Subject: [PATCH 06/10] set minimum php requirement --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index f19d747..4725f25 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ "source": "https://github.com/cakedc/enum" }, "require": { + "php": ">=7.2.0", "cakephp/cakephp": "4.x-dev as 4.0.0" }, "require-dev": { From f503bbd9811fa153ec7b75cff1950ed3ede99eb3 Mon Sep 17 00:00:00 2001 From: Marcelo Rocha Date: Wed, 6 May 2020 15:56:17 -0300 Subject: [PATCH 07/10] phpcs fixes --- src/Model/Behavior/EnumBehavior.php | 1 + .../Behavior/Exception/MissingEnumConfigurationException.php | 1 + src/Model/Behavior/Exception/MissingEnumStrategyException.php | 1 + .../Behavior/Exception/MissingEnumStrategyPrefixException.php | 1 + src/Model/Behavior/Strategy/AbstractStrategy.php | 1 + src/Model/Behavior/Strategy/ConfigStrategy.php | 1 + src/Model/Behavior/Strategy/ConstStrategy.php | 1 + src/Model/Behavior/Strategy/LookupStrategy.php | 1 + src/Model/Behavior/Strategy/StrategyInterface.php | 1 + src/Model/Table/LookupsTable.php | 1 + src/Plugin.php | 1 + tests/TestCase/Model/Behavior/EnumBehaviorTest.php | 3 +++ tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php | 1 + tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php | 4 +++- tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php | 1 + tests/bootstrap.php | 1 + 16 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Model/Behavior/EnumBehavior.php b/src/Model/Behavior/EnumBehavior.php index 764a91e..573665b 100644 --- a/src/Model/Behavior/EnumBehavior.php +++ b/src/Model/Behavior/EnumBehavior.php @@ -1,5 +1,6 @@ Date: Wed, 6 May 2020 15:59:05 -0300 Subject: [PATCH 08/10] using stable releases --- composer.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 4725f25..292c63c 100644 --- a/composer.json +++ b/composer.json @@ -15,12 +15,12 @@ }, "require": { "php": ">=7.2.0", - "cakephp/cakephp": "4.x-dev as 4.0.0" + "cakephp/cakephp": "^4.0.0" }, "require-dev": { - "phpunit/phpunit": "^7", + "phpunit/phpunit": "^8.5", "vlucas/phpdotenv": "^3.3", - "cakephp/cakephp-codesniffer": "dev-next" + "cakephp/cakephp-codesniffer": "~4.0.0" }, "autoload": { "psr-4": { @@ -45,8 +45,8 @@ "test": "phpunit --stderr", "stan": "phpstan analyse src/ && psalm --show-info=false", "psalm": "psalm --show-info=false", - "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.11 vimeo/psalm:^3.0 && mv composer.backup composer.json", - "rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:^0.4.11 && mv composer.backup composer.json", + "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.11 vimeo/psalm:^3.0 && mv composer.backup composer.json", + "rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:^0.4.11 && mv composer.backup composer.json", "coverage-test": "phpunit --stderr --coverage-clover=clover.xml" } } From c23578a67eaeea7e37a844f61524e23b282584d0 Mon Sep 17 00:00:00 2001 From: Marcelo Rocha Date: Thu, 7 May 2020 14:04:28 -0300 Subject: [PATCH 09/10] Updated config for travis --- .travis.yml | 19 +++++++++++++++---- composer.json | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index cef3c1d..a090ff9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,14 +1,25 @@ language: php +dist: xenial + php: - 7.2 - 7.3 sudo: false +services: + - postgresql + - mysql + +cache: + directories: + - vendor + - $HOME/.composer/cache + env: matrix: - - DB=mysql db_dsn='mysql://root@127.0.0.1/cakephp_test' + - DB=mysql db_dsn='mysql://root@127.0.0.1/cakephp_test?init[]=SET sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"' - DB=pgsql db_dsn='postgres://postgres@127.0.0.1/cakephp_test' - DB=sqlite db_dsn='sqlite:///:memory:' @@ -31,10 +42,10 @@ matrix: before_script: - if [[ $TRAVIS_PHP_VERSION != 7.3 ]]; then phpenv config-rm xdebug.ini; fi - composer install --prefer-dist --no-interaction - - if [ $DB = 'mysql' ]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi - - if [ $DB = 'pgsql' ]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi + - if [[ $DB == 'mysql' ]]; then mysql -u root -e 'CREATE DATABASE cakephp_test;'; fi + - if [[ $DB == 'pgsql' ]]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi - if [[ $PHPSTAN = 1 ]]; then composer stan-setup; fi - + script: - if [[ $DEFAULT = 1 ]]; then composer test; fi - if [[ $COVERAGE = 1 ]]; then composer coverage-test; fi diff --git a/composer.json b/composer.json index 292c63c..bbc7e8e 100644 --- a/composer.json +++ b/composer.json @@ -45,7 +45,7 @@ "test": "phpunit --stderr", "stan": "phpstan analyse src/ && psalm --show-info=false", "psalm": "psalm --show-info=false", - "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.11 vimeo/psalm:^3.0 && mv composer.backup composer.json", + "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.12.7 psalm/phar:~3.8.0 && mv composer.backup composer.json", "rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:^0.4.11 && mv composer.backup composer.json", "coverage-test": "phpunit --stderr --coverage-clover=clover.xml" } From 33735592ee70717e62356f7d591fdcc527b5cb23 Mon Sep 17 00:00:00 2001 From: Marcelo Rocha Date: Thu, 7 May 2020 14:17:03 -0300 Subject: [PATCH 10/10] Updated config for travis --- composer.json | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index bbc7e8e..3da75b3 100644 --- a/composer.json +++ b/composer.json @@ -35,16 +35,20 @@ "prefer-stable": true, "minimum-stability": "dev", "scripts": { + "analyse": [ + "@stan", + "@psalm" + ], "check": [ "@cs-check", "@test", - "@stan" + "@analyse" ], "cs-check": "phpcs -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/", "cs-fix": "phpcbf --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/", "test": "phpunit --stderr", - "stan": "phpstan analyse src/ && psalm --show-info=false", - "psalm": "psalm --show-info=false", + "stan": "phpstan analyse src/", + "psalm": "php vendor/psalm/phar/psalm.phar --show-info=false src/ ", "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.12.7 psalm/phar:~3.8.0 && mv composer.backup composer.json", "rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:^0.4.11 && mv composer.backup composer.json", "coverage-test": "phpunit --stderr --coverage-clover=clover.xml"