diff --git a/CHANGELOG.md b/CHANGELOG.md index b2ab7cc..2dcde51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ Changelog ========= +* 3.0.0-dev CakePHP 5.x support + * 2.0.0 CakePHP 4.x support * 1.2.0 diff --git a/Docs/Documentation/Installation.md b/Docs/Documentation/Installation.md index 3497bf4..0349a7e 100644 --- a/Docs/Documentation/Installation.md +++ b/Docs/Documentation/Installation.md @@ -3,7 +3,7 @@ ## Composer ``` -composer require cakedc/enum:dev-master +composer require cakedc/enum:3.0.0-dev ``` ## Creating Required Tables @@ -16,12 +16,8 @@ bin/cake migrations migrate -p CakeDC/Enum ## Load the Plugin -Ensure the Enum Plugin is loaded in your config/bootstrap.php file +Ensure the Enum Plugin is loaded in your `Application::bootstrap()` method: ```php -Plugin::load('CakeDC/Enum'); +$this->addPlugin('CakeDC/Enum'); ``` -or -```php -Plugin::loadAll(); -``` \ No newline at end of file diff --git a/Docs/Home.md b/Docs/Home.md index ebe6109..7d5d621 100644 --- a/Docs/Home.md +++ b/Docs/Home.md @@ -4,8 +4,8 @@ Home 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 5.0+ +* PHP 8.1+ Documentation ------------- diff --git a/README.md b/README.md index 5f22e2a..8ac371d 100644 --- a/README.md +++ b/README.md @@ -6,18 +6,19 @@ CakeDC Enum Plugin [![Total Downloads](https://img.shields.io/packagist/dt/cakedc/enum.svg?style=flat-square)](https://packagist.org/packages/cakedc/enum) [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE) -Enumeration list for [CakePHP 4](http://cakephp.org). +Enumeration list for [CakePHP 5](http://cakephp.org). Versions and branches --------------------- -| CakePHP | CakeDC Enum Plugin | Tag | Notes | -| :-------------: | :------------------------: | :--: | :---- | -| ^4.0 | [2.next](https://github.com/cakedc/enum/tree/2.next) | 2.0.4 | stable | -| ^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 | +| CakePHP | CakeDC Enum Plugin | Tag | Notes | +|:-----------|:--------------------------------------------------------|:----------|:---------| +| ^5.0.0-dev | [3.next](https://github.com/cakedc/enum/tree/3next) | 3.0.0-dev | unstable | +| ^4.0 | [2.next](https://github.com/cakedc/enum/tree/2.next) | 2.0.4 | stable | +| ^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 | Install @@ -26,7 +27,7 @@ Install Using [Composer](http://getcomposer.org): ``` -composer require cakedc/enum:2.0.0 +composer require cakedc/enum:3.0.0-dev ``` You then need to load the plugin. You can use the shell command: @@ -35,17 +36,17 @@ You then need to load the plugin. You can use the shell command: bin/cake plugin load CakeDC/Enum ``` -or by manually adding statement shown below to `bootstrap.php`: +or by manually adding statement shown below to `Application::bootstrap()` method: ```php -Plugin::load('CakeDC/Enum'); +$this->addPlugin('CakeDC/Enum'); ``` Requirements ------------ -* CakePHP 4.0+ -* PHP 7.3+ +* CakePHP 5.0+ +* PHP 8.1+ Documentation ------------- @@ -67,6 +68,6 @@ This repository follows the [CakeDC Plugin Standard](http://cakedc.com/plugin-st License ------- -Copyright 2015 - 2021 Cake Development Corporation (CakeDC). All rights reserved. +Copyright 2015 - 2023 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/composer.json b/composer.json index 8ec885b..0048be4 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "phpunit/phpunit": "^9.5.19", "cakephp/cakephp-codesniffer": "^5.0", "cakephp/bake": "3.x-dev", - "cakephp/migrations": "4.x-dev" + "cakephp/migrations": "4.x-dev", + "aura/intl": "^3.0" }, "autoload": { "psr-4": { diff --git a/config/Migrations/001_create_enum_lookups.php b/config/Migrations/001_create_enum_lookups.php index 2ffa2f1..49b4713 100644 --- a/config/Migrations/001_create_enum_lookups.php +++ b/config/Migrations/001_create_enum_lookups.php @@ -8,7 +8,7 @@ class CreateEnumLookups extends AbstractMigration * * @return void */ - public function change() + public function change(): void { $table = $this->table('enum_lookups'); diff --git a/src/Plugin.php b/src/EnumPlugin.php similarity index 54% rename from src/Plugin.php rename to src/EnumPlugin.php index b01c19f..6bd6e81 100644 --- a/src/Plugin.php +++ b/src/EnumPlugin.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -15,12 +15,7 @@ use Cake\Core\BasePlugin; -class Plugin extends BasePlugin +class EnumPlugin extends BasePlugin { - /** - * Plugin name. - * - * @var string - */ - protected $name = 'BootstrapUI'; + } diff --git a/src/Model/Behavior/EnumBehavior.php b/src/Model/Behavior/EnumBehavior.php index 983fc51..8206b50 100644 --- a/src/Model/Behavior/EnumBehavior.php +++ b/src/Model/Behavior/EnumBehavior.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -15,7 +15,8 @@ use ArrayObject; use BadMethodCallException; -use Cake\Event\EventInterface;use Cake\ORM\Behavior; +use Cake\Event\EventInterface; +use Cake\ORM\Behavior; use Cake\ORM\Query; use Cake\ORM\RulesChecker; use Cake\Utility\Hash; @@ -64,7 +65,7 @@ class EnumBehavior extends Behavior * * @var array */ - protected $_defaultConfig = [ + protected array $_defaultConfig = [ 'defaultStrategy' => 'lookup', 'translate' => false, 'translationDomain' => 'default', @@ -80,7 +81,7 @@ class EnumBehavior extends Behavior * * @var array */ - protected $_classMap = [ + protected array $classMap = [ 'lookup' => LookupStrategy::class, 'const' => ConstStrategy::class, 'config' => ConfigStrategy::class, @@ -91,7 +92,7 @@ class EnumBehavior extends Behavior * * @var array */ - protected $_strategies = []; + protected array $strategies = []; /** * Initializes the behavior. @@ -102,7 +103,7 @@ class EnumBehavior extends Behavior public function initialize(array $config): void { parent::initialize($config); - $this->_normalizeConfig(); + $this->normalizeConfig(); } /** @@ -113,28 +114,28 @@ public function initialize(array $config): void * @return \CakeDC\Enum\Model\Behavior\Strategy\StrategyInterface * @throws \CakeDC\Enum\Model\Behavior\Exception\MissingEnumStrategyException */ - public function strategy(string $alias, $strategy): \CakeDC\Enum\Model\Behavior\Strategy\StrategyInterface + public function strategy(string $alias, mixed $strategy): StrategyInterface { - if (!empty($this->_strategies[$alias])) { - return $this->_strategies[$alias]; + if (!empty($this->strategies[$alias])) { + return $this->strategies[$alias]; } - $this->_strategies[$alias] = $strategy; + $this->strategies[$alias] = $strategy; if ($strategy instanceof StrategyInterface) { return $strategy; } $class = null; - if (isset($this->_classMap[$strategy])) { - $class = $this->_classMap[$strategy]; + if (isset($this->classMap[$strategy])) { + $class = $this->classMap[$strategy]; } if ($class === null || !class_exists($class)) { throw new MissingEnumStrategyException([$class]); } - return $this->_strategies[$alias] = new $class($alias, $this->_table); + return $this->strategies[$alias] = new $class($alias, $this->_table); } /** @@ -142,10 +143,10 @@ public function strategy(string $alias, $strategy): \CakeDC\Enum\Model\Behavior\ * * @return void */ - protected function _normalizeConfig(): void + protected function normalizeConfig(): void { $classMap = $this->getConfig('classMap'); - $this->_classMap = array_merge($this->_classMap, $classMap); + $this->classMap = array_merge($this->classMap, $classMap); $lists = $this->getConfig('lists'); $defaultStrategy = $this->getConfig('defaultStrategy'); @@ -175,11 +176,11 @@ protected function _normalizeConfig(): void } /** - * @param string|array|null $alias Defined list's alias/name. + * @param array|string|null $alias Defined list's alias/name. * @return array * @throws \CakeDC\Enum\Model\Behavior\Exception\MissingEnumConfigurationException */ - public function enum($alias = null): array + public function enum(array|string|null $alias = null): array { if (is_string($alias)) { $config = $this->getConfig('lists.' . $alias); @@ -187,7 +188,7 @@ public function enum($alias = null): array throw new MissingEnumConfigurationException([$alias]); } - return $this->_enumList($alias, $config); + return $this->enumList($alias, $config); } $lists = $this->getConfig('lists'); @@ -197,7 +198,7 @@ public function enum($alias = null): array $return = []; foreach ($lists as $alias => $config) { - $return[$alias] = $this->_enumList($alias, $config); + $return[$alias] = $this->enumList($alias, $config); } return $return; @@ -208,11 +209,11 @@ public function enum($alias = null): array * @param array $config Config * @return array */ - protected function _enumList(string $alias, array $config): array + protected function enumList(string $alias, array $config): array { $return = $this->strategy($alias, $config['strategy'])->enum($config); if ($this->getConfig('translate')) { - $return = $this->_translate($return); + $return = $this->translate($return); } if ($this->getConfig('nested')) { @@ -235,7 +236,7 @@ function (&$item, $val) { * @param array $list List. * @return array */ - protected function _translate(array $list): array + protected function translate(array $list): array { $domain = $this->getConfig('translationDomain'); @@ -249,7 +250,7 @@ protected function _translate(array $list): array * @param \Cake\ORM\RulesChecker $rules Rules checker. * @return \Cake\ORM\RulesChecker */ - public function buildRules(EventInterface $event, RulesChecker $rules): \Cake\ORM\RulesChecker + public function buildRules(EventInterface $event, RulesChecker $rules): RulesChecker { foreach ($this->getConfig('lists') as $alias => $config) { if (Hash::get($config, 'applicationRules') === false) { @@ -277,7 +278,7 @@ public function buildRules(EventInterface $event, RulesChecker $rules): \Cake\OR */ public function __call(string $method, array $args): bool { - if (strpos($method, 'isValid') !== 0) { + if (!str_starts_with($method, 'isValid')) { throw new BadMethodCallException(sprintf('Call to undefined method (%s)', $method)); } @@ -302,7 +303,7 @@ public function __call(string $method, array $args): bool * @param \ArrayObject $options The options for the query * @return void */ - public function beforeFind(EventInterface $event, Query $query, ArrayObject $options) + public function beforeFind(EventInterface $event, Query $query, ArrayObject $options): void { foreach ($this->getConfig('lists') as $alias => $config) { $strategy = $this->strategy($alias, $config['strategy']); diff --git a/src/Model/Behavior/Exception/InvalidAliasListException.php b/src/Model/Behavior/Exception/InvalidAliasListException.php index bb32383..57374e3 100644 --- a/src/Model/Behavior/Exception/InvalidAliasListException.php +++ b/src/Model/Behavior/Exception/InvalidAliasListException.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -17,5 +17,5 @@ class InvalidAliasListException extends CakeException { - protected $_messageTemplate = 'Invalid alias %s. %s defined association %s.'; + protected string $_messageTemplate = 'Invalid alias %s. %s defined association %s.'; } diff --git a/src/Model/Behavior/Exception/MissingEnumConfigurationException.php b/src/Model/Behavior/Exception/MissingEnumConfigurationException.php index fb5e7dc..ae0810d 100644 --- a/src/Model/Behavior/Exception/MissingEnumConfigurationException.php +++ b/src/Model/Behavior/Exception/MissingEnumConfigurationException.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -17,5 +17,5 @@ class MissingEnumConfigurationException extends CakeException { - protected $_templateMessage = 'Missing enum configuration (%s)'; + protected string $_messageTemplate = 'Missing enum configuration (%s)'; } diff --git a/src/Model/Behavior/Exception/MissingEnumStrategyException.php b/src/Model/Behavior/Exception/MissingEnumStrategyException.php index 229720a..0e29578 100644 --- a/src/Model/Behavior/Exception/MissingEnumStrategyException.php +++ b/src/Model/Behavior/Exception/MissingEnumStrategyException.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -17,5 +17,5 @@ class MissingEnumStrategyException extends CakeException { - protected $_templateMessage = 'Missing enum strategy class (%s)'; + protected string $_messageTemplate = 'Missing enum strategy class (%s)'; } diff --git a/src/Model/Behavior/Exception/MissingEnumStrategyPrefixException.php b/src/Model/Behavior/Exception/MissingEnumStrategyPrefixException.php index 908d196..e539421 100644 --- a/src/Model/Behavior/Exception/MissingEnumStrategyPrefixException.php +++ b/src/Model/Behavior/Exception/MissingEnumStrategyPrefixException.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -17,5 +17,5 @@ class MissingEnumStrategyPrefixException extends CakeException { - protected $_messageTemplate = 'Missing prefix for strategy (%s)'; + protected string $_messageTemplate = 'Missing prefix for strategy (%s)'; } diff --git a/src/Model/Behavior/Strategy/AbstractStrategy.php b/src/Model/Behavior/Strategy/AbstractStrategy.php index d43dc30..3dae0f6 100644 --- a/src/Model/Behavior/Strategy/AbstractStrategy.php +++ b/src/Model/Behavior/Strategy/AbstractStrategy.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -26,21 +26,21 @@ abstract class AbstractStrategy implements StrategyInterface * * @var array */ - protected $_defaultConfig = []; + protected array $_defaultConfig = []; /** * Target table. * * @var \Cake\ORM\Table */ - protected $_table; + protected Table $table; /** * Table alias. * * @var string */ - protected $_alias; + protected string $alias; /** * Constructor. @@ -50,8 +50,8 @@ abstract class AbstractStrategy implements StrategyInterface */ public function __construct(string $alias, Table $table) { - $this->_alias = $alias; - $this->_table = $table; + $this->alias = $alias; + $this->table = $table; } /** @@ -61,11 +61,11 @@ public function initialize(array $config): void { $prefix = $this->getConfig('prefix'); if (empty($config['prefix']) && empty($prefix)) { - $config['prefix'] = $this->_generatePrefix(); + $config['prefix'] = $this->generatePrefix(); } if (empty($config['field'])) { - $config['field'] = Inflector::underscore($this->_alias); + $config['field'] = Inflector::underscore($this->alias); } if (empty($config['errorMessage'])) { @@ -83,10 +83,10 @@ public function initialize(array $config): void * * @return string */ - protected function _generatePrefix(): string + protected function generatePrefix(): string { - $prefix = Inflector::underscore(Inflector::singularize($this->_table->getAlias())); - $prefix .= '_' . $this->_alias; + $prefix = Inflector::underscore(Inflector::singularize($this->table->getAlias())); + $prefix .= '_' . $this->alias; return strtoupper($prefix); } diff --git a/src/Model/Behavior/Strategy/ConfigStrategy.php b/src/Model/Behavior/Strategy/ConfigStrategy.php index 6c2fb89..1055dc3 100644 --- a/src/Model/Behavior/Strategy/ConfigStrategy.php +++ b/src/Model/Behavior/Strategy/ConfigStrategy.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ diff --git a/src/Model/Behavior/Strategy/ConstStrategy.php b/src/Model/Behavior/Strategy/ConstStrategy.php index 2938796..6bd80fe 100644 --- a/src/Model/Behavior/Strategy/ConstStrategy.php +++ b/src/Model/Behavior/Strategy/ConstStrategy.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -31,7 +31,7 @@ class ConstStrategy extends AbstractStrategy * * @var array */ - protected $_constants; + protected array $constants; /** * {@inheritDoc} @@ -51,10 +51,11 @@ public function __construct(string $alias, Table $table) * * @param array $config List of callable filters to limit items generated from list. * @return array + * @throws \ReflectionException */ public function enum(array $config = []): array { - $constants = $this->_getConstants(); + $constants = $this->getConstants(); $keys = array_keys($constants); foreach ($config as $callable) { @@ -74,22 +75,23 @@ public function enum(array $config = []): array * Returns defined constants for the current `$_table`. * * @return array + * @throws \ReflectionException */ - protected function _getConstants(): array + protected function getConstants(): array { - if ($this->_constants !== null) { - return $this->_constants; + if ($this->constants !== null) { + return $this->constants; } $prefix = $this->getConfig('prefix'); $lowercase = $this->getConfig('lowercase'); - $className = $this->getConfig('className') ?: get_class($this->_table); + $className = $this->getConfig('className') ?: get_class($this->table); $length = strlen($prefix) + 1; $classConstants = (new ReflectionClass($className))->getConstants(); $constants = []; foreach ($classConstants as $key => $value) { - if (strpos($key, (string)$prefix) === 0) { + if (str_starts_with($key, (string)$prefix)) { $listKey = substr($key, $length); if ($lowercase) { $listKey = strtolower($listKey); @@ -98,7 +100,7 @@ protected function _getConstants(): array } } - return $this->_constants = $constants; + return $this->constants = $constants; } /** @@ -107,16 +109,18 @@ protected function _getConstants(): array * @param \ArrayObject $options The options for the query * @return void */ - public function beforeFind(EventInterface $event, Query $query, ArrayObject $options) + public function beforeFind(EventInterface $event, Query $query, ArrayObject $options): void { - $assocName = Inflector::pluralize(Inflector::classify($this->_alias)); - if ($this->_table->hasAssociation($assocName)) { - throw new InvalidAliasListException([$this->_alias, $this->_table->getAlias(), $assocName]); + $assocName = Inflector::pluralize(Inflector::classify($this->alias)); + if ($this->table->hasAssociation($assocName)) { + throw new InvalidAliasListException([$this->alias, $this->table->getAlias(), $assocName]); } - $contain = array_filter($query->getContain(), function ($value) use ($assocName) { - return $value !== $assocName; - }, ARRAY_FILTER_USE_KEY); + $contain = array_filter( + $query->getContain(), + fn($value): bool => $value !== $assocName, + ARRAY_FILTER_USE_KEY + ); $query->clearContain()->contain($contain); @@ -128,9 +132,9 @@ public function beforeFind(EventInterface $event, Query $query, ArrayObject $opt $constant = Hash::get($row, $this->getConfig('field')); - $field = Inflector::singularize(Inflector::underscore($this->_alias)); + $field = Inflector::singularize(Inflector::underscore($this->alias)); $value = new Entity([ - 'label' => Hash::get($this->_getConstants(), $constant, $constant), + 'label' => Hash::get($this->getConstants(), $constant, $constant), 'prefix' => $this->getConfig('prefix'), 'value' => $constant, ], ['markClean' => true, 'markNew' => false]); diff --git a/src/Model/Behavior/Strategy/LookupStrategy.php b/src/Model/Behavior/Strategy/LookupStrategy.php index 7c5f4be..4055f83 100644 --- a/src/Model/Behavior/Strategy/LookupStrategy.php +++ b/src/Model/Behavior/Strategy/LookupStrategy.php @@ -56,9 +56,9 @@ public function initialize(array $config): void { parent::initialize($config); $config = $this->getConfig(); - $assocName = Inflector::pluralize(Inflector::classify($this->_alias)); + $assocName = Inflector::pluralize(Inflector::classify($this->alias)); - $this->_table + $this->table ->belongsTo($assocName) ->setClassName('CakeDC/Enum.Lookups') ->setForeignKey($config['field']) diff --git a/src/Model/Behavior/Strategy/StrategyInterface.php b/src/Model/Behavior/Strategy/StrategyInterface.php index 44d50da..364ac53 100644 --- a/src/Model/Behavior/Strategy/StrategyInterface.php +++ b/src/Model/Behavior/Strategy/StrategyInterface.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -60,5 +60,5 @@ public function initialize(array $config): void; * @param mixed $default The return value when the key does not exist. * @return mixed Config value being read. */ - public function getConfig(?string $key = null, $default = null); + public function getConfig(?string $key = null, mixed $default = null): mixed; } diff --git a/src/Model/Table/LookupsTable.php b/src/Model/Table/LookupsTable.php index 4f7cb07..f968657 100644 --- a/src/Model/Table/LookupsTable.php +++ b/src/Model/Table/LookupsTable.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ diff --git a/tests/Fixture/ArticlesFixture.php b/tests/Fixture/ArticlesFixture.php index ec0bd78..6140f70 100644 --- a/tests/Fixture/ArticlesFixture.php +++ b/tests/Fixture/ArticlesFixture.php @@ -1,12 +1,13 @@ ['type' => 'integer'], 'title' => ['type' => 'string'], 'body' => ['type' => 'text'], @@ -28,7 +29,7 @@ class ArticlesFixture extends TestFixture '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]], ]; - public $records = [ + public array $records = [ [ 'title' => 'Dummy article', 'body' => '', diff --git a/tests/Fixture/LookupsFixture.php b/tests/Fixture/LookupsFixture.php index 6de7a05..75cdabb 100644 --- a/tests/Fixture/LookupsFixture.php +++ b/tests/Fixture/LookupsFixture.php @@ -1,12 +1,13 @@ ['type' => 'integer'], 'label' => ['type' => 'string'], 'prefix' => ['type' => 'string'], @@ -26,7 +27,7 @@ class LookupsFixture extends TestFixture '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]], ]; - public $records = [ + public array $records = [ [ 'label' => 'Urgent', 'prefix' => 'PRIORITY', diff --git a/tests/TestCase/Model/Behavior/EnumBehaviorTest.php b/tests/TestCase/Model/Behavior/EnumBehaviorTest.php index d7b4abf..09b3203 100644 --- a/tests/TestCase/Model/Behavior/EnumBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/EnumBehaviorTest.php @@ -2,20 +2,21 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ namespace CakeDC\Enum\Test\TestCase\Model\Behavior; use Cake\Core\Configure; +use Cake\ORM\Association\BelongsTo; +use Cake\ORM\Entity; use Cake\ORM\Table; -use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; use CakeDC\Enum\Model\Behavior\Strategy\AbstractStrategy; @@ -70,12 +71,12 @@ public function enum(array $config = []): array class EnumBehaviorTest extends TestCase // @codingStandardsIgnoreEnd { - public $fixtures = [ + public array $fixtures = [ 'plugin.CakeDC/Enum.Articles', 'plugin.CakeDC/Enum.Lookups', ]; - protected $Articles; + protected Table $Articles; public function setUp(): void { @@ -86,8 +87,8 @@ public function setUp(): void 'Open Source Software', ]); - $this->Articles = TableRegistry::get('CakeDC/Enum.Articles', [ - 'className' => \CakeDC\Enum\Test\TestCase\Model\Behavior\ArticlesTable::class, + $this->Articles = $this->getTableLocator()->get('CakeDC/Enum.Articles', [ + 'className' => ArticlesTable::class, 'table' => 'enum_articles', ]); } @@ -95,10 +96,10 @@ public function setUp(): void public function tearDown(): void { parent::tearDown(); - TableRegistry::clear(); + $this->getTableLocator()->clear(); } - public function provideBasicConfiguration() + public function provideBasicConfiguration(): array { $expected = [ 'defaultStrategy' => 'lookup', @@ -187,14 +188,14 @@ public function provideBasicConfiguration() */ public function testBasicConfiguration(array $config, array $expected) { - TableRegistry::clear(); - $Articles = TableRegistry::get('CakeDC/Enum.Articles', ['table' => 'enum_articles']); + $this->getTableLocator()->clear(); + $Articles = $this->getTableLocator()->get('CakeDC/Enum.Articles', ['table' => 'enum_articles']); $Articles->addBehavior('CakeDC/Enum.Enum', $config); $result = $Articles->behaviors()->Enum->getConfig(); $this->assertEquals($expected, $result); } - public function provideBasicLookups() + public function provideBasicLookups(): array { return [ [ @@ -251,7 +252,7 @@ public function testBasicLookups($group, $expected) $this->assertEquals($expected, $result); } - public function provideBuildRules() + public function provideBuildRules(): array { return [ [ @@ -294,7 +295,7 @@ public function provideBuildRules() */ public function testBuildRules($data, $expected) { - $article = new \Cake\ORM\Entity($data); + $article = new Entity($data); $this->Articles->save($article); $result = $article->getErrors(); $this->assertEquals($expected, $result); @@ -319,7 +320,7 @@ public function testAssociationsCreated() $this->assertEquals($expected, $result); foreach ($result as $assoc) { - $this->assertInstanceOf(\Cake\ORM\Association\BelongsTo::class, $this->Articles->getAssociation($assoc)); + $this->assertInstanceOf(BelongsTo::class, $this->Articles->getAssociation($assoc)); } $result = $this->Articles->get(1); @@ -399,12 +400,12 @@ public function testTranslatedValues() $this->assertEquals($expected, $result); } - public function provideThirdPartyStrategy() + public function provideThirdPartyStrategy(): array { return [ [ [ - 'classMap' => ['third_party' => \CakeDC\Enum\Test\TestCase\Model\Behavior\ThirdPartyStrategy::class], + 'classMap' => ['third_party' => ThirdPartyStrategy::class], 'lists' => [ 'article_category' => ['strategy' => 'third_party'], ], @@ -422,8 +423,8 @@ public function provideThirdPartyStrategy() */ public function testThirdPartyStrategy(array $config, array $expected) { - TableRegistry::clear(); - $Articles = TableRegistry::get('CakeDC/Enum.Articles', ['table' => 'enum_articles']); + $this->getTableLocator()->clear(); + $Articles = $this->getTableLocator()->get('CakeDC/Enum.Articles', ['table' => 'enum_articles']); $Articles->addBehavior('CakeDC/Enum.Enum', $config); $result = $Articles->enum('article_category'); $this->assertEquals($expected, $result); diff --git a/tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php b/tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php index e0dee36..5526323 100644 --- a/tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php +++ b/tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -20,7 +20,7 @@ class ConfigStrategyTest extends TestCase { - public $Strategy; + public ConfigStrategy $Strategy; public function setUp(): void { diff --git a/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php b/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php index a24cedd..e672258 100644 --- a/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php +++ b/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -39,9 +39,9 @@ class Article extends Entity class ConstStrategyTest extends TestCase // @codingStandardsIgnoreEnd { - public $StrategyTable; + public ConstStrategy $StrategyTable; - public $StrategyEntity; + public ConstStrategy $StrategyEntity; public function setUp(): void { @@ -64,6 +64,9 @@ public function tearDown(): void unset($this->StrategyEntity); } + /** + * @throws \ReflectionException + */ public function testEnumTable() { $result = $this->StrategyTable->enum(); @@ -78,6 +81,9 @@ public function testEnumTable() $this->assertEquals($expected, $this->StrategyTable->enum()); } + /** + * @throws \ReflectionException + */ public function testEnumEntity() { $result = $this->StrategyEntity->enum(); diff --git a/tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php b/tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php index f0c214a..08b3ef6 100644 --- a/tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php +++ b/tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -22,8 +22,9 @@ class LookupStrategyTest extends TestCase /** * @var \CakeDC\Enum\Model\Behavior\Strategy\LookupStrategy|mixed */ - public $Strategy; - public $fixtures = [ + public mixed $Strategy; + + public array $fixtures = [ 'plugin.CakeDC/Enum.Lookups', ]; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 903a1a7..e7f7571 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,18 +2,23 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ use Aura\Intl\Package; +use Cake\Core\Plugin; use Cake\I18n\I18n; +use CakeDC\Enum\EnumPlugin; +/** + * @throws \Exception + */ $findRoot = function ($root) { do { $lastRoot = $root; @@ -35,7 +40,7 @@ require $root . '/vendor/cakephp/cakephp/tests/bootstrap.php'; -\Cake\Core\Plugin::getCollection()->add(new \CakeDC\Enum\Plugin(['path' => dirname(__FILE__, 2) . DS])); +Plugin::getCollection()->add(new EnumPlugin(['path' => dirname(__FILE__, 2) . DS])); I18n::config('default', function ($name, $locale) { $package = new Package('default'); $messages = [