diff --git a/CONTRIBUTING.md b/.github/CONTRIBUTING.md similarity index 73% rename from CONTRIBUTING.md rename to .github/CONTRIBUTING.md index 1c08c0b..667d124 100644 --- a/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -5,6 +5,6 @@ * Screenshots and code exemple are welcome in the issues. * You want to implement a new feature or fix a bug ? Please follow this guide : - * Your code **must follow** the [Coding Standard of CakePHP](http://book.cakephp.org/3.0/en/contributing/cakephp-coding-conventions.html). Check the [cakephp-codesniffer](https://github.com/cakephp/cakephp-codesniffer) repository to setup the CakePHP standard. + * Your code **must follow** the [PSR2 Coding Standard](http://www.php-fig.org/psr/psr-2/). Check the [squizlabs/php_codesniffer](https://github.com/squizlabs/php_codesniffer) repository to setup the codesniffer. * You must **add Test Cases** for your new feature. Test Cases ensure that the application will continue to working in the future. * Your PR should be on the `master` branch. The master branch is actually the dev branch. diff --git a/.travis.yml b/.travis.yml index 1d655a2..d85a592 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,7 @@ before_script: - composer install --prefer-dist --no-interaction --dev script: - - sh -c "if [ '$PHPCS' = '1' ]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src; fi" + - sh -c "if [ '$PHPCS' = '1' ]; then vendor/bin/phpcs -p --extensions=php --standard=PSR2 ./src; fi" notifications: email: false diff --git a/README.md b/README.md index c77dc7d..8c12042 100644 --- a/README.md +++ b/README.md @@ -155,7 +155,7 @@ This module has the following command and it require to be admin of the bot by d |`!module loaded`|Show the list of the loaded modules.|E.g `Modules loaded : Basic, Module, Developer.`| # Contribute -[Follow this guide to contribute](https://github.com/Xety/Skinny/blob/master/CONTRIBUTING.md) +[Follow this guide to contribute](https://github.com/Xety/Skinny/blob/master/.github/CONTRIBUTING.md) # Ressources * [CakePHP](https://github.com/cakephp/cakephp) diff --git a/VERSION.txt b/VERSION.txt index 8acdd82..4e379d2 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1 +1 @@ -0.0.1 +0.0.2 diff --git a/composer.json b/composer.json index caaedf5..a2fa82e 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ }, "require-dev": { "phpunit/phpunit": "*", - "cakephp/cakephp-codesniffer": "~2.0" + "squizlabs/php_codesniffer": "3.0.x-dev" }, "autoload": { "psr-4": { diff --git a/src/Configure/Configure.php b/src/Configure/Configure.php index 0d826d6..09aaa8d 100644 --- a/src/Configure/Configure.php +++ b/src/Configure/Configure.php @@ -19,7 +19,7 @@ class Configure * * @var array */ - protected static $_values = [ + protected static $values = [ 'debug' => 0 ]; @@ -28,7 +28,7 @@ class Configure * * @return void */ - protected static $_hasIniSet = null; + protected static $hasIniSet = null; /** * Used to store a dynamic variable in Configure. @@ -60,13 +60,13 @@ public static function write($config, $value = null) $config = [$config => $value]; } foreach ($config as $name => $value) { - static::$_values = Hash::insert(static::$_values, $name, $value); + static::$values = Hash::insert(static::$values, $name, $value); } if (isset($config['debug'])) { - if (static::$_hasIniSet === null) { - static::$_hasIniSet = function_exists('ini_set'); + if (static::$hasIniSet === null) { + static::$hasIniSet = function_exists('ini_set'); } - if (static::$_hasIniSet) { + if (static::$hasIniSet) { ini_set('display_errors', $config['debug'] ? 1 : 0); } } @@ -91,10 +91,10 @@ public static function write($config, $value = null) public static function read($var = null) { if ($var === null) { - return static::$_values; + return static::$values; } - return Hash::get(static::$_values, $var); + return Hash::get(static::$values, $var); } /** @@ -110,7 +110,7 @@ public static function check($var) return false; } - return Hash::get(static::$_values, $var) !== null; + return Hash::get(static::$values, $var) !== null; } /** @@ -128,7 +128,7 @@ public static function check($var) */ public static function delete($var) { - static::$_values = Hash::remove(static::$_values, $var); + static::$values = Hash::remove(static::$values, $var); } /** @@ -144,17 +144,17 @@ public static function delete($var) public static function consume($var) { $simple = strpos($var, '.') === false; - if ($simple && !isset(static::$_values[$var])) { + if ($simple && !isset(static::$values[$var])) { return null; } if ($simple) { - $value = static::$_values[$var]; - unset(static::$_values[$var]); + $value = static::$values[$var]; + unset(static::$values[$var]); return $value; } - $value = Hash::get(static::$_values, $var); - static::$_values = Hash::remove(static::$_values, $var); + $value = Hash::get(static::$values, $var); + static::$_values = Hash::remove(static::$values, $var); return $value; } @@ -181,7 +181,7 @@ public static function consume($var) */ public static function load($key, $merge = true) { - $file = static::_getFilePath($key); + $file = static::getFilePath($key); $return = include $file; if (!is_array($return)) { @@ -203,7 +203,7 @@ public static function load($key, $merge = true) * * @return string */ - protected static function _getFilePath($key) + protected static function getFilePath($key) { return CONFIG . $key . static::EXT; } @@ -231,7 +231,7 @@ protected static function _getFilePath($key) */ public static function dump($key, $keys = []) { - $values = static::$_values; + $values = static::$values; if (!empty($keys) && is_array($keys)) { $values = array_intersect_key($values, array_flip($keys)); @@ -252,12 +252,12 @@ public static function dump($key, $keys = []) */ public static function version() { - if (!isset(static::$_values['Bot']['version'])) { + if (!isset(static::$values['Bot']['version'])) { require ROOT . DS . 'config' . DS . 'version.php'; static::write($config); } - return static::$_values['Bot']['version']; + return static::$values['Bot']['version']; } /** @@ -267,7 +267,7 @@ public static function version() */ public static function clear() { - static::$_values = []; + static::$values = []; return true; } diff --git a/src/Configure/Configure/Exception/Exception.php b/src/Configure/Configure/Exception/Exception.php index 40775c0..e454e9b 100644 --- a/src/Configure/Configure/Exception/Exception.php +++ b/src/Configure/Configure/Exception/Exception.php @@ -9,21 +9,21 @@ class Exception extends \RuntimeException * * @var array */ - protected $_attributes = []; + protected $attributes = []; /** * Template string that has attributes sprintf()'ed into it. * * @var string */ - protected $_messageTemplate = ''; + protected $messageTemplate = ''; /** * Array of headers to be passed to \Bot\Network\Response::header() * * @var array */ - protected $_responseHeaders = null; + protected $responseHeaders = null; /** * Constructor. @@ -39,8 +39,8 @@ class Exception extends \RuntimeException public function __construct($message, $code = 500, $previous = null) { if (is_array($message)) { - $this->_attributes = $message; - $message = vsprintf($this->_messageTemplate, $message); + $this->attributes = $message; + $message = vsprintf($this->messageTemplate, $message); } parent::__construct($message, $code, $previous); } @@ -52,6 +52,6 @@ public function __construct($message, $code = 500, $previous = null) */ public function getAttributes() { - return $this->_attributes; + return $this->attributes; } } diff --git a/src/Configure/InstanceConfigTrait.php b/src/Configure/InstanceConfigTrait.php index a25895f..fba14b4 100644 --- a/src/Configure/InstanceConfigTrait.php +++ b/src/Configure/InstanceConfigTrait.php @@ -16,14 +16,14 @@ trait InstanceConfigTrait * * @var array */ - protected $_config = []; + protected $config = []; /** * Whether the config property has already been configured with defaults. * * @var bool */ - protected $_configInitialized = false; + protected $configInitialized = false; /** * ### Usage @@ -62,18 +62,18 @@ trait InstanceConfigTrait */ public function config($key = null, $value = null, $merge = true) { - if (!$this->_configInitialized) { - $this->_config = $this->_defaultConfig; - $this->_configInitialized = true; + if (!$this->configInitialized) { + $this->config = $this->defaultConfig; + $this->configInitialized = true; } if (is_array($key) || func_num_args() >= 2) { - $this->_configWrite($key, $value, $merge); + $this->configWrite($key, $value, $merge); return $this; } - return $this->_configRead($key); + return $this->configRead($key); } /** @@ -98,12 +98,12 @@ public function config($key = null, $value = null, $merge = true) */ public function configShallow($key, $value = null) { - if (!$this->_configInitialized) { - $this->_config = $this->_defaultConfig; - $this->_configInitialized = true; + if (!$this->configInitialized) { + $this->config = $this->defaultConfig; + $this->configInitialized = true; } - $this->_configWrite($key, $value, 'shallow'); + $this->configWrite($key, $value, 'shallow'); return $this; } @@ -114,17 +114,17 @@ public function configShallow($key, $value = null) * @param string|null $key Key to read. * @return mixed */ - protected function _configRead($key) + protected function configRead($key) { if ($key === null) { - return $this->_config; + return $this->config; } if (strpos($key, '.') === false) { - return isset($this->_config[$key]) ? $this->_config[$key] : null; + return isset($this->config[$key]) ? $this->config[$key] : null; } - $return = $this->_config; + $return = $this->config; foreach (explode('.', $key) as $k) { if (!is_array($return) || !isset($return[$k])) { @@ -149,10 +149,10 @@ protected function _configRead($key) * * @throws \Skinny\Configure\Configure\Exception\Exception if attempting to clobber existing config */ - protected function _configWrite($key, $value, $merge = false) + protected function configWrite($key, $value, $merge = false) { if (is_string($key) && $value === null) { - $this->_configDelete($key); + $this->configDelete($key); return; } @@ -164,9 +164,9 @@ protected function _configWrite($key, $value, $merge = false) $update = [$key => $value]; } if ($merge === 'shallow') { - $this->_config = array_merge($this->_config, Hash::expand($update)); + $this->config = array_merge($this->config, Hash::expand($update)); } else { - $this->_config = Hash::merge($this->_config, Hash::expand($update)); + $this->config = Hash::merge($this->config, Hash::expand($update)); } return; @@ -174,19 +174,19 @@ protected function _configWrite($key, $value, $merge = false) if (is_array($key)) { foreach ($key as $k => $val) { - $this->_configWrite($k, $val); + $this->configWrite($k, $val); } return; } if (strpos($key, '.') === false) { - $this->_config[$key] = $value; + $this->config[$key] = $value; return; } - $update =& $this->_config; + $update =& $this->config; $stack = explode('.', $key); foreach ($stack as $k) { @@ -212,15 +212,15 @@ protected function _configWrite($key, $value, $merge = false) * * @throws \Skinny\Configure\Configure\Exception\Exception if attempting to clobber existing config */ - protected function _configDelete($key) + protected function configDelete($key) { if (strpos($key, '.') === false) { - unset($this->_config[$key]); + unset($this->config[$key]); return; } - $update =& $this->_config; + $update =& $this->config; $stack = explode('.', $key); $length = count($stack); diff --git a/src/Error/Debugger.php b/src/Error/Debugger.php index 61adad9..ca987ef 100644 --- a/src/Error/Debugger.php +++ b/src/Error/Debugger.php @@ -17,21 +17,21 @@ class Debugger * * @var string */ - protected $_outputFormat = 'js'; + protected $outputFormat = 'js'; /** * Holds current output data when outputFormat is false. * * @var string */ - protected $_data = []; + protected $data = []; /** * Template used to generate the debug messages. * * @var array */ - protected $_templates = [ + protected $templates = [ 'log' => [ 'trace' => '{:reference} - {:path}, line {:line}', 'error' => "{:error} ({:code}): {:description} in [{:file}, line {:line}]" @@ -71,11 +71,13 @@ class Debugger public static function getInstance($class = null) { static $instance = []; + //@codingStandardsIgnoreStart if (!empty($class)) { if (!$instance || strtolower($class) != strtolower(get_class($instance[0]))) { $instance[0] = new $class(); } } + //@codingStandardsIgnoreEnd if (!$instance) { $instance[0] = new Debugger(); } @@ -155,10 +157,10 @@ public static function trace(array $options = []) } elseif ($options['format'] === 'array') { $back[] = $trace; } else { - if (isset($self->_templates[$options['format']]['traceLine'])) { - $tpl = $self->_templates[$options['format']]['traceLine']; + if (isset($self->templates[$options['format']]['traceLine'])) { + $tpl = $self->templates[$options['format']]['traceLine']; } else { - $tpl = $self->_templates['base']['traceLine']; + $tpl = $self->templates['base']['traceLine']; } $trace['path'] = static::trimPath($trace['file']); @@ -200,7 +202,7 @@ public static function trace(array $options = []) */ public static function exportVar($var, $depth = 3) { - return static::_export($var, $depth, 0); + return static::export($var, $depth, 0); } /** @@ -212,7 +214,7 @@ public static function exportVar($var, $depth = 3) * * @return string The dumped variable. */ - protected static function _export($var, $depth, $indent) + protected static function export($var, $depth, $indent) { switch (static::getType($var)) { case 'boolean': @@ -228,7 +230,7 @@ protected static function _export($var, $depth, $indent) return "'" . $var . "'"; case 'array': - return static::_array($var, $depth - 1, $indent + 1); + return static::array($var, $depth - 1, $indent + 1); case 'resource': return strtolower(gettype($var)); case 'null': @@ -236,7 +238,7 @@ protected static function _export($var, $depth, $indent) case 'unknown': return 'unknown'; default: - return static::_object($var, $depth - 1, $indent + 1); + return static::object($var, $depth - 1, $indent + 1); } } @@ -249,7 +251,7 @@ protected static function _export($var, $depth, $indent) * * @return string */ - protected static function _object($var, $depth, $indent) + protected static function object($var, $depth, $indent) { $out = ''; $props = []; @@ -262,7 +264,7 @@ protected static function _object($var, $depth, $indent) if ($depth > 0 && method_exists($var, '__debugInfo')) { try { return $out . "\n" . - substr(static::_array($var->__debugInfo(), $depth - 1, $indent), 1, -1) . + substr(static::array($var->__debugInfo(), $depth - 1, $indent), 1, -1) . $end . '}'; } catch (Exception $e) { return $out . "\n(unable to export object)\n }"; @@ -272,7 +274,7 @@ protected static function _object($var, $depth, $indent) if ($depth > 0) { $objectVars = get_object_vars($var); foreach ($objectVars as $key => $value) { - $value = static::_export($value, $depth - 1, $indent); + $value = static::export($value, $depth - 1, $indent); $props[] = "$key => " . $value; } @@ -285,10 +287,11 @@ protected static function _object($var, $depth, $indent) foreach ($filters as $filter => $visibility) { $reflectionProperties = $ref->getProperties($filter); + foreach ($reflectionProperties as $reflectionProperty) { $reflectionProperty->setAccessible(true); $property = $reflectionProperty->getValue($var); - $value = static::_export($property, $depth - 1, $indent); + $value = static::export($property, $depth - 1, $indent); $key = $reflectionProperty->name; $props[] = sprintf('[%s] %s => %s', $visibility, $key, $value); } @@ -320,7 +323,7 @@ protected static function _object($var, $depth, $indent) * * @return string Exported array. */ - protected static function _array(array $var, $depth, $indent) + protected static function array(array $var, $depth, $indent) { $out = "["; $break = $end = null; @@ -335,7 +338,7 @@ protected static function _array(array $var, $depth, $indent) if ($key === 'GLOBALS' && is_array($val) && isset($val['GLOBALS'])) { $val = '[recursion]'; } elseif ($val !== $var) { - $val = static::_export($val, $depth, $indent); + $val = static::export($val, $depth, $indent); } $vars[] = $break . static::exportVar($key) . ' => ' . diff --git a/src/Message/Message.php b/src/Message/Message.php index 3236ce4..3a14f4d 100644 --- a/src/Message/Message.php +++ b/src/Message/Message.php @@ -10,7 +10,7 @@ class Message extends Singleton * * @var array */ - protected static $_defaultConfig = [ + protected static $defaultConfig = [ 'raw' => '', 'parts' => [], 'command' => '', @@ -36,7 +36,7 @@ class Message extends Singleton public static function setData($message) { //Set the default values before processing. - static::_setDefaultValues(); + static::setDefaultValues(); //Check if we have a Message. if (!empty($message)) { @@ -98,8 +98,8 @@ public static function setValues($values = []) * * @return void */ - protected static function _setDefaultValues() + protected static function setDefaultValues() { - static::$data = static::$_defaultConfig; + static::$data = static::$defaultConfig; } } diff --git a/src/Module/ModuleManager.php b/src/Module/ModuleManager.php index 12731f4..58a99cc 100644 --- a/src/Module/ModuleManager.php +++ b/src/Module/ModuleManager.php @@ -23,21 +23,21 @@ class ModuleManager implements ArrayAccess, Countable * * @var array */ - protected $_priorityList = []; + protected $priorityList = []; /** * Loaded modules. * * @var array */ - protected $_loadedModules = []; + protected $loadedModules = []; /** * An argument that should be passed every call. * * @var array */ - protected $_prefixArgument = null; + protected $prefixArgument = null; /** * Constructor, loads all Modules in the Module directory. @@ -46,7 +46,7 @@ class ModuleManager implements ArrayAccess, Countable */ public function __construct(array $priorities = []) { - $this->_priorityList = $priorities; + $this->priorityList = $priorities; $files = new DirectoryIterator(MODULE_DIR); foreach ($files as $file) { @@ -72,7 +72,7 @@ public function __construct(array $priorities = []) */ public function __destruct() { - $this->_loadedModules = []; + $this->loadedModules = []; } /** @@ -84,7 +84,7 @@ public function __destruct() */ public function addPrefixArgument($argument) { - $this->_prefixArgument = $argument; + $this->prefixArgument = $argument; return true; } @@ -102,11 +102,11 @@ public function addPrefixArgument($argument) public function __call($method, array $arguments) { //Add out predefined prefix argument to the total list. - if (!is_null($this->_prefixArgument)) { - array_unshift($arguments, $this->_prefixArgument); + if (!is_null($this->prefixArgument)) { + array_unshift($arguments, $this->prefixArgument); } - foreach ($this->_loadedModules as $module) { + foreach ($this->loadedModules as $module) { //Check if the module has the method. if (!method_exists($module['object'], $method)) { continue; @@ -134,7 +134,7 @@ public function load($module) { $module = Inflector::camelize($module); - if (isset($this->_loadedModules[$module])) { + if (isset($this->loadedModules[$module])) { //Return the message AlreadyLoaded. return 'AL'; } elseif (!file_exists(MODULE_DIR . DS . $module . '.php')) { @@ -149,8 +149,8 @@ public function load($module) if (Configure::read('debug') === false) { require_once $path; } else { - //Here, we load the file's contents first, then use preg_replace() to replace the original class-name with a random one. - //After that, we create a copy and include it. + //Here, we load the file's contents first, then use preg_replace() to replace + //the original class-name with a random one. After that, we create a copy and include it. $newClass = $module . '_' . md5(mt_rand() . time()); $contents = preg_replace( "/(class[\s]+?)" . $module . "([\s]+?implements[\s]+?ModuleInterface[\s]+?{)/", @@ -179,18 +179,20 @@ public function load($module) //Check if this module implements our default interface. if (!$objectModule instanceof ModuleInterface) { - throw new RuntimeException(sprintf('ModuleManager::load() expects "%s" to be an instance of ModuleInterface.', $className)); + throw new RuntimeException( + sprintf('ModuleManager::load() expects "%s" to be an instance of ModuleInterface.', $className) + ); } //Prioritize. - if (in_array($module, $this->_priorityList)) { + if (in_array($module, $this->priorityList)) { //So, here we reverse our list of loaded modules, so that prioritized modules will be the last ones, //then, we add the current prioritized modules to the array and reverse it again. - $temp = array_reverse($this->_loadedModules, true); + $temp = array_reverse($this->loadedModules, true); $temp[$module] = $new; - $this->_loadedModules = array_reverse($temp, true); + $this->loadedModules = array_reverse($temp, true); } else { - $this->_loadedModules[$module] = $new; + $this->loadedModules[$module] = $new; } //Return the message Loaded. @@ -208,15 +210,15 @@ public function unload($module) { $module = Inflector::camelize($module); - if (!isset($this->_loadedModules[$module])) { + if (!isset($this->loadedModules[$module])) { //Return the message AlreadyUnloaded. return 'AU'; } //Remove this module, also calling the __destruct method of it. - $object = $this->_loadedModules[$module]['object']; + $object = $this->loadedModules[$module]['object']; unset($object); - unset($this->_loadedModules[$module]); + unset($this->loadedModules[$module]); //Return the message Unloaded. return 'U'; @@ -253,11 +255,11 @@ public function timeLoaded($module) { $module = Inflector::camelize($module); - if (!isset($this->_loadedModules[$module])) { + if (!isset($this->loadedModules[$module])) { return false; } - return $this->_loadedModules[$module]['loaded']; + return $this->loadedModules[$module]['loaded']; } /** @@ -271,11 +273,11 @@ public function isModified($module) { $module = Inflector::camelize($module); - if (!isset($this->_loadedModules[$module])) { + if (!isset($this->loadedModules[$module])) { return -1; } - return $this->_loadedModules[$module]['modified']; + return $this->loadedModules[$module]['modified']; } /** @@ -285,7 +287,7 @@ public function isModified($module) */ public function getLoadedModules() { - return array_keys($this->_loadedModules); + return array_keys($this->loadedModules); } /** @@ -295,7 +297,7 @@ public function getLoadedModules() */ public function count() { - return count($this->_loadedModules); + return count($this->loadedModules); } /** @@ -309,11 +311,11 @@ public function offsetGet($module) { $module = Inflector::camelize($module); - if (!isset($this->_loadedModules[$module])) { + if (!isset($this->loadedModules[$module])) { return false; } - return $this->_loadedModules[$module]['object']; + return $this->loadedModules[$module]['object']; } /** @@ -327,7 +329,7 @@ public function offsetExists($module) { $module = Inflector::camelize($module); - return isset($this->_loadedModules[$module]); + return isset($this->loadedModules[$module]); } /** @@ -343,7 +345,9 @@ public function offsetExists($module) public function offsetSet($offset, $module) { if (!$module instanceof ModuleInterface) { - throw new \RuntimeException(sprintf('ModuleManager::offsetSet() expects "%s" to be an instance of ModuleInterface.', $module)); + throw new RuntimeException( + sprintf('ModuleManager::offsetSet() expects "%s" to be an instance of ModuleInterface.', $module) + ); } $newModule = [ @@ -353,12 +357,12 @@ public function offsetSet($offset, $module) 'modified' => false ]; - if (in_array($offset, $this->_priorityList)) { - $temp = array_reverse($this->_loadedModules, true); + if (in_array($offset, $this->priorityList)) { + $temp = array_reverse($this->loadedModules, true); $temp[$offset] = $newModule; - $this->_loadedModules = array_reverse($temp, true); + $this->loadedModules = array_reverse($temp, true); } else { - $this->_loadedModules[$offset] = $newModule; + $this->loadedModules[$offset] = $newModule; } return true; @@ -373,11 +377,11 @@ public function offsetSet($offset, $module) */ public function offsetUnset($module) { - if (!isset($this->_loadedModules[$module])) { + if (!isset($this->loadedModules[$module])) { return true; } - unset($this->_loadedModules[$module]); + unset($this->loadedModules[$module]); return true; } diff --git a/src/Network/Server.php b/src/Network/Server.php index a9da7b0..6d85060 100644 --- a/src/Network/Server.php +++ b/src/Network/Server.php @@ -11,8 +11,18 @@ class Server { + /** + * The Discord instance. + * + * @var \Discord\Discord + */ public $Discord; + /** + * The Module Manager instance. + * + * @var \Skinny\Module\ModuleManager + */ public $ModuleManager; /** diff --git a/src/Utility/Command.php b/src/Utility/Command.php index 816565e..4ca2280 100644 --- a/src/Utility/Command.php +++ b/src/Utility/Command.php @@ -14,7 +14,8 @@ class Command */ public static function syntax($message) { - return 'Not enough parameters given. Syntax: `' . Configure::read('Command.prefix') . Configure::read('Commands')[$message['command']]['syntax'] . '`'; + return 'Not enough parameters given. Syntax: `' . Configure::read('Command.prefix') . + Configure::read('Commands')[$message['command']]['syntax'] . '`'; } /** diff --git a/src/Utility/Hash.php b/src/Utility/Hash.php index e0bb644..0c0df9c 100644 --- a/src/Utility/Hash.php +++ b/src/Utility/Hash.php @@ -31,17 +31,17 @@ public static function insert(array $data, $path, $values = null) } if ($noTokens && strpos($path, '{') === false) { - return static::_simpleOp('insert', $data, $tokens, $values); + return static::simpleOp('insert', $data, $tokens, $values); } $token = array_shift($tokens); $nextPath = implode('.', $tokens); - list($token, $conditions) = static::_splitConditions($token); + list($token, $conditions) = static::splitConditions($token); foreach ($data as $k => $v) { - if (static::_matchToken($k, $token)) { - if ($conditions && static::_matches($v, $conditions)) { + if (static::matchToken($k, $token)) { + if ($conditions && static::matches($v, $conditions)) { $data[$k] = array_merge($v, $values); continue; } @@ -93,7 +93,8 @@ public static function get(array $data, $path, $default = null) case 2: return isset($data[$parts[0]][$parts[1]]) ? $data[$parts[0]][$parts[1]] : $default; case 3: - return isset($data[$parts[0]][$parts[1]][$parts[2]]) ? $data[$parts[0]][$parts[1]][$parts[2]] : $default; + return isset($data[$parts[0]][$parts[1]][$parts[2]]) ? + $data[$parts[0]][$parts[1]][$parts[2]] : $default; default: foreach ($parts as $key) { if (is_array($data) && isset($data[$key])) { @@ -130,18 +131,18 @@ public static function remove(array $data, $path) $tokens = $noTokens ? explode('.', $path) : Text::tokenize($path, '.', '[', ']'); if ($noExpansion && $noTokens) { - return static::_simpleOp('remove', $data, $tokens); + return static::simpleOp('remove', $data, $tokens); } $token = array_shift($tokens); $nextPath = implode('.', $tokens); - list($token, $conditions) = self::_splitConditions($token); + list($token, $conditions) = self::splitConditions($token); foreach ($data as $k => $v) { - $match = static::_matchToken($k, $token); + $match = static::matchToken($k, $token); if ($match && is_array($v)) { - if ($conditions && static::_matches($v, $conditions)) { + if ($conditions && static::matches($v, $conditions)) { unset($data[$k]); continue; } @@ -186,7 +187,7 @@ public static function expand(array $data, $separator = '.') } $stack = [[$child, &$result]]; - static::_merge($stack, $result); + static::mergeHelper($stack, $result); } return $result; @@ -199,7 +200,8 @@ public static function expand(array $data, $separator = '.') * Hash::merge() will behave in a recursive fashion (unlike `array_merge`). But it will not act recursively for * keys that contain scalar values (unlike `array_merge_recursive`). * - * Note: This function will work with an unlimited amount of arguments and typecasts non-array parameters into arrays. + * Note: This function will work with an unlimited amount of arguments + * and typecasts non-array parameters into arrays. * * @param array $data Array to be merged. * @param mixed $merge Array to merge with. The argument and all trailing arguments will be array cast when merged. @@ -215,7 +217,7 @@ public static function merge(array $data, $merge) $stack[] = [(array)$curArg, &$return]; } unset($curArg); - static::_merge($stack, $return); + static::mergeHelper($stack, $return); return $return; } @@ -227,7 +229,7 @@ public static function merge(array $data, $merge) * * @return array [token, conditions] with token splitted */ - protected static function _splitConditions($token) + protected static function splitConditions($token) { $conditions = false; $position = strpos($token, '['); @@ -247,7 +249,7 @@ protected static function _splitConditions($token) * * @return bool */ - protected static function _matchToken($key, $token) + protected static function matchToken($key, $token) { if ($token === '{n}') { return is_numeric($key); @@ -269,7 +271,7 @@ protected static function _matchToken($key, $token) * @param string $selector The patterns to match. * @return bool Fitness of expression. */ - protected static function _matches(array $data, $selector) + protected static function matches(array $data, $selector) { preg_match_all( '/(\[ (?P[^=>[><]) \s* (?P(?:\/.*?\/ | [^\]]+)) )? \])/x', @@ -333,7 +335,7 @@ protected static function _matches(array $data, $selector) * * @return array data. */ - protected static function _simpleOp($op, $data, $path, $values = null) + protected static function simpleOp($op, $data, $path, $values = null) { $_list =& $data; @@ -380,12 +382,14 @@ protected static function _simpleOp($op, $data, $path, $values = null) * * @return void */ - protected static function _merge($stack, &$return) + protected static function mergeHelper($stack, &$return) { while (!empty($stack)) { foreach ($stack as $curKey => &$curMerge) { foreach ($curMerge[0] as $key => &$val) { - if (!empty($curMerge[1][$key]) && (array)$curMerge[1][$key] === $curMerge[1][$key] && (array)$val === $val) { + if (!empty($curMerge[1][$key]) && + (array)$curMerge[1][$key] === $curMerge[1][$key] && + (array)$val === $val) { $stack[] = [&$val, &$curMerge[1][$key]]; } elseif ((int)$key === $key && isset($curMerge[1][$key])) { $curMerge[1][] = $val; diff --git a/src/Utility/Inflector.php b/src/Utility/Inflector.php index 4c1c5fb..6d27d7f 100644 --- a/src/Utility/Inflector.php +++ b/src/Utility/Inflector.php @@ -8,47 +8,48 @@ class Inflector * * @var array */ - protected static $_cache = []; + protected static $cache = []; /** - * Cache inflected values, and return if already available + * Cache inflected values, and return if already available. + * + * @param string $type Inflection type. + * @param string $key Original value. + * @param string|bool $value Inflected value. * - * @param string $type Inflection type - * @param string $key Original value - * @param string|bool $value Inflected value * @return string|bool Inflected value on cache hit or false on cache miss. */ - protected static function _cache($type, $key, $value = false) + protected static function cache($type, $key, $value = false) { $key = '_' . $key; $type = '_' . $type; if ($value !== false) { - static::$_cache[$type][$key] = $value; + static::$cache[$type][$key] = $value; return $value; } - if (!isset(static::$_cache[$type][$key])) { + if (!isset(static::$cache[$type][$key])) { return false; } - return static::$_cache[$type][$key]; + return static::$cache[$type][$key]; } /** * Returns the input lower_case_delimited_string as a CamelCasedString. * - * @param string $string String to camelize - * @param string $delimiter the delimiter in the input string + * @param string $string String to camelize. + * @param string $delimiter The delimiter in the input string. + * * @return string CamelizedStringLikeThis. - * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-camelcase-and-under-scored-forms */ public static function camelize($string, $delimiter = '_') { $cacheKey = __FUNCTION__ . $delimiter; - $result = static::_cache($cacheKey, $string); + $result = static::cache($cacheKey, $string); if ($result === false) { $result = str_replace(' ', '', static::humanize($string, $delimiter)); - static::_cache(__FUNCTION__, $string, $result); + static::cache(__FUNCTION__, $string, $result); } return $result; @@ -58,22 +59,22 @@ public static function camelize($string, $delimiter = '_') * Returns the input lower_case_delimited_string as 'A Human Readable String'. * (Underscores are replaced by spaces and capitalized following words.) * - * @param string $string String to be humanized - * @param string $delimiter the character to replace with a space - * @return string Human-readable string - * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-human-readable-forms + * @param string $string String to be humanized. + * @param string $delimiter The character to replace with a space. + * + * @return string Human-readable string. */ public static function humanize($string, $delimiter = '_') { $cacheKey = __FUNCTION__ . $delimiter; - $result = static::_cache($cacheKey, $string); + $result = static::cache($cacheKey, $string); if ($result === false) { $result = explode(' ', str_replace($delimiter, ' ', $string)); foreach ($result as &$word) { $word = mb_strtoupper(mb_substr($word, 0, 1)) . mb_substr($word, 1); } $result = implode(' ', $result); - static::_cache($cacheKey, $string, $result); + static::cache($cacheKey, $string, $result); } return $result;