Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'improvements' into develop
Browse files Browse the repository at this point in the history
Forward port #91
  • Loading branch information
michalbundyra committed Aug 16, 2019
2 parents 7016f33 + 02ec060 commit 4f4fac8
Show file tree
Hide file tree
Showing 35 changed files with 147 additions and 167 deletions.
4 changes: 2 additions & 2 deletions src/AbstractDateDropdown.php
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractUnicode.php
Expand Up @@ -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
Expand Down
54 changes: 27 additions & 27 deletions src/Boolean.php
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)
{
Expand 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;
}
}
Expand All @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Callback.php
Expand Up @@ -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 = [])
Expand Down
2 changes: 1 addition & 1 deletion src/Compress.php
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/Compress/AbstractCompressionAlgorithm.php
Expand Up @@ -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)
Expand All @@ -50,8 +50,8 @@ public function getOptions($option = null)
return $this->options;
}

if (! array_key_exists($option, $this->options)) {
return;
if (! isset($this->options[$option])) {
return null;
}

return $this->options[$option];
Expand Down
10 changes: 5 additions & 5 deletions src/Compress/Gz.php
Expand Up @@ -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');
}

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -172,21 +172,21 @@ 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);

$file = gzopen($archive, 'r');
$compressed = gzread($file, $size);
gzclose($file);
} elseif ($mode == 'deflate') {
} elseif ($mode === 'deflate') {
$compressed = gzinflate($content);
} else {
$compressed = gzuncompress($content);
Expand Down
4 changes: 2 additions & 2 deletions src/Compress/Rar.php
Expand Up @@ -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');
}
Expand Down Expand Up @@ -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();
Expand Down
18 changes: 9 additions & 9 deletions src/Compress/Tar.php
Expand Up @@ -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,
];

/**
Expand Down Expand Up @@ -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');
}

Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/Compress/Zip.php
Expand Up @@ -127,12 +127,12 @@ public function compress($content)

$dir = dir($current);
while (false !== ($node = $dir->read())) {
if (($node == '.') || ($node == '..')) {
if ($node === '.' || $node === '..') {
continue;
}

if (is_dir($current . $node)) {
array_push($stack, $current . $node . DIRECTORY_SEPARATOR);
$stack[] = $current . $node . DIRECTORY_SEPARATOR;
}

if (is_file($current . $node)) {
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions src/DataUnitFormatter.php
Expand Up @@ -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' => [],
];

/**
Expand Down Expand Up @@ -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;
Expand All @@ -103,7 +103,7 @@ protected function getMode()
*/
protected function isDecimalMode()
{
return $this->getMode() == self::MODE_DECIMAL;
return $this->getMode() === self::MODE_DECIMAL;
}

/**
Expand All @@ -113,7 +113,7 @@ protected function isDecimalMode()
*/
protected function isBinaryMode()
{
return $this->getMode() == self::MODE_BINARY;
return $this->getMode() === self::MODE_BINARY;
}

/**
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Digits.php
Expand Up @@ -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;
Expand Down

0 comments on commit 4f4fac8

Please sign in to comment.