Call mbstring functions without getting encoding

The functions from the mbstring can deal find with strings without
forcing an specific encoding. However, sometimes "mb_detect_encoding()"
cannot identify the encoding therefore the functions that expect a valid
encoding will trigger a PHP error.

This commit will remove the unnecessary use of "mb_detect_encoding()."

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2021-02-06 17:42:11 +01:00
parent 3dcd859d98
commit 51ad23e24c
No known key found for this signature in database
GPG key ID: 221E9281655813A6
8 changed files with 12 additions and 32 deletions

View file

@ -54,8 +54,6 @@ final class Charset extends AbstractRule
*/
public function validate($input): bool
{
$detectedEncoding = mb_detect_encoding($input, $this->charset, true);
return in_array($detectedEncoding, $this->charset, true);
return in_array(mb_detect_encoding($input, $this->charset, true), $this->charset, true);
}
}

View file

@ -16,7 +16,6 @@ namespace Respect\Validation\Rules;
use function in_array;
use function is_array;
use function is_scalar;
use function mb_detect_encoding;
use function mb_stripos;
use function mb_strpos;
@ -74,11 +73,10 @@ final class Contains extends AbstractRule
return false;
}
$encoding = (string) mb_detect_encoding($haystack);
if ($this->identical) {
return mb_strpos($haystack, $needle, 0, $encoding) !== false;
return mb_strpos($haystack, $needle) !== false;
}
return mb_stripos($haystack, $needle, 0, $encoding) !== false;
return mb_stripos($haystack, $needle) !== false;
}
}

View file

@ -15,7 +15,6 @@ namespace Respect\Validation\Rules;
use function end;
use function is_array;
use function mb_detect_encoding;
use function mb_strlen;
use function mb_strripos;
use function mb_strrpos;
@ -70,10 +69,7 @@ final class EndsWith extends AbstractRule
return end($input) == $this->endValue;
}
$encoding = (string) mb_detect_encoding($input);
$endPosition = mb_strlen($input, $encoding) - mb_strlen($this->endValue, $encoding);
return mb_strripos($input, $this->endValue, 0, $encoding) === $endPosition;
return mb_strripos($input, $this->endValue) === mb_strlen($input) - mb_strlen($this->endValue);
}
/**
@ -85,9 +81,6 @@ final class EndsWith extends AbstractRule
return end($input) === $this->endValue;
}
$encoding = (string) mb_detect_encoding($input);
$endPosition = mb_strlen($input, $encoding) - mb_strlen($this->endValue, $encoding);
return mb_strrpos($input, $this->endValue, 0, $encoding) === $endPosition;
return mb_strrpos($input, $this->endValue) === mb_strlen($input) - mb_strlen($this->endValue);
}
}

View file

@ -15,7 +15,6 @@ namespace Respect\Validation\Rules;
use function in_array;
use function is_array;
use function mb_detect_encoding;
use function mb_stripos;
use function mb_strpos;
@ -74,9 +73,7 @@ final class In extends AbstractRule
return $input == $this->haystack;
}
$inputString = (string) $input;
return mb_stripos($this->haystack, $inputString, 0, (string) mb_detect_encoding($inputString)) !== false;
return mb_stripos($this->haystack, (string) $input) !== false;
}
/**
@ -92,8 +89,6 @@ final class In extends AbstractRule
return $input === $this->haystack;
}
$inputString = (string) $input;
return mb_strpos($this->haystack, $inputString, 0, (string) mb_detect_encoding($inputString)) !== false;
return mb_strpos($this->haystack, (string) $input) !== false;
}
}

View file

@ -22,7 +22,6 @@ use function is_array;
use function is_int;
use function is_object;
use function is_string;
use function mb_detect_encoding;
use function mb_strlen;
use function sprintf;
@ -89,7 +88,7 @@ final class Length extends AbstractRule
private function extractLength($input): ?int
{
if (is_string($input)) {
return (int) mb_strlen($input, (string) mb_detect_encoding($input));
return (int) mb_strlen($input);
}
if (is_array($input) || $input instanceof CountableInterface) {

View file

@ -14,7 +14,6 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use function is_string;
use function mb_detect_encoding;
use function mb_strtolower;
/**
@ -36,6 +35,6 @@ final class Lowercase extends AbstractRule
return false;
}
return $input === mb_strtolower($input, (string) mb_detect_encoding($input));
return $input === mb_strtolower($input);
}
}

View file

@ -14,7 +14,6 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use function is_array;
use function mb_detect_encoding;
use function mb_stripos;
use function mb_strpos;
use function reset;
@ -68,7 +67,7 @@ final class StartsWith extends AbstractRule
return reset($input) == $this->startValue;
}
return mb_stripos($input, $this->startValue, 0, (string) mb_detect_encoding($input)) === 0;
return mb_stripos($input, $this->startValue) === 0;
}
/**
@ -80,6 +79,6 @@ final class StartsWith extends AbstractRule
return reset($input) === $this->startValue;
}
return mb_strpos($input, $this->startValue, 0, (string) mb_detect_encoding($input)) === 0;
return mb_strpos($input, $this->startValue) === 0;
}
}

View file

@ -14,7 +14,6 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use function is_string;
use function mb_detect_encoding;
use function mb_strtoupper;
/**
@ -36,6 +35,6 @@ final class Uppercase extends AbstractRule
return false;
}
return $input === mb_strtoupper($input, (string) mb_detect_encoding($input));
return $input === mb_strtoupper($input);
}
}