Browse Source

Add more types in Utility.

Mark Story 7 years ago
parent
commit
5cdd44fd56

+ 8 - 4
src/Utility/Crypto/OpenSsl.php

@@ -40,7 +40,7 @@ class OpenSsl
      * @return string Encrypted data.
      * @throws \InvalidArgumentException On invalid data or key.
      */
-    public static function encrypt($plain, $key)
+    public static function encrypt(string $plain, string $key): string
     {
         $method = 'AES-256-CBC';
         $ivSize = openssl_cipher_iv_length($method);
@@ -58,15 +58,19 @@ class OpenSsl
      * @return string Decrypted data. Any trailing null bytes will be removed.
      * @throws \InvalidArgumentException On invalid data or key.
      */
-    public static function decrypt($cipher, $key)
+    public static function decrypt(string $cipher, string $key): ?string
     {
         $method = 'AES-256-CBC';
         $ivSize = openssl_cipher_iv_length($method);
 
         $iv = mb_substr($cipher, 0, $ivSize, '8bit');
-
         $cipher = mb_substr($cipher, $ivSize, null, '8bit');
 
-        return openssl_decrypt($cipher, $method, $key, OPENSSL_RAW_DATA, $iv);
+        $value = openssl_decrypt($cipher, $method, $key, OPENSSL_RAW_DATA, $iv);
+        if ($value === false) {
+            return null;
+        }
+
+        return $value;
     }
 }

+ 29 - 29
src/Utility/Hash.php

@@ -122,7 +122,7 @@ class Hash
      *   if there are no matches.
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::extract
      */
-    public static function extract($data, $path)
+    public static function extract($data, string $path)
     {
         if (!(is_array($data) || $data instanceof ArrayAccess)) {
             throw new InvalidArgumentException(
@@ -195,7 +195,7 @@ class Hash
      * @param string $token the token being splitted.
      * @return array [token, conditions] with token splitted
      */
-    protected static function _splitConditions($token)
+    protected static function _splitConditions(string $token): array
     {
         $conditions = false;
         $position = strpos($token, '[');
@@ -214,7 +214,7 @@ class Hash
      * @param string $token The token being matched.
      * @return bool
      */
-    protected static function _matchToken($key, $token)
+    protected static function _matchToken($key, $token): bool
     {
         switch ($token) {
             case '{n}':
@@ -235,7 +235,7 @@ class Hash
      * @param string $selector The patterns to match.
      * @return bool Fitness of expression.
      */
-    protected static function _matches($data, $selector)
+    protected static function _matches($data, string $selector): bool
     {
         preg_match_all(
             '/(\[ (?P<attr>[^=><!]+?) (\s* (?P<op>[><!]?[=]|[><]) \s* (?P<val>(?:\/.*?\/ | [^\]]+)) )? \])/x',
@@ -301,7 +301,7 @@ class Hash
      * @return array The data with $values inserted.
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::insert
      */
-    public static function insert(array $data, $path, $values = null)
+    public static function insert(array $data, string $path, $values = null): array
     {
         $noTokens = strpos($path, '[') === false;
         if ($noTokens && strpos($path, '.') === false) {
@@ -393,7 +393,7 @@ class Hash
      * @return array The modified array.
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::remove
      */
-    public static function remove(array $data, $path)
+    public static function remove(array $data, string $path): array
     {
         $noTokens = strpos($path, '[') === false;
         $noExpansion = strpos($path, '{') === false;
@@ -447,14 +447,14 @@ class Hash
      * following the path specified in `$groupPath`.
      *
      * @param array $data Array from where to extract keys and values
-     * @param string $keyPath A dot-separated string.
-     * @param string|null $valuePath A dot-separated string.
-     * @param string|null $groupPath A dot-separated string.
+     * @param string|array $keyPath A dot-separated string.
+     * @param string|array|null $valuePath A dot-separated string.
+     * @param string|array|null $groupPath A dot-separated string.
      * @return array Combined array
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::combine
      * @throws \RuntimeException When keys and values count is unequal.
      */
-    public static function combine(array $data, $keyPath, $valuePath = null, $groupPath = null)
+    public static function combine(array $data, $keyPath, $valuePath = null, ?string $groupPath = null): array
     {
         if (empty($data)) {
             return [];
@@ -532,7 +532,7 @@ class Hash
      * @see sprintf()
      * @see \Cake\Utility\Hash::extract()
      */
-    public static function format(array $data, array $paths, $format)
+    public static function format(array $data, array $paths, string $format): ?array
     {
         $extracted = [];
         $count = count($paths);
@@ -570,7 +570,7 @@ class Hash
      * @return bool true If $data contains $needle, false otherwise
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::contains
      */
-    public static function contains(array $data, array $needle)
+    public static function contains(array $data, array $needle): bool
     {
         if (empty($data) || empty($needle)) {
             return false;
@@ -614,7 +614,7 @@ class Hash
      * @see \Cake\Utility\Hash::extract()
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::check
      */
-    public static function check(array $data, $path)
+    public static function check(array $data, string $path): bool
     {
         $results = static::extract($data, $path);
         if (!is_array($results)) {
@@ -633,7 +633,7 @@ class Hash
      * @return array Filtered array
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::filter
      */
-    public static function filter(array $data, $callback = ['self', '_filter'])
+    public static function filter(array $data, $callback = ['self', '_filter']): array
     {
         foreach ($data as $k => $v) {
             if (is_array($v)) {
@@ -665,7 +665,7 @@ class Hash
      * @return array
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::flatten
      */
-    public static function flatten(array $data, $separator = '.')
+    public static function flatten(array $data, string $separator = '.'): array
     {
         $result = [];
         $stack = [];
@@ -709,7 +709,7 @@ class Hash
      * @return array
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::expand
      */
-    public static function expand(array $data, $separator = '.')
+    public static function expand(array $data, string $separator = '.'): array
     {
         $result = [];
         foreach ($data as $flat => $value) {
@@ -746,7 +746,7 @@ class Hash
      * @return array Merged array
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::merge
      */
-    public static function merge(array $data, $merge)
+    public static function merge(array $data, $merge): array
     {
         $args = array_slice(func_get_args(), 1);
         $return = $data;
@@ -796,7 +796,7 @@ class Hash
      * @return bool true if values are numeric, false otherwise
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::numeric
      */
-    public static function numeric(array $data)
+    public static function numeric(array $data): bool
     {
         if (empty($data)) {
             return false;
@@ -816,7 +816,7 @@ class Hash
      * @return int The number of dimensions in $data
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::dimensions
      */
-    public static function dimensions(array $data)
+    public static function dimensions(array $data): int
     {
         if (empty($data)) {
             return 0;
@@ -843,7 +843,7 @@ class Hash
      * @return int The maximum number of dimensions in $data
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::maxDimensions
      */
-    public static function maxDimensions(array $data)
+    public static function maxDimensions(array $data): int
     {
         $depth = [];
         if (is_array($data) && !empty($data)) {
@@ -869,7 +869,7 @@ class Hash
      * @return array An array of the modified values.
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::map
      */
-    public static function map(array $data, $path, $function)
+    public static function map(array $data, $path, $function): array
     {
         $values = (array)static::extract($data, $path);
 
@@ -885,7 +885,7 @@ class Hash
      * @return mixed The reduced value.
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::reduce
      */
-    public static function reduce(array $data, $path, $function)
+    public static function reduce(array $data, string $path, $function)
     {
         $values = (array)static::extract($data, $path);
 
@@ -917,7 +917,7 @@ class Hash
      * @return mixed The results of the applied method.
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::apply
      */
-    public static function apply(array $data, $path, $function)
+    public static function apply(array $data, string $path, $function)
     {
         $values = (array)static::extract($data, $path);
 
@@ -957,7 +957,7 @@ class Hash
      * @return array Sorted array of data
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::sort
      */
-    public static function sort(array $data, $path, $dir = 'asc', $type = 'regular')
+    public static function sort(array $data, string $path, string $dir = 'asc', $type = 'regular')
     {
         if (empty($data)) {
             return [];
@@ -1043,7 +1043,7 @@ class Hash
      * @param string|null $key The key for the data.
      * @return array
      */
-    protected static function _squash(array $data, $key = null)
+    protected static function _squash(array $data, $key = null): array
     {
         $stack = [];
         foreach ($data as $k => $r) {
@@ -1072,7 +1072,7 @@ class Hash
      *    The expression for this function is ($data - $compare) + ($compare - ($data - $compare))
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::diff
      */
-    public static function diff(array $data, array $compare)
+    public static function diff(array $data, array $compare): array
     {
         if (empty($data)) {
             return (array)$compare;
@@ -1099,7 +1099,7 @@ class Hash
      * @return array The merged array.
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::mergeDiff
      */
-    public static function mergeDiff(array $data, array $compare)
+    public static function mergeDiff(array $data, array $compare): array
     {
         if (empty($data) && !empty($compare)) {
             return $compare;
@@ -1126,7 +1126,7 @@ class Hash
      * @return array
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::normalize
      */
-    public static function normalize(array $data, $assoc = true)
+    public static function normalize(array $data, bool $assoc = true): array
     {
         $keys = array_keys($data);
         $count = count($keys);
@@ -1174,7 +1174,7 @@ class Hash
      * @throws \InvalidArgumentException When providing invalid data.
      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::nest
      */
-    public static function nest(array $data, array $options = [])
+    public static function nest(array $data, array $options = []): array
     {
         if (!$data) {
             return $data;

+ 11 - 11
src/Utility/Inflector.php

@@ -477,7 +477,7 @@ class Inflector
      *        new rules that are being defined in $rules.
      * @return void
      */
-    public static function rules($type, $rules, $reset = false)
+    public static function rules(string $type, array $rules, bool $reset = false)
     {
         $var = '_' . $type;
 
@@ -502,7 +502,7 @@ class Inflector
      * @return string Word in plural
      * @link https://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-plural-singular-forms
      */
-    public static function pluralize($word)
+    public static function pluralize(string $word): string
     {
         if (isset(static::$_cache['pluralize'][$word])) {
             return static::$_cache['pluralize'][$word];
@@ -545,7 +545,7 @@ class Inflector
      * @return string Word in singular
      * @link https://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-plural-singular-forms
      */
-    public static function singularize($word)
+    public static function singularize(string $word): string
     {
         if (isset(static::$_cache['singularize'][$word])) {
             return static::$_cache['singularize'][$word];
@@ -592,7 +592,7 @@ class Inflector
      * @return string CamelizedStringLikeThis.
      * @link https://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-camelcase-and-under-scored-forms
      */
-    public static function camelize($string, $delimiter = '_')
+    public static function camelize(string $string, string $delimiter = '_'): string
     {
         $cacheKey = __FUNCTION__ . $delimiter;
 
@@ -615,7 +615,7 @@ class Inflector
      * @return string underscore_version of the input string
      * @link https://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-camelcase-and-under-scored-forms
      */
-    public static function underscore($string)
+    public static function underscore(string $string): string
     {
         return static::delimit(str_replace('-', '_', $string), '_');
     }
@@ -628,7 +628,7 @@ class Inflector
      * @param string $string The string to dasherize.
      * @return string Dashed version of the input string
      */
-    public static function dasherize($string)
+    public static function dasherize(string $string): string
     {
         return static::delimit(str_replace('_', '-', $string), '-');
     }
@@ -642,7 +642,7 @@ class Inflector
      * @return string Human-readable string
      * @link https://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-human-readable-forms
      */
-    public static function humanize($string, $delimiter = '_')
+    public static function humanize(string $string, string $delimiter = '_'): string
     {
         $cacheKey = __FUNCTION__ . $delimiter;
 
@@ -667,7 +667,7 @@ class Inflector
      * @param string $delimiter the character to use as a delimiter
      * @return string delimited string
      */
-    public static function delimit($string, $delimiter = '_')
+    public static function delimit(string $string, string $delimiter = '_'): string
     {
         $cacheKey = __FUNCTION__ . $delimiter;
 
@@ -688,7 +688,7 @@ class Inflector
      * @return string Name of the database table for given class
      * @link https://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-table-and-class-name-forms
      */
-    public static function tableize($className)
+    public static function tableize(string $className): string
     {
         $result = static::_cache(__FUNCTION__, $className);
 
@@ -707,7 +707,7 @@ class Inflector
      * @return string Class name
      * @link https://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-table-and-class-name-forms
      */
-    public static function classify($tableName)
+    public static function classify(string $tableName): string
     {
         $result = static::_cache(__FUNCTION__, $tableName);
 
@@ -726,7 +726,7 @@ class Inflector
      * @return string in variable form
      * @link https://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-variable-names
      */
-    public static function variable($string)
+    public static function variable(string $string): string
     {
         $result = static::_cache(__FUNCTION__, $string);
 

+ 1 - 1
tests/TestCase/Utility/Crypto/OpenSslTest.php

@@ -67,6 +67,6 @@ class OpenSslTest extends TestCase
         $result = $this->crypt->encrypt($txt, $key);
 
         $key = 'Not the same key.';
-        $this->assertFalse($this->crypt->decrypt($txt, $key), 'Modified key will fail.');
+        $this->assertNull($this->crypt->decrypt($txt, $key), 'Modified key will fail.');
     }
 }

+ 3 - 9
tests/TestCase/Utility/InflectorTest.php

@@ -379,10 +379,8 @@ class InflectorTest extends TestCase
         $this->assertSame('test_thing_extra', Inflector::underscore('testThingExtra'));
         $this->assertSame('test_thing_extrå', Inflector::underscore('testThingExtrå'));
 
-        // Test stupid values
-        $this->assertSame('', Inflector::underscore(''));
-        $this->assertSame('0', Inflector::underscore(0));
-        $this->assertSame('', Inflector::underscore(false));
+        // Test other values
+        $this->assertSame('0', Inflector::underscore('0'));
     }
 
     /**
@@ -399,10 +397,8 @@ class InflectorTest extends TestCase
         $this->assertSame('test-this-thing', Inflector::dasherize('test_this_thing'));
 
         // Test stupid values
-        $this->assertSame('', Inflector::dasherize(null));
         $this->assertSame('', Inflector::dasherize(''));
-        $this->assertSame('0', Inflector::dasherize(0));
-        $this->assertSame('', Inflector::dasherize(false));
+        $this->assertSame('0', Inflector::dasherize('0'));
     }
 
     /**
@@ -476,8 +472,6 @@ class InflectorTest extends TestCase
         $this->assertEquals('Posts', Inflector::humanize('posts'));
         $this->assertEquals('Posts Tags', Inflector::humanize('posts_tags'));
         $this->assertEquals('File Systems', Inflector::humanize('file_systems'));
-        $this->assertSame('', Inflector::humanize(null));
-        $this->assertSame('', Inflector::humanize(false));
         $this->assertSame('Hello Wörld', Inflector::humanize('hello_wörld'));
         $this->assertSame('福岡 City', Inflector::humanize('福岡_city'));
     }