From 777b8340c7c50475b881792c2576ff78b5183a0d Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 16 Aug 2019 20:14:08 +0200 Subject: [PATCH 01/16] Return null explicitly --- src/AbstractDateDropdown.php | 4 ++-- src/Compress/AbstractCompressionAlgorithm.php | 2 +- src/ToNull.php | 12 ++++++------ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/AbstractDateDropdown.php b/src/AbstractDateDropdown.php index 076b8137..97195df8 100644 --- a/src/AbstractDateDropdown.php +++ b/src/AbstractDateDropdown.php @@ -105,13 +105,13 @@ public function filter($value) if ($this->isNullOnEmpty() && array_reduce($value, __CLASS__ . '::reduce', false) ) { - return; + return null; } if ($this->isNullOnAllEmpty() && array_reduce($value, __CLASS__ . '::reduce', true) ) { - return; + return null; } $this->filterable($value); diff --git a/src/Compress/AbstractCompressionAlgorithm.php b/src/Compress/AbstractCompressionAlgorithm.php index 993d9db8..c62539cb 100644 --- a/src/Compress/AbstractCompressionAlgorithm.php +++ b/src/Compress/AbstractCompressionAlgorithm.php @@ -51,7 +51,7 @@ public function getOptions($option = null) } if (! array_key_exists($option, $this->options)) { - return; + return null; } return $this->options[$option]; diff --git a/src/ToNull.php b/src/ToNull.php index 59fef14c..db77a7cc 100644 --- a/src/ToNull.php +++ b/src/ToNull.php @@ -127,42 +127,42 @@ public function filter($value) // FLOAT (0.0) if ($type & self::TYPE_FLOAT) { if (is_float($value) && ($value == 0.0)) { - return; + return null; } } // STRING ZERO ('0') if ($type & self::TYPE_ZERO_STRING) { if (is_string($value) && ($value == '0')) { - return; + return null; } } // STRING ('') if ($type & self::TYPE_STRING) { if (is_string($value) && ($value == '')) { - return; + return null; } } // EMPTY_ARRAY (array()) if ($type & self::TYPE_EMPTY_ARRAY) { if (is_array($value) && ($value == [])) { - return; + return null; } } // INTEGER (0) if ($type & self::TYPE_INTEGER) { if (is_int($value) && ($value == 0)) { - return; + return null; } } // BOOLEAN (false) if ($type & self::TYPE_BOOLEAN) { if (is_bool($value) && ($value == false)) { - return; + return null; } } From 2de67550822ac3e4b4b4efc35af43a161d7b7e73 Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 16 Aug 2019 20:18:15 +0200 Subject: [PATCH 02/16] Use strict type comparison --- src/AbstractUnicode.php | 2 +- src/Boolean.php | 28 ++++++++++++++-------------- src/Compress.php | 2 +- src/Compress/Gz.php | 6 +++--- src/Compress/Tar.php | 6 +++--- src/Compress/Zip.php | 2 +- src/DataUnitFormatter.php | 10 +++++----- src/Digits.php | 2 +- src/Encrypt.php | 2 +- src/Encrypt/Openssl.php | 2 +- src/File/Rename.php | 12 ++++++------ src/Inflector.php | 4 ++-- src/RealPath.php | 10 +++++----- src/StripTags.php | 2 +- src/ToNull.php | 20 ++++++++++---------- src/UriNormalize.php | 2 +- 16 files changed, 56 insertions(+), 56 deletions(-) diff --git a/src/AbstractUnicode.php b/src/AbstractUnicode.php index 93b53850..5faa65ab 100644 --- a/src/AbstractUnicode.php +++ b/src/AbstractUnicode.php @@ -31,7 +31,7 @@ public function setEncoding($encoding = null) $encoding = strtolower($encoding); $mbEncodings = array_map('strtolower', mb_list_encodings()); - if (! in_array($encoding, $mbEncodings)) { + if (! in_array($encoding, $mbEncodings, true)) { throw new Exception\InvalidArgumentException(sprintf( "Encoding '%s' is not supported by mbstring extension", $encoding diff --git a/src/Boolean.php b/src/Boolean.php index 94ea983e..d8c9eb60 100644 --- a/src/Boolean.php +++ b/src/Boolean.php @@ -99,14 +99,14 @@ public function setType($type = null) foreach ($type as $value) { if (is_int($value)) { $detected |= $value; - } elseif (in_array($value, $this->constants)) { - $detected |= array_search($value, $this->constants); + } elseif (($found = array_search($value, $this->constants, true)) !== false) { + $detected |= $found; } } $type = $detected; - } elseif (is_string($type) && in_array($type, $this->constants)) { - $type = array_search($type, $this->constants); + } elseif (is_string($type) && ($found = array_search($type, $this->constants, true)) !== false) { + $type = $found; } if (! is_int($type) || ($type < 0) || ($type > self::TYPE_ALL)) { @@ -209,11 +209,11 @@ public function filter($value) // FALSE_STRING ('false') if ($type & self::TYPE_FALSE_STRING) { - if (is_string($value) && (strtolower($value) == 'false')) { + if (is_string($value) && strtolower($value) === 'false') { return false; } - if (! $casting && is_string($value) && (strtolower($value) == 'true')) { + if (! $casting && is_string($value) && strtolower($value) === 'true') { return true; } } @@ -227,47 +227,47 @@ public function filter($value) // EMPTY_ARRAY (array()) if ($type & self::TYPE_EMPTY_ARRAY) { - if (is_array($value) && ($value == [])) { + if (is_array($value) && $value === []) { return false; } } // ZERO_STRING ('0') if ($type & self::TYPE_ZERO_STRING) { - if (is_string($value) && ($value == '0')) { + if (is_string($value) && $value === '0') { return false; } - if (! $casting && (is_string($value)) && ($value == '1')) { + if (! $casting && is_string($value) && $value === '1') { return true; } } // STRING ('') if ($type & self::TYPE_STRING) { - if (is_string($value) && ($value == '')) { + if (is_string($value) && $value === '') { return false; } } // FLOAT (0.0) if ($type & self::TYPE_FLOAT) { - if (is_float($value) && ($value == 0.0)) { + if (is_float($value) && $value === 0.0) { return false; } - if (! $casting && is_float($value) && ($value == 1.0)) { + if (! $casting && is_float($value) && $value === 1.0) { return true; } } // INTEGER (0) if ($type & self::TYPE_INTEGER) { - if (is_int($value) && ($value == 0)) { + if (is_int($value) && $value === 0) { return false; } - if (! $casting && is_int($value) && ($value == 1)) { + if (! $casting && is_int($value) && $value === 1) { return true; } } diff --git a/src/Compress.php b/src/Compress.php index 5f964bcd..e09c5e1d 100644 --- a/src/Compress.php +++ b/src/Compress.php @@ -64,7 +64,7 @@ public function setOptions($options) } foreach ($options as $key => $value) { - if ($key == 'options') { + if ($key === 'options') { $key = 'adapterOptions'; } $method = 'set' . ucfirst($key); diff --git a/src/Compress/Gz.php b/src/Compress/Gz.php index 5744ad89..9411cb3e 100644 --- a/src/Compress/Gz.php +++ b/src/Compress/Gz.php @@ -92,7 +92,7 @@ public function getMode() */ public function setMode($mode) { - if (($mode != 'compress') && ($mode != 'deflate')) { + if ($mode !== 'compress' && $mode !== 'deflate') { throw new Exception\InvalidArgumentException('Given compression mode not supported'); } @@ -141,7 +141,7 @@ public function compress($content) gzwrite($file, $content); gzclose($file); $compressed = true; - } elseif ($this->options['mode'] == 'deflate') { + } elseif ($this->options['mode'] === 'deflate') { $compressed = gzdeflate($content, $this->getLevel()); } else { $compressed = gzcompress($content, $this->getLevel()); @@ -186,7 +186,7 @@ public function decompress($content) $file = gzopen($archive, 'r'); $compressed = gzread($file, $size); gzclose($file); - } elseif ($mode == 'deflate') { + } elseif ($mode === 'deflate') { $compressed = gzinflate($content); } else { $compressed = gzuncompress($content); diff --git a/src/Compress/Tar.php b/src/Compress/Tar.php index c0bc7ed0..4bc0b960 100644 --- a/src/Compress/Tar.php +++ b/src/Compress/Tar.php @@ -128,15 +128,15 @@ public function getMode() public function setMode($mode) { $mode = strtolower($mode); - if (($mode != 'bz2') && ($mode != 'gz')) { + if ($mode !== 'bz2' && $mode !== 'gz') { throw new Exception\InvalidArgumentException("The mode '$mode' is unknown"); } - if (($mode == 'bz2') && (! extension_loaded('bz2'))) { + if ($mode === 'bz2' && ! extension_loaded('bz2')) { throw new Exception\ExtensionNotLoadedException('This mode needs the bz2 extension'); } - if (($mode == 'gz') && (! extension_loaded('zlib'))) { + if ($mode === 'gz' && ! extension_loaded('zlib')) { throw new Exception\ExtensionNotLoadedException('This mode needs the zlib extension'); } diff --git a/src/Compress/Zip.php b/src/Compress/Zip.php index 47fb3e17..157eda78 100644 --- a/src/Compress/Zip.php +++ b/src/Compress/Zip.php @@ -127,7 +127,7 @@ public function compress($content) $dir = dir($current); while (false !== ($node = $dir->read())) { - if (($node == '.') || ($node == '..')) { + if ($node === '.' || $node === '..') { continue; } diff --git a/src/DataUnitFormatter.php b/src/DataUnitFormatter.php index 0f2e2188..676bf0da 100644 --- a/src/DataUnitFormatter.php +++ b/src/DataUnitFormatter.php @@ -80,7 +80,7 @@ public function __construct($options = []) protected function setMode($mode) { $mode = strtolower($mode); - if (! in_array($mode, self::$modes)) { + if (! in_array($mode, self::$modes, true)) { throw new InvalidArgumentException(sprintf('Invalid binary mode: %s', $mode)); } $this->options['mode'] = $mode; @@ -103,7 +103,7 @@ protected function getMode() */ protected function isDecimalMode() { - return $this->getMode() == self::MODE_DECIMAL; + return $this->getMode() === self::MODE_DECIMAL; } /** @@ -113,7 +113,7 @@ protected function isDecimalMode() */ protected function isBinaryMode() { - return $this->getMode() == self::MODE_BINARY; + return $this->getMode() === self::MODE_BINARY; } /** @@ -212,7 +212,7 @@ public function filter($value) // Parse to float and check if value is not zero $amount = (float) $value; - if ($amount == 0) { + if ($amount === 0.0) { return $this->formatAmount($amount); } @@ -222,7 +222,7 @@ public function filter($value) $prefix = $this->getPrefixAt((int)$power); // When the amount is too big, no prefix can be found: - if (is_null($prefix)) { + if ($prefix === null) { return $this->formatAmount($amount); } diff --git a/src/Digits.php b/src/Digits.php index 7bc0b7d9..963ff98f 100644 --- a/src/Digits.php +++ b/src/Digits.php @@ -28,7 +28,7 @@ public function filter($value) if (is_int($value)) { return (string) $value; } - if (! (is_float($value) || is_string($value))) { + if (! is_float($value) && ! is_string($value)) { return $value; } $value = (string) $value; diff --git a/src/Encrypt.php b/src/Encrypt.php index 05f30ddb..974a8b44 100644 --- a/src/Encrypt.php +++ b/src/Encrypt.php @@ -142,7 +142,7 @@ public function setAdapter($options = null) public function __call($method, $options) { $part = substr($method, 0, 3); - if ((($part != 'get') && ($part != 'set')) || ! method_exists($this->adapter, $method)) { + if (($part !== 'get' && $part !== 'set') || ! method_exists($this->adapter, $method)) { throw new Exception\BadMethodCallException("Unknown method '{$method}'"); } diff --git a/src/Encrypt/Openssl.php b/src/Encrypt/Openssl.php index 03abbfaf..ec19e910 100644 --- a/src/Encrypt/Openssl.php +++ b/src/Encrypt/Openssl.php @@ -432,7 +432,7 @@ public function decrypt($value) for ($i = $count; $i > 0; --$i) { $header = unpack('H32print/nsize', substr($value, $length, 18)); $length += 18; - if ($header['print'] == $fingerprint) { + if ($header['print'] === $fingerprint) { $envelope = substr($value, $length, $header['size']); } diff --git a/src/File/Rename.php b/src/File/Rename.php index 7db736a0..bd7e92c3 100644 --- a/src/File/Rename.php +++ b/src/File/Rename.php @@ -123,7 +123,7 @@ public function getNewName($value, $source = false) return $file; } - if ($file['source'] == $file['target']) { + if ($file['source'] === $file['target']) { return $value; } @@ -268,7 +268,7 @@ protected function _convertOptions($options) $found = false; foreach ($this->files as $key => $value) { - if ($value['source'] == $files['source']) { + if ($value['source'] === $files['source']) { $this->files[$key] = $files; $found = true; } @@ -295,14 +295,14 @@ protected function _getFileName($file) // @codingStandardsIgnoreEnd $rename = []; foreach ($this->files as $value) { - if ($value['source'] == '*') { + if ($value['source'] === '*') { if (! isset($rename['source'])) { $rename = $value; $rename['source'] = $file; } } - if ($value['source'] == $file) { + if ($value['source'] === $file) { $rename = $value; break; } @@ -312,14 +312,14 @@ protected function _getFileName($file) return $file; } - if (! isset($rename['target']) || $rename['target'] == '*') { + if (! isset($rename['target']) || $rename['target'] === '*') { $rename['target'] = $rename['source']; } if (is_dir($rename['target'])) { $name = basename($rename['source']); $last = $rename['target'][strlen($rename['target']) - 1]; - if (($last != '/') && ($last != '\\')) { + if ($last !== '/' && $last !== '\\') { $rename['target'] .= DIRECTORY_SEPARATOR; } diff --git a/src/Inflector.php b/src/Inflector.php index be59ac81..3dfcbdf8 100644 --- a/src/Inflector.php +++ b/src/Inflector.php @@ -261,7 +261,7 @@ public function addRules(array $rules) { $keys = array_keys($rules); foreach ($keys as $spec) { - if ($spec[0] == ':') { + if ($spec[0] === ':') { $this->addFilterRule($spec, $rules[$spec]); } else { $this->setStaticRule($spec, $rules[$spec]); @@ -451,7 +451,7 @@ public function filter($source) $inflectedTarget = preg_replace(array_keys($processedParts), array_values($processedParts), $this->target); if ($this->throwTargetExceptionsOn - && (preg_match('#(?=' . $pregQuotedTargetReplacementIdentifier.'[A-Za-z]{1})#', $inflectedTarget) == true) + && preg_match('#(?=' . $pregQuotedTargetReplacementIdentifier.'[A-Za-z]{1})#', $inflectedTarget) ) { throw new Exception\RuntimeException( 'A replacement identifier ' . $this->targetReplacementIdentifier diff --git a/src/RealPath.php b/src/RealPath.php index cf558a60..fe312aaf 100644 --- a/src/RealPath.php +++ b/src/RealPath.php @@ -90,26 +90,26 @@ public function filter($value) } $drive = ''; - if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { + if (stripos(PHP_OS, 'WIN') === 0) { $path = preg_replace('/[\\\\\/]/', DIRECTORY_SEPARATOR, $path); if (preg_match('/([a-zA-Z]\:)(.*)/', $path, $matches)) { list(, $drive, $path) = $matches; } else { $cwd = getcwd(); $drive = substr($cwd, 0, 2); - if (substr($path, 0, 1) != DIRECTORY_SEPARATOR) { + if (strpos($path, DIRECTORY_SEPARATOR) !== 0) { $path = substr($cwd, 3) . DIRECTORY_SEPARATOR . $path; } } - } elseif (substr($path, 0, 1) != DIRECTORY_SEPARATOR) { + } elseif (strpos($path, DIRECTORY_SEPARATOR) !== 0) { $path = getcwd() . DIRECTORY_SEPARATOR . $path; } $stack = []; $parts = explode(DIRECTORY_SEPARATOR, $path); foreach ($parts as $dir) { - if (strlen($dir) && $dir !== '.') { - if ($dir == '..') { + if ($dir !== '' && $dir !== '.') { + if ($dir === '..') { array_pop($stack); } else { array_push($stack, $dir); diff --git a/src/StripTags.php b/src/StripTags.php index 9ff89073..35dcc17f 100644 --- a/src/StripTags.php +++ b/src/StripTags.php @@ -269,7 +269,7 @@ protected function _filterTag($tag) foreach ($matches[1] as $index => $attributeName) { $attributeName = strtolower($attributeName); $attributeDelimiter = empty($matches[2][$index]) ? $matches[4][$index] : $matches[2][$index]; - $attributeValue = (strlen($matches[3][$index]) == 0) ? $matches[5][$index] : $matches[3][$index]; + $attributeValue = $matches[3][$index] === '' ? $matches[5][$index] : $matches[3][$index]; // If the attribute is not allowed, then remove it entirely if (! array_key_exists($attributeName, $this->tagsAllowed[$tagName]) diff --git a/src/ToNull.php b/src/ToNull.php index db77a7cc..efb0edcb 100644 --- a/src/ToNull.php +++ b/src/ToNull.php @@ -79,14 +79,14 @@ public function setType($type = null) foreach ($type as $value) { if (is_int($value)) { $detected |= $value; - } elseif (in_array($value, $this->constants)) { - $detected |= array_search($value, $this->constants); + } elseif (($found = array_search($value, $this->constants, true)) !== false) { + $detected |= $found; } } $type = $detected; - } elseif (is_string($type) && in_array($type, $this->constants)) { - $type = array_search($type, $this->constants); + } elseif (is_string($type) && ($found = array_search($type, $this->constants, true)) !== false) { + $type = $found; } if (! is_int($type) || ($type < 0) || ($type > self::TYPE_ALL)) { @@ -126,42 +126,42 @@ public function filter($value) // FLOAT (0.0) if ($type & self::TYPE_FLOAT) { - if (is_float($value) && ($value == 0.0)) { + if (is_float($value) && $value === 0.0) { return null; } } // STRING ZERO ('0') if ($type & self::TYPE_ZERO_STRING) { - if (is_string($value) && ($value == '0')) { + if (is_string($value) && $value === '0') { return null; } } // STRING ('') if ($type & self::TYPE_STRING) { - if (is_string($value) && ($value == '')) { + if (is_string($value) && $value === '') { return null; } } // EMPTY_ARRAY (array()) if ($type & self::TYPE_EMPTY_ARRAY) { - if (is_array($value) && ($value == [])) { + if (is_array($value) && $value === []) { return null; } } // INTEGER (0) if ($type & self::TYPE_INTEGER) { - if (is_int($value) && ($value == 0)) { + if (is_int($value) && $value === 0) { return null; } } // BOOLEAN (false) if ($type & self::TYPE_BOOLEAN) { - if (is_bool($value) && ($value == false)) { + if (is_bool($value) && $value === false) { return null; } } diff --git a/src/UriNormalize.php b/src/UriNormalize.php index bb9f6372..5b5a7aa1 100644 --- a/src/UriNormalize.php +++ b/src/UriNormalize.php @@ -98,7 +98,7 @@ public function filter($value) try { $uri = UriFactory::factory($value, $defaultScheme); - if ($this->enforcedScheme && (! $uri->getScheme())) { + if ($this->enforcedScheme && ! $uri->getScheme()) { $this->enforceScheme($uri); } } catch (UriException $ex) { From 18a066a02e3774145ff1c3f67d3fa7e2deb6a8bb Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 16 Aug 2019 20:19:18 +0200 Subject: [PATCH 03/16] Remove redundant spaces --- src/Boolean.php | 22 +++++++++++----------- src/Compress/Tar.php | 10 +++++----- src/DataUnitFormatter.php | 8 ++++---- src/Encrypt/BlockCipher.php | 6 +++--- src/ToNull.php | 14 +++++++------- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/Boolean.php b/src/Boolean.php index d8c9eb60..1408c575 100644 --- a/src/Boolean.php +++ b/src/Boolean.php @@ -14,17 +14,17 @@ class Boolean extends AbstractFilter { - const TYPE_BOOLEAN = 1; - const TYPE_INTEGER = 2; - const TYPE_FLOAT = 4; - const TYPE_STRING = 8; - const TYPE_ZERO_STRING = 16; - const TYPE_EMPTY_ARRAY = 32; - const TYPE_NULL = 64; - const TYPE_PHP = 127; - const TYPE_FALSE_STRING = 128; - const TYPE_LOCALIZED = 256; - const TYPE_ALL = 511; + const TYPE_BOOLEAN = 1; + const TYPE_INTEGER = 2; + const TYPE_FLOAT = 4; + const TYPE_STRING = 8; + const TYPE_ZERO_STRING = 16; + const TYPE_EMPTY_ARRAY = 32; + const TYPE_NULL = 64; + const TYPE_PHP = 127; + const TYPE_FALSE_STRING = 128; + const TYPE_LOCALIZED = 256; + const TYPE_ALL = 511; /** * @var array diff --git a/src/Compress/Tar.php b/src/Compress/Tar.php index 4bc0b960..b491774a 100644 --- a/src/Compress/Tar.php +++ b/src/Compress/Tar.php @@ -22,16 +22,16 @@ class Tar extends AbstractCompressionAlgorithm /** * Compression Options * array( - * 'archive' => Archive to use - * 'target' => Target to write the files to + * 'archive' => Archive to use + * 'target' => Target to write the files to * ) * * @var array */ protected $options = [ - 'archive' => null, - 'target' => '.', - 'mode' => null, + 'archive' => null, + 'target' => '.', + 'mode' => null, ]; /** diff --git a/src/DataUnitFormatter.php b/src/DataUnitFormatter.php index 676bf0da..cd1c1597 100644 --- a/src/DataUnitFormatter.php +++ b/src/DataUnitFormatter.php @@ -48,10 +48,10 @@ final class DataUnitFormatter extends AbstractFilter * @var array */ protected $options = [ - 'mode' => self::MODE_DECIMAL, - 'unit' => '', - 'precision' => 2, - 'prefixes' => [], + 'mode' => self::MODE_DECIMAL, + 'unit' => '', + 'precision' => 2, + 'prefixes' => [], ]; /** diff --git a/src/Encrypt/BlockCipher.php b/src/Encrypt/BlockCipher.php index 68284c3b..f15b6453 100644 --- a/src/Encrypt/BlockCipher.php +++ b/src/Encrypt/BlockCipher.php @@ -34,9 +34,9 @@ class BlockCipher implements EncryptionAlgorithmInterface * ) */ protected $encryption = [ - 'key_iteration' => 5000, - 'algorithm' => 'aes', - 'hash' => 'sha256', + 'key_iteration' => 5000, + 'algorithm' => 'aes', + 'hash' => 'sha256', ]; /** diff --git a/src/ToNull.php b/src/ToNull.php index efb0edcb..77135820 100644 --- a/src/ToNull.php +++ b/src/ToNull.php @@ -13,13 +13,13 @@ class ToNull extends AbstractFilter { - const TYPE_BOOLEAN = 1; - const TYPE_INTEGER = 2; - const TYPE_EMPTY_ARRAY = 4; - const TYPE_STRING = 8; - const TYPE_ZERO_STRING = 16; - const TYPE_FLOAT = 32; - const TYPE_ALL = 63; + const TYPE_BOOLEAN = 1; + const TYPE_INTEGER = 2; + const TYPE_EMPTY_ARRAY = 4; + const TYPE_STRING = 8; + const TYPE_ZERO_STRING = 16; + const TYPE_FLOAT = 32; + const TYPE_ALL = 63; /** * @var array From 53305717e0e1c42c6dfe5ecadc6dc6f604fbaba4 Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 16 Aug 2019 20:20:15 +0200 Subject: [PATCH 04/16] Single quotes instead of double --- src/Compress/Gz.php | 4 ++-- src/Compress/Rar.php | 2 +- src/Compress/Tar.php | 2 +- src/Compress/Zip.php | 2 +- src/Encrypt/Openssl.php | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Compress/Gz.php b/src/Compress/Gz.php index 9411cb3e..65137405 100644 --- a/src/Compress/Gz.php +++ b/src/Compress/Gz.php @@ -172,14 +172,14 @@ public function decompress($content) } if (file_exists($archive)) { - $handler = fopen($archive, "rb"); + $handler = fopen($archive, 'rb'); if (! $handler) { throw new Exception\RuntimeException("Error opening the archive '" . $archive . "'"); } fseek($handler, -4, SEEK_END); $packet = fread($handler, 4); - $bytes = unpack("V", $packet); + $bytes = unpack('V', $packet); $size = end($bytes); fclose($handler); diff --git a/src/Compress/Rar.php b/src/Compress/Rar.php index 6c145040..b804661c 100644 --- a/src/Compress/Rar.php +++ b/src/Compress/Rar.php @@ -197,7 +197,7 @@ public function decompress($content) } if (! $archive) { - throw new Exception\RuntimeException("Error opening the RAR Archive"); + throw new Exception\RuntimeException('Error opening the RAR Archive'); } $target = $this->getTarget(); diff --git a/src/Compress/Tar.php b/src/Compress/Tar.php index b491774a..3aa1e539 100644 --- a/src/Compress/Tar.php +++ b/src/Compress/Tar.php @@ -158,7 +158,7 @@ public function compress($content) if (! file_exists($content)) { $file = $this->getTarget(); if (is_dir($file)) { - $file .= DIRECTORY_SEPARATOR . "tar.tmp"; + $file .= DIRECTORY_SEPARATOR . 'tar.tmp'; } $result = file_put_contents($file, $content); diff --git a/src/Compress/Zip.php b/src/Compress/Zip.php index 157eda78..48e8b8ea 100644 --- a/src/Compress/Zip.php +++ b/src/Compress/Zip.php @@ -161,7 +161,7 @@ public function compress($content) if (! is_dir($file)) { $file = basename($file); } else { - $file = "zip.tmp"; + $file = 'zip.tmp'; } $res = $zip->addFromString($file, $content); diff --git a/src/Encrypt/Openssl.php b/src/Encrypt/Openssl.php index ec19e910..7054f3d3 100644 --- a/src/Encrypt/Openssl.php +++ b/src/Encrypt/Openssl.php @@ -403,7 +403,7 @@ public function encrypt($value) */ public function decrypt($value) { - $decrypted = ""; + $decrypted = ''; $envelope = current($this->getEnvelopeKey()); if (count($this->keys['private']) !== 1) { From 34b347a635b01d4ec35ec0799a296c54016b761e Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 16 Aug 2019 20:21:54 +0200 Subject: [PATCH 05/16] Change logical operator "and" to "&&" --- src/Encrypt/Openssl.php | 2 +- src/File/Decrypt.php | 2 +- src/File/Encrypt.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Encrypt/Openssl.php b/src/Encrypt/Openssl.php index 7054f3d3..95e57046 100644 --- a/src/Encrypt/Openssl.php +++ b/src/Encrypt/Openssl.php @@ -116,7 +116,7 @@ protected function _setKeys($keys) } foreach ($keys as $type => $key) { - if (is_file($key) and is_readable($key)) { + if (is_file($key) && is_readable($key)) { $file = fopen($key, 'r'); $cert = fread($file, 8192); fclose($file); diff --git a/src/File/Decrypt.php b/src/File/Decrypt.php index d6ef8985..cdcbf793 100644 --- a/src/File/Decrypt.php +++ b/src/File/Decrypt.php @@ -82,7 +82,7 @@ public function filter($value) $this->filename = $value; } - if (file_exists($this->filename) and ! is_writable($this->filename)) { + if (file_exists($this->filename) && ! is_writable($this->filename)) { throw new Exception\RuntimeException("File '{$this->filename}' is not writable"); } diff --git a/src/File/Encrypt.php b/src/File/Encrypt.php index e8554b01..10c7afde 100644 --- a/src/File/Encrypt.php +++ b/src/File/Encrypt.php @@ -82,7 +82,7 @@ public function filter($value) $this->filename = $value; } - if (file_exists($this->filename) and ! is_writable($this->filename)) { + if (file_exists($this->filename) && ! is_writable($this->filename)) { throw new Exception\RuntimeException("File '{$this->filename}' is not writable"); } From 3baa3b1fc21a105859b7a27b4f164aed40fecf59 Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 16 Aug 2019 20:23:39 +0200 Subject: [PATCH 06/16] PHPDoc fixes, removed redundant constructor comments --- src/Boolean.php | 4 ++-- src/Callback.php | 2 +- src/Compress/AbstractCompressionAlgorithm.php | 2 +- src/Encrypt/Openssl.php | 4 ++-- src/ToFloat.php | 2 +- src/ToInt.php | 2 +- src/ToNull.php | 4 ++-- src/UriNormalize.php | 4 ++-- src/Word/AbstractSeparator.php | 2 +- src/Word/CamelCaseToDash.php | 4 ---- src/Word/CamelCaseToUnderscore.php | 4 ---- src/Word/DashToCamelCase.php | 4 ---- src/Word/DashToUnderscore.php | 3 --- src/Word/UnderscoreToCamelCase.php | 4 ---- src/Word/UnderscoreToDash.php | 4 ---- 15 files changed, 13 insertions(+), 36 deletions(-) diff --git a/src/Boolean.php b/src/Boolean.php index 1408c575..577614c6 100644 --- a/src/Boolean.php +++ b/src/Boolean.php @@ -190,8 +190,8 @@ public function getTranslations() * * Returns a boolean representation of $value * - * @param string $value - * @return string + * @param null|array|bool|float|int|string $value + * @return bool|mixed */ public function filter($value) { diff --git a/src/Callback.php b/src/Callback.php index 8602ff2c..d03453f3 100644 --- a/src/Callback.php +++ b/src/Callback.php @@ -22,7 +22,7 @@ class Callback extends AbstractFilter ]; /** - * @param callable|array|Traversable $callbackOrOptions + * @param callable|array|string|Traversable $callbackOrOptions * @param array $callbackParams */ public function __construct($callbackOrOptions = [], $callbackParams = []) diff --git a/src/Compress/AbstractCompressionAlgorithm.php b/src/Compress/AbstractCompressionAlgorithm.php index c62539cb..d852ee24 100644 --- a/src/Compress/AbstractCompressionAlgorithm.php +++ b/src/Compress/AbstractCompressionAlgorithm.php @@ -41,7 +41,7 @@ public function __construct($options = null) /** * Returns one or all set options * - * @param string $option (Optional) Option to return + * @param string|null $option Option to return * @return mixed */ public function getOptions($option = null) diff --git a/src/Encrypt/Openssl.php b/src/Encrypt/Openssl.php index 95e57046..3ee6c3a7 100644 --- a/src/Encrypt/Openssl.php +++ b/src/Encrypt/Openssl.php @@ -202,8 +202,8 @@ public function getPrivateKey() /** * Sets private keys * - * @param string $key Private key - * @param string $passphrase + * @param string|array $key Private key + * @param string|null $passphrase * @return self */ public function setPrivateKey($key, $passphrase = null) diff --git a/src/ToFloat.php b/src/ToFloat.php index e73d720c..fd90eb8b 100644 --- a/src/ToFloat.php +++ b/src/ToFloat.php @@ -16,7 +16,7 @@ class ToFloat extends AbstractFilter * * If the value provided is non-scalar, the value will remain unfiltered * - * @param scalar $value + * @param mixed $value * @return float|mixed */ public function filter($value) diff --git a/src/ToInt.php b/src/ToInt.php index 557c3f43..bf3c95a6 100644 --- a/src/ToInt.php +++ b/src/ToInt.php @@ -18,7 +18,7 @@ class ToInt extends AbstractFilter * * If the value provided is non-scalar, the value will remain unfiltered * - * @param string $value + * @param mixed $value * @return int|mixed */ public function filter($value) diff --git a/src/ToNull.php b/src/ToNull.php index 77135820..170f33a0 100644 --- a/src/ToNull.php +++ b/src/ToNull.php @@ -117,8 +117,8 @@ public function getType() * Returns null representation of $value, if value is empty and matches * types that should be considered null. * - * @param string $value - * @return string + * @param null|array|bool|float|int|string $value + * @return null|mixed */ public function filter($value) { diff --git a/src/UriNormalize.php b/src/UriNormalize.php index 5b5a7aa1..a66f0fa7 100644 --- a/src/UriNormalize.php +++ b/src/UriNormalize.php @@ -18,14 +18,14 @@ class UriNormalize extends AbstractFilter /** * The default scheme to use when parsing scheme-less URIs * - * @var string + * @var string|null */ protected $defaultScheme = null; /** * Enforced scheme for scheme-less URIs. See setEnforcedScheme docs for info * - * @var string + * @var string|null */ protected $enforcedScheme = null; diff --git a/src/Word/AbstractSeparator.php b/src/Word/AbstractSeparator.php index 94313bc0..659f0c13 100644 --- a/src/Word/AbstractSeparator.php +++ b/src/Word/AbstractSeparator.php @@ -19,7 +19,7 @@ abstract class AbstractSeparator extends AbstractFilter /** * Constructor * - * @param string $separator Space by default + * @param array|string $separator Space by default */ public function __construct($separator = ' ') { diff --git a/src/Word/CamelCaseToDash.php b/src/Word/CamelCaseToDash.php index ce31683b..2a02d44d 100644 --- a/src/Word/CamelCaseToDash.php +++ b/src/Word/CamelCaseToDash.php @@ -11,10 +11,6 @@ class CamelCaseToDash extends CamelCaseToSeparator { - /** - * Constructor - * - */ public function __construct() { parent::__construct('-'); diff --git a/src/Word/CamelCaseToUnderscore.php b/src/Word/CamelCaseToUnderscore.php index 82507847..0d6c4292 100644 --- a/src/Word/CamelCaseToUnderscore.php +++ b/src/Word/CamelCaseToUnderscore.php @@ -11,10 +11,6 @@ class CamelCaseToUnderscore extends CamelCaseToSeparator { - /** - * Constructor - * - */ public function __construct() { parent::__construct('_'); diff --git a/src/Word/DashToCamelCase.php b/src/Word/DashToCamelCase.php index 6bf36f87..7aaf1562 100644 --- a/src/Word/DashToCamelCase.php +++ b/src/Word/DashToCamelCase.php @@ -11,10 +11,6 @@ class DashToCamelCase extends SeparatorToCamelCase { - /** - * Constructor - * - */ public function __construct() { parent::__construct('-'); diff --git a/src/Word/DashToUnderscore.php b/src/Word/DashToUnderscore.php index 104dedc3..be8b68a3 100644 --- a/src/Word/DashToUnderscore.php +++ b/src/Word/DashToUnderscore.php @@ -11,9 +11,6 @@ class DashToUnderscore extends SeparatorToSeparator { - /** - * Constructor - */ public function __construct() { parent::__construct('-', '_'); diff --git a/src/Word/UnderscoreToCamelCase.php b/src/Word/UnderscoreToCamelCase.php index 0c6c5be6..51271a01 100644 --- a/src/Word/UnderscoreToCamelCase.php +++ b/src/Word/UnderscoreToCamelCase.php @@ -11,10 +11,6 @@ class UnderscoreToCamelCase extends SeparatorToCamelCase { - /** - * Constructor - * - */ public function __construct() { parent::__construct('_'); diff --git a/src/Word/UnderscoreToDash.php b/src/Word/UnderscoreToDash.php index 816d3557..60b03650 100644 --- a/src/Word/UnderscoreToDash.php +++ b/src/Word/UnderscoreToDash.php @@ -11,10 +11,6 @@ class UnderscoreToDash extends SeparatorToSeparator { - /** - * Constructor - * - */ public function __construct() { parent::__construct('_', '-'); From 49ec69e302f6e90f4695ac467df4e870895bdba7 Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 16 Aug 2019 20:24:13 +0200 Subject: [PATCH 07/16] Replace call_user_func with function call --- src/Compress/Rar.php | 2 +- src/Word/UnderscoreToStudlyCase.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Compress/Rar.php b/src/Compress/Rar.php index b804661c..43a453fc 100644 --- a/src/Compress/Rar.php +++ b/src/Compress/Rar.php @@ -166,7 +166,7 @@ public function compress($content) $options = $this->getOptions(); unset($options['callback']); - $result = call_user_func($callback, $options, $content); + $result = $callback($options, $content); if ($result !== true) { throw new Exception\RuntimeException('Error compressing the RAR Archive'); } diff --git a/src/Word/UnderscoreToStudlyCase.php b/src/Word/UnderscoreToStudlyCase.php index 3c1f0de4..a640d9ef 100755 --- a/src/Word/UnderscoreToStudlyCase.php +++ b/src/Word/UnderscoreToStudlyCase.php @@ -31,6 +31,6 @@ public function filter($value) }; } - return is_array($value) ? array_map($lowerCaseFirst, $value) : call_user_func($lowerCaseFirst, $value); + return is_array($value) ? array_map($lowerCaseFirst, $value) : $lowerCaseFirst($value); } } From 85b99b20ab2feb23a653296706c3ffef40485144 Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 16 Aug 2019 20:24:48 +0200 Subject: [PATCH 08/16] Use isset instead of array_key_exists isset is faster, result in that case is exactly the same --- src/Compress/AbstractCompressionAlgorithm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compress/AbstractCompressionAlgorithm.php b/src/Compress/AbstractCompressionAlgorithm.php index d852ee24..991652d1 100644 --- a/src/Compress/AbstractCompressionAlgorithm.php +++ b/src/Compress/AbstractCompressionAlgorithm.php @@ -50,7 +50,7 @@ public function getOptions($option = null) return $this->options; } - if (! array_key_exists($option, $this->options)) { + if (! isset($this->options[$option])) { return null; } From e8174761302c3b45aa3d038f95d2645182467a9c Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 16 Aug 2019 20:25:30 +0200 Subject: [PATCH 09/16] Use array assignment instead of array_push --- src/Compress/Zip.php | 2 +- src/RealPath.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Compress/Zip.php b/src/Compress/Zip.php index 48e8b8ea..eca0e7fb 100644 --- a/src/Compress/Zip.php +++ b/src/Compress/Zip.php @@ -132,7 +132,7 @@ public function compress($content) } if (is_dir($current . $node)) { - array_push($stack, $current . $node . DIRECTORY_SEPARATOR); + $stack[] = $current . $node . DIRECTORY_SEPARATOR; } if (is_file($current . $node)) { diff --git a/src/RealPath.php b/src/RealPath.php index fe312aaf..897625e4 100644 --- a/src/RealPath.php +++ b/src/RealPath.php @@ -112,7 +112,7 @@ public function filter($value) if ($dir === '..') { array_pop($stack); } else { - array_push($stack, $dir); + $stack[] = $dir; } } } From a053f87424acf8794fdb7bdec7f9f804624ab5ff Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 16 Aug 2019 20:25:49 +0200 Subject: [PATCH 10/16] Return value directly, without assigning variable --- src/Encrypt/Openssl.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Encrypt/Openssl.php b/src/Encrypt/Openssl.php index 3ee6c3a7..56d4fe9f 100644 --- a/src/Encrypt/Openssl.php +++ b/src/Encrypt/Openssl.php @@ -162,8 +162,7 @@ protected function _setKeys($keys) */ public function getPublicKey() { - $key = $this->keys['public']; - return $key; + return $this->keys['public']; } /** @@ -195,8 +194,7 @@ public function setPublicKey($key) */ public function getPrivateKey() { - $key = $this->keys['private']; - return $key; + return $this->keys['private']; } /** @@ -233,8 +231,7 @@ public function setPrivateKey($key, $passphrase = null) */ public function getEnvelopeKey() { - $key = $this->keys['envelope']; - return $key; + return $this->keys['envelope']; } /** From aec865541ba015e9121baee875958d98ae3dd046 Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 16 Aug 2019 20:27:32 +0200 Subject: [PATCH 11/16] Use static callbacks micro-optimisation --- src/Word/SeparatorToCamelCase.php | 18 ++++++++++++------ src/Word/UnderscoreToStudlyCase.php | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Word/SeparatorToCamelCase.php b/src/Word/SeparatorToCamelCase.php index 712e6b5e..b7086183 100644 --- a/src/Word/SeparatorToCamelCase.php +++ b/src/Word/SeparatorToCamelCase.php @@ -35,21 +35,25 @@ public function filter($value) ]; if (! extension_loaded('mbstring')) { $replacements = [ - function ($matches) { + // @codingStandardsIgnoreStart + static function ($matches) { return strtoupper($matches[2]); }, - function ($matches) { + static function ($matches) { return strtoupper($matches[1]); }, + // @codingStandardsIgnoreEnd ]; } else { $replacements = [ - function ($matches) { + // @codingStandardsIgnoreStart + static function ($matches) { return mb_strtoupper($matches[2], 'UTF-8'); }, - function ($matches) { + static function ($matches) { return mb_strtoupper($matches[1], 'UTF-8'); }, + // @codingStandardsIgnoreEnd ]; } } else { @@ -58,12 +62,14 @@ function ($matches) { '#(^[\S]{1})#', ]; $replacements = [ - function ($matches) { + // @codingStandardsIgnoreStart + static function ($matches) { return strtoupper($matches[2]); }, - function ($matches) { + static function ($matches) { return strtoupper($matches[1]); }, + // @codingStandardsIgnoreEnd ]; } diff --git a/src/Word/UnderscoreToStudlyCase.php b/src/Word/UnderscoreToStudlyCase.php index a640d9ef..ae9e9f4a 100755 --- a/src/Word/UnderscoreToStudlyCase.php +++ b/src/Word/UnderscoreToStudlyCase.php @@ -22,7 +22,7 @@ public function filter($value) $lowerCaseFirst = 'lcfirst'; if (StringUtils::hasPcreUnicodeSupport() && extension_loaded('mbstring')) { - $lowerCaseFirst = function ($value) { + $lowerCaseFirst = static function ($value) { if (0 === mb_strlen($value)) { return $value; } From ada203c67a47dd778fc8b0ead2a07646d28adfaa Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 16 Aug 2019 20:27:46 +0200 Subject: [PATCH 12/16] Removed default null value on properties --- src/Inflector.php | 4 ++-- src/UriNormalize.php | 4 ++-- src/Word/SeparatorToSeparator.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Inflector.php b/src/Inflector.php index 3dfcbdf8..8be35491 100644 --- a/src/Inflector.php +++ b/src/Inflector.php @@ -21,12 +21,12 @@ class Inflector extends AbstractFilter /** * @var FilterPluginManager */ - protected $pluginManager = null; + protected $pluginManager; /** * @var string */ - protected $target = null; + protected $target; /** * @var bool diff --git a/src/UriNormalize.php b/src/UriNormalize.php index a66f0fa7..8df3ff91 100644 --- a/src/UriNormalize.php +++ b/src/UriNormalize.php @@ -20,14 +20,14 @@ class UriNormalize extends AbstractFilter * * @var string|null */ - protected $defaultScheme = null; + protected $defaultScheme; /** * Enforced scheme for scheme-less URIs. See setEnforcedScheme docs for info * * @var string|null */ - protected $enforcedScheme = null; + protected $enforcedScheme; /** * Sets filter options diff --git a/src/Word/SeparatorToSeparator.php b/src/Word/SeparatorToSeparator.php index ccdbb492..776d59ee 100644 --- a/src/Word/SeparatorToSeparator.php +++ b/src/Word/SeparatorToSeparator.php @@ -14,8 +14,8 @@ class SeparatorToSeparator extends AbstractFilter { - protected $searchSeparator = null; - protected $replacementSeparator = null; + protected $searchSeparator; + protected $replacementSeparator; /** * Constructor From 44757574c46dc7deabe0b8b55bd9d6e9f8d2f588 Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 16 Aug 2019 20:28:58 +0200 Subject: [PATCH 13/16] Remove redundant floatval call when casting to float --- src/ToFloat.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ToFloat.php b/src/ToFloat.php index fd90eb8b..e54c76ec 100644 --- a/src/ToFloat.php +++ b/src/ToFloat.php @@ -25,6 +25,6 @@ public function filter($value) return $value; } - return (float) floatval($value); + return (float) $value; } } From 7968561fe8959254cae452613954ce6c4e6c7358 Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 16 Aug 2019 20:29:12 +0200 Subject: [PATCH 14/16] Operator optimisation --- src/Encrypt/BlockCipher.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Encrypt/BlockCipher.php b/src/Encrypt/BlockCipher.php index f15b6453..be384b95 100644 --- a/src/Encrypt/BlockCipher.php +++ b/src/Encrypt/BlockCipher.php @@ -118,7 +118,7 @@ public function setEncryption($options) throw new Exception\InvalidArgumentException('Invalid options argument provided to filter'); } - $options = $options + $this->encryption; + $options += $this->encryption; if (isset($options['key'])) { $this->blockCipher->setKey($options['key']); From 7a8c7ba94004366aa8d6ba5d8754ab4c7ee21e6e Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 16 Aug 2019 20:29:28 +0200 Subject: [PATCH 15/16] Add 2nd param to uniqid function call --- src/File/Rename.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/File/Rename.php b/src/File/Rename.php index bd7e92c3..8c21566c 100644 --- a/src/File/Rename.php +++ b/src/File/Rename.php @@ -329,7 +329,7 @@ protected function _getFileName($file) if ($rename['randomize']) { $info = pathinfo($rename['target']); $newTarget = $info['dirname'] . DIRECTORY_SEPARATOR . - $info['filename'] . uniqid('_'); + $info['filename'] . uniqid('_', true); if (isset($info['extension'])) { $newTarget .= '.' . $info['extension']; } From 02ec060793d565477458ea50869e20dc5e7d2685 Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 16 Aug 2019 22:26:07 +0200 Subject: [PATCH 16/16] Change 2nd argument to false in uniqid (more_entropy=false) One test failed with this changes, so to keep compatibility it's reverted. --- src/File/Rename.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/File/Rename.php b/src/File/Rename.php index 8c21566c..f3ba4728 100644 --- a/src/File/Rename.php +++ b/src/File/Rename.php @@ -329,7 +329,7 @@ protected function _getFileName($file) if ($rename['randomize']) { $info = pathinfo($rename['target']); $newTarget = $info['dirname'] . DIRECTORY_SEPARATOR . - $info['filename'] . uniqid('_', true); + $info['filename'] . uniqid('_', false); if (isset($info['extension'])) { $newTarget .= '.' . $info['extension']; }