From fd143bbc15643f577e14e8f450d6b79abd426cd9 Mon Sep 17 00:00:00 2001 From: BackEndTea Date: Mon, 1 Jul 2019 19:43:13 +0200 Subject: [PATCH] Prefix all function calls with a \ This means PHP won't search the current namespace for a function first It also means PHP can use the Opcache (opcode?) for some functions, which is the actual speed increase. If possible i'd use the `use function` syntax, but that became available in php 5.6, and thus isn't an option atm. --- src/Assert.php | 368 ++++++++++++++++++++++++------------------------- 1 file changed, 184 insertions(+), 184 deletions(-) diff --git a/src/Assert.php b/src/Assert.php index 53802337..9c9e9475 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -190,8 +190,8 @@ class Assert { public static function string($value, $message = '') { - if (!is_string($value)) { - static::reportInvalidArgument(sprintf( + if (!\is_string($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a string. Got: %s', static::typeToString($value) )); @@ -206,8 +206,8 @@ public static function stringNotEmpty($value, $message = '') public static function integer($value, $message = '') { - if (!is_int($value)) { - static::reportInvalidArgument(sprintf( + if (!\is_int($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected an integer. Got: %s', static::typeToString($value) )); @@ -216,8 +216,8 @@ public static function integer($value, $message = '') public static function integerish($value, $message = '') { - if (!is_numeric($value) || $value != (int) $value) { - static::reportInvalidArgument(sprintf( + if (!\is_numeric($value) || $value != (int) $value) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected an integerish value. Got: %s', static::typeToString($value) )); @@ -226,8 +226,8 @@ public static function integerish($value, $message = '') public static function float($value, $message = '') { - if (!is_float($value)) { - static::reportInvalidArgument(sprintf( + if (!\is_float($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a float. Got: %s', static::typeToString($value) )); @@ -236,8 +236,8 @@ public static function float($value, $message = '') public static function numeric($value, $message = '') { - if (!is_numeric($value)) { - static::reportInvalidArgument(sprintf( + if (!\is_numeric($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a numeric. Got: %s', static::typeToString($value) )); @@ -246,8 +246,8 @@ public static function numeric($value, $message = '') public static function natural($value, $message = '') { - if (!is_int($value) || $value < 0) { - static::reportInvalidArgument(sprintf( + if (!\is_int($value) || $value < 0) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a non-negative integer. Got %s', static::valueToString($value) )); @@ -256,8 +256,8 @@ public static function natural($value, $message = '') public static function boolean($value, $message = '') { - if (!is_bool($value)) { - static::reportInvalidArgument(sprintf( + if (!\is_bool($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a boolean. Got: %s', static::typeToString($value) )); @@ -266,8 +266,8 @@ public static function boolean($value, $message = '') public static function scalar($value, $message = '') { - if (!is_scalar($value)) { - static::reportInvalidArgument(sprintf( + if (!\is_scalar($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a scalar. Got: %s', static::typeToString($value) )); @@ -276,8 +276,8 @@ public static function scalar($value, $message = '') public static function object($value, $message = '') { - if (!is_object($value)) { - static::reportInvalidArgument(sprintf( + if (!\is_object($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected an object. Got: %s', static::typeToString($value) )); @@ -286,15 +286,15 @@ public static function object($value, $message = '') public static function resource($value, $type = null, $message = '') { - if (!is_resource($value)) { - static::reportInvalidArgument(sprintf( + if (!\is_resource($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a resource. Got: %s', static::typeToString($value) )); } - if ($type && $type !== get_resource_type($value)) { - static::reportInvalidArgument(sprintf( + if ($type && $type !== \get_resource_type($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a resource of type %2$s. Got: %s', static::typeToString($value), $type @@ -304,8 +304,8 @@ public static function resource($value, $type = null, $message = '') public static function isCallable($value, $message = '') { - if (!is_callable($value)) { - static::reportInvalidArgument(sprintf( + if (!\is_callable($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a callable. Got: %s', static::typeToString($value) )); @@ -314,8 +314,8 @@ public static function isCallable($value, $message = '') public static function isArray($value, $message = '') { - if (!is_array($value)) { - static::reportInvalidArgument(sprintf( + if (!\is_array($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected an array. Got: %s', static::typeToString($value) )); @@ -327,16 +327,16 @@ public static function isArray($value, $message = '') */ public static function isTraversable($value, $message = '') { - @trigger_error( - sprintf( + @\trigger_error( + \sprintf( 'The "%s" assertion is deprecated. You should stop using it, as it will soon be removed in 2.0 version. Use "isIterable" or "isInstanceOf" instead.', __METHOD__ ), - E_USER_DEPRECATED + \E_USER_DEPRECATED ); - if (!is_array($value) && !($value instanceof Traversable)) { - static::reportInvalidArgument(sprintf( + if (!\is_array($value) && !($value instanceof Traversable)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a traversable. Got: %s', static::typeToString($value) )); @@ -345,8 +345,8 @@ public static function isTraversable($value, $message = '') public static function isArrayAccessible($value, $message = '') { - if (!is_array($value) && !($value instanceof ArrayAccess)) { - static::reportInvalidArgument(sprintf( + if (!\is_array($value) && !($value instanceof ArrayAccess)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected an array accessible. Got: %s', static::typeToString($value) )); @@ -355,8 +355,8 @@ public static function isArrayAccessible($value, $message = '') public static function isCountable($value, $message = '') { - if (!is_array($value) && !($value instanceof Countable)) { - static::reportInvalidArgument(sprintf( + if (!\is_array($value) && !($value instanceof Countable)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a countable. Got: %s', static::typeToString($value) )); @@ -365,8 +365,8 @@ public static function isCountable($value, $message = '') public static function isIterable($value, $message = '') { - if (!is_array($value) && !($value instanceof Traversable)) { - static::reportInvalidArgument(sprintf( + if (!\is_array($value) && !($value instanceof Traversable)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected an iterable. Got: %s', static::typeToString($value) )); @@ -376,7 +376,7 @@ public static function isIterable($value, $message = '') public static function isInstanceOf($value, $class, $message = '') { if (!($value instanceof $class)) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected an instance of %2$s. Got: %s', static::typeToString($value), $class @@ -387,7 +387,7 @@ public static function isInstanceOf($value, $class, $message = '') public static function notInstanceOf($value, $class, $message = '') { if ($value instanceof $class) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected an instance other than %2$s. Got: %s', static::typeToString($value), $class @@ -403,17 +403,17 @@ public static function isInstanceOfAny($value, array $classes, $message = '') } } - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected an instance of any of %2$s. Got: %s', static::typeToString($value), - implode(', ', array_map(array('static', 'valueToString'), $classes)) + \implode(', ', \array_map(array('static', 'valueToString'), $classes)) )); } public static function isEmpty($value, $message = '') { if (!empty($value)) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected an empty value. Got: %s', static::valueToString($value) )); @@ -423,7 +423,7 @@ public static function isEmpty($value, $message = '') public static function notEmpty($value, $message = '') { if (empty($value)) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a non-empty value. Got: %s', static::valueToString($value) )); @@ -433,7 +433,7 @@ public static function notEmpty($value, $message = '') public static function null($value, $message = '') { if (null !== $value) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected null. Got: %s', static::valueToString($value) )); @@ -452,7 +452,7 @@ public static function notNull($value, $message = '') public static function true($value, $message = '') { if (true !== $value) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to be true. Got: %s', static::valueToString($value) )); @@ -462,7 +462,7 @@ public static function true($value, $message = '') public static function false($value, $message = '') { if (false !== $value) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to be false. Got: %s', static::valueToString($value) )); @@ -471,8 +471,8 @@ public static function false($value, $message = '') public static function ip($value, $message = '') { - if (false === filter_var($value, FILTER_VALIDATE_IP)) { - static::reportInvalidArgument(sprintf( + if (false === \filter_var($value, \FILTER_VALIDATE_IP)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to be an IP. Got: %s', static::valueToString($value) )); @@ -481,8 +481,8 @@ public static function ip($value, $message = '') public static function ipv4($value, $message = '') { - if (false === filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { - static::reportInvalidArgument(sprintf( + if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to be an IPv4. Got: %s', static::valueToString($value) )); @@ -491,8 +491,8 @@ public static function ipv4($value, $message = '') public static function ipv6($value, $message = '') { - if (false === filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - static::reportInvalidArgument(sprintf( + if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to be an IPv6. Got %s', static::valueToString($value) )); @@ -501,13 +501,13 @@ public static function ipv6($value, $message = '') public static function uniqueValues(array $values, $message = '') { - $allValues = count($values); - $uniqueValues = count(array_unique($values)); + $allValues = \count($values); + $uniqueValues = \count(\array_unique($values)); if ($allValues !== $uniqueValues) { $difference = $allValues - $uniqueValues; - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected an array of unique values, but %s of them %s duplicated', $difference, (1 === $difference ? 'is' : 'are') @@ -518,7 +518,7 @@ public static function uniqueValues(array $values, $message = '') public static function eq($value, $expect, $message = '') { if ($expect != $value) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value equal to %2$s. Got: %s', static::valueToString($value), static::valueToString($expect) @@ -529,7 +529,7 @@ public static function eq($value, $expect, $message = '') public static function notEq($value, $expect, $message = '') { if ($expect == $value) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a different value than %s.', static::valueToString($expect) )); @@ -539,7 +539,7 @@ public static function notEq($value, $expect, $message = '') public static function same($value, $expect, $message = '') { if ($expect !== $value) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value identical to %2$s. Got: %s', static::valueToString($value), static::valueToString($expect) @@ -550,7 +550,7 @@ public static function same($value, $expect, $message = '') public static function notSame($value, $expect, $message = '') { if ($expect === $value) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value not identical to %s.', static::valueToString($expect) )); @@ -560,7 +560,7 @@ public static function notSame($value, $expect, $message = '') public static function greaterThan($value, $limit, $message = '') { if ($value <= $limit) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value greater than %2$s. Got: %s', static::valueToString($value), static::valueToString($limit) @@ -571,7 +571,7 @@ public static function greaterThan($value, $limit, $message = '') public static function greaterThanEq($value, $limit, $message = '') { if ($value < $limit) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value greater than or equal to %2$s. Got: %s', static::valueToString($value), static::valueToString($limit) @@ -582,7 +582,7 @@ public static function greaterThanEq($value, $limit, $message = '') public static function lessThan($value, $limit, $message = '') { if ($value >= $limit) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value less than %2$s. Got: %s', static::valueToString($value), static::valueToString($limit) @@ -593,7 +593,7 @@ public static function lessThan($value, $limit, $message = '') public static function lessThanEq($value, $limit, $message = '') { if ($value > $limit) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value less than or equal to %2$s. Got: %s', static::valueToString($value), static::valueToString($limit) @@ -604,7 +604,7 @@ public static function lessThanEq($value, $limit, $message = '') public static function range($value, $min, $max, $message = '') { if ($value < $min || $value > $max) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value between %2$s and %3$s. Got: %s', static::valueToString($value), static::valueToString($min), @@ -615,19 +615,19 @@ public static function range($value, $min, $max, $message = '') public static function oneOf($value, array $values, $message = '') { - if (!in_array($value, $values, true)) { - static::reportInvalidArgument(sprintf( + if (!\in_array($value, $values, true)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected one of: %2$s. Got: %s', static::valueToString($value), - implode(', ', array_map(array('static', 'valueToString'), $values)) + \implode(', ', \array_map(array('static', 'valueToString'), $values)) )); } } public static function contains($value, $subString, $message = '') { - if (false === strpos($value, $subString)) { - static::reportInvalidArgument(sprintf( + if (false === \strpos($value, $subString)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to contain %2$s. Got: %s', static::valueToString($value), static::valueToString($subString) @@ -637,8 +637,8 @@ public static function contains($value, $subString, $message = '') public static function notContains($value, $subString, $message = '') { - if (false !== strpos($value, $subString)) { - static::reportInvalidArgument(sprintf( + if (false !== \strpos($value, $subString)) { + static::reportInvalidArgument(\sprintf( $message ?: '%2$s was not expected to be contained in a value. Got: %s', static::valueToString($value), static::valueToString($subString) @@ -648,8 +648,8 @@ public static function notContains($value, $subString, $message = '') public static function notWhitespaceOnly($value, $message = '') { - if (preg_match('/^\s*$/', $value)) { - static::reportInvalidArgument(sprintf( + if (\preg_match('/^\s*$/', $value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a non-whitespace string. Got: %s', static::valueToString($value) )); @@ -658,8 +658,8 @@ public static function notWhitespaceOnly($value, $message = '') public static function startsWith($value, $prefix, $message = '') { - if (0 !== strpos($value, $prefix)) { - static::reportInvalidArgument(sprintf( + if (0 !== \strpos($value, $prefix)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to start with %2$s. Got: %s', static::valueToString($value), static::valueToString($prefix) @@ -672,14 +672,14 @@ public static function startsWithLetter($value, $message = '') $valid = isset($value[0]); if ($valid) { - $locale = setlocale(LC_CTYPE, 0); - setlocale(LC_CTYPE, 'C'); - $valid = ctype_alpha($value[0]); - setlocale(LC_CTYPE, $locale); + $locale = \setlocale(LC_CTYPE, 0); + \setlocale(LC_CTYPE, 'C'); + $valid = \ctype_alpha($value[0]); + \setlocale(LC_CTYPE, $locale); } if (!$valid) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to start with a letter. Got: %s', static::valueToString($value) )); @@ -688,8 +688,8 @@ public static function startsWithLetter($value, $message = '') public static function endsWith($value, $suffix, $message = '') { - if ($suffix !== substr($value, -strlen($suffix))) { - static::reportInvalidArgument(sprintf( + if ($suffix !== \substr($value, -\strlen($suffix))) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to end with %2$s. Got: %s', static::valueToString($value), static::valueToString($suffix) @@ -699,8 +699,8 @@ public static function endsWith($value, $suffix, $message = '') public static function regex($value, $pattern, $message = '') { - if (!preg_match($pattern, $value)) { - static::reportInvalidArgument(sprintf( + if (!\preg_match($pattern, $value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'The value %s does not match the expected pattern.', static::valueToString($value) )); @@ -709,8 +709,8 @@ public static function regex($value, $pattern, $message = '') public static function notRegex($value, $pattern, $message = '') { - if (preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) { - static::reportInvalidArgument(sprintf( + if (\preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) { + static::reportInvalidArgument(\sprintf( $message ?: 'The value %s matches the pattern %s (at offset %d).', static::valueToString($value), static::valueToString($pattern), @@ -721,13 +721,13 @@ public static function notRegex($value, $pattern, $message = '') public static function alpha($value, $message = '') { - $locale = setlocale(LC_CTYPE, 0); - setlocale(LC_CTYPE, 'C'); - $valid = !ctype_alpha($value); - setlocale(LC_CTYPE, $locale); + $locale = \setlocale(LC_CTYPE, 0); + \setlocale(LC_CTYPE, 'C'); + $valid = !\ctype_alpha($value); + \setlocale(LC_CTYPE, $locale); if ($valid) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to contain only letters. Got: %s', static::valueToString($value) )); @@ -736,13 +736,13 @@ public static function alpha($value, $message = '') public static function digits($value, $message = '') { - $locale = setlocale(LC_CTYPE, 0); - setlocale(LC_CTYPE, 'C'); - $valid = !ctype_digit($value); - setlocale(LC_CTYPE, $locale); + $locale = \setlocale(LC_CTYPE, 0); + \setlocale(LC_CTYPE, 'C'); + $valid = !\ctype_digit($value); + \setlocale(LC_CTYPE, $locale); if ($valid) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to contain digits only. Got: %s', static::valueToString($value) )); @@ -751,13 +751,13 @@ public static function digits($value, $message = '') public static function alnum($value, $message = '') { - $locale = setlocale(LC_CTYPE, 0); - setlocale(LC_CTYPE, 'C'); - $valid = !ctype_alnum($value); - setlocale(LC_CTYPE, $locale); + $locale = \setlocale(LC_CTYPE, 0); + \setlocale(LC_CTYPE, 'C'); + $valid = !\ctype_alnum($value); + \setlocale(LC_CTYPE, $locale); if ($valid) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to contain letters and digits only. Got: %s', static::valueToString($value) )); @@ -766,13 +766,13 @@ public static function alnum($value, $message = '') public static function lower($value, $message = '') { - $locale = setlocale(LC_CTYPE, 0); - setlocale(LC_CTYPE, 'C'); - $valid = !ctype_lower($value); - setlocale(LC_CTYPE, $locale); + $locale = \setlocale(LC_CTYPE, 0); + \setlocale(LC_CTYPE, 'C'); + $valid = !\ctype_lower($value); + \setlocale(LC_CTYPE, $locale); if ($valid) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to contain lowercase characters only. Got: %s', static::valueToString($value) )); @@ -781,13 +781,13 @@ public static function lower($value, $message = '') public static function upper($value, $message = '') { - $locale = setlocale(LC_CTYPE, 0); - setlocale(LC_CTYPE, 'C'); - $valid = !ctype_upper($value); - setlocale(LC_CTYPE, $locale); + $locale = \setlocale(LC_CTYPE, 0); + \setlocale(LC_CTYPE, 'C'); + $valid = !\ctype_upper($value); + \setlocale(LC_CTYPE, $locale); if ($valid) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to contain uppercase characters only. Got: %s', static::valueToString($value) )); @@ -797,7 +797,7 @@ public static function upper($value, $message = '') public static function length($value, $length, $message = '') { if ($length !== static::strlen($value)) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to contain %2$s characters. Got: %s', static::valueToString($value), $length @@ -808,7 +808,7 @@ public static function length($value, $length, $message = '') public static function minLength($value, $min, $message = '') { if (static::strlen($value) < $min) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to contain at least %2$s characters. Got: %s', static::valueToString($value), $min @@ -819,7 +819,7 @@ public static function minLength($value, $min, $message = '') public static function maxLength($value, $max, $message = '') { if (static::strlen($value) > $max) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to contain at most %2$s characters. Got: %s', static::valueToString($value), $max @@ -832,7 +832,7 @@ public static function lengthBetween($value, $min, $max, $message = '') $length = static::strlen($value); if ($length < $min || $length > $max) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s', static::valueToString($value), $min, @@ -845,8 +845,8 @@ public static function fileExists($value, $message = '') { static::string($value); - if (!file_exists($value)) { - static::reportInvalidArgument(sprintf( + if (!\file_exists($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'The file %s does not exist.', static::valueToString($value) )); @@ -857,8 +857,8 @@ public static function file($value, $message = '') { static::fileExists($value, $message); - if (!is_file($value)) { - static::reportInvalidArgument(sprintf( + if (!\is_file($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'The path %s is not a file.', static::valueToString($value) )); @@ -869,8 +869,8 @@ public static function directory($value, $message = '') { static::fileExists($value, $message); - if (!is_dir($value)) { - static::reportInvalidArgument(sprintf( + if (!\is_dir($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'The path %s is no directory.', static::valueToString($value) )); @@ -879,8 +879,8 @@ public static function directory($value, $message = '') public static function readable($value, $message = '') { - if (!is_readable($value)) { - static::reportInvalidArgument(sprintf( + if (!\is_readable($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'The path %s is not readable.', static::valueToString($value) )); @@ -889,8 +889,8 @@ public static function readable($value, $message = '') public static function writable($value, $message = '') { - if (!is_writable($value)) { - static::reportInvalidArgument(sprintf( + if (!\is_writable($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'The path %s is not writable.', static::valueToString($value) )); @@ -899,8 +899,8 @@ public static function writable($value, $message = '') public static function classExists($value, $message = '') { - if (!class_exists($value)) { - static::reportInvalidArgument(sprintf( + if (!\class_exists($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected an existing class name. Got: %s', static::valueToString($value) )); @@ -909,8 +909,8 @@ public static function classExists($value, $message = '') public static function subclassOf($value, $class, $message = '') { - if (!is_subclass_of($value, $class)) { - static::reportInvalidArgument(sprintf( + if (!\is_subclass_of($value, $class)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected a sub-class of %2$s. Got: %s', static::valueToString($value), static::valueToString($class) @@ -920,8 +920,8 @@ public static function subclassOf($value, $class, $message = '') public static function interfaceExists($value, $message = '') { - if (!interface_exists($value)) { - static::reportInvalidArgument(sprintf( + if (!\interface_exists($value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected an existing interface name. got %s', static::valueToString($value) )); @@ -930,8 +930,8 @@ public static function interfaceExists($value, $message = '') public static function implementsInterface($value, $interface, $message = '') { - if (!in_array($interface, class_implements($value))) { - static::reportInvalidArgument(sprintf( + if (!\in_array($interface, \class_implements($value))) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected an implementation of %2$s. Got: %s', static::valueToString($value), static::valueToString($interface) @@ -941,8 +941,8 @@ public static function implementsInterface($value, $interface, $message = '') public static function propertyExists($classOrObject, $property, $message = '') { - if (!property_exists($classOrObject, $property)) { - static::reportInvalidArgument(sprintf( + if (!\property_exists($classOrObject, $property)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected the property %s to exist.', static::valueToString($property) )); @@ -951,8 +951,8 @@ public static function propertyExists($classOrObject, $property, $message = '') public static function propertyNotExists($classOrObject, $property, $message = '') { - if (property_exists($classOrObject, $property)) { - static::reportInvalidArgument(sprintf( + if (\property_exists($classOrObject, $property)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected the property %s to not exist.', static::valueToString($property) )); @@ -961,8 +961,8 @@ public static function propertyNotExists($classOrObject, $property, $message = ' public static function methodExists($classOrObject, $method, $message = '') { - if (!method_exists($classOrObject, $method)) { - static::reportInvalidArgument(sprintf( + if (!\method_exists($classOrObject, $method)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected the method %s to exist.', static::valueToString($method) )); @@ -971,8 +971,8 @@ public static function methodExists($classOrObject, $method, $message = '') public static function methodNotExists($classOrObject, $method, $message = '') { - if (method_exists($classOrObject, $method)) { - static::reportInvalidArgument(sprintf( + if (\method_exists($classOrObject, $method)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected the method %s to not exist.', static::valueToString($method) )); @@ -981,8 +981,8 @@ public static function methodNotExists($classOrObject, $method, $message = '') public static function keyExists($array, $key, $message = '') { - if (!(isset($array[$key]) || array_key_exists($key, $array))) { - static::reportInvalidArgument(sprintf( + if (!(isset($array[$key]) || \array_key_exists($key, $array))) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected the key %s to exist.', static::valueToString($key) )); @@ -991,8 +991,8 @@ public static function keyExists($array, $key, $message = '') public static function keyNotExists($array, $key, $message = '') { - if (isset($array[$key]) || array_key_exists($key, $array)) { - static::reportInvalidArgument(sprintf( + if (isset($array[$key]) || \array_key_exists($key, $array)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected the key %s to not exist.', static::valueToString($key) )); @@ -1002,18 +1002,18 @@ public static function keyNotExists($array, $key, $message = '') public static function count($array, $number, $message = '') { static::eq( - count($array), + \count($array), $number, - $message ?: sprintf('Expected an array to contain %d elements. Got: %d.', $number, count($array)) + $message ?: \sprintf('Expected an array to contain %d elements. Got: %d.', $number, \count($array)) ); } public static function minCount($array, $min, $message = '') { - if (count($array) < $min) { - static::reportInvalidArgument(sprintf( + if (\count($array) < $min) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected an array to contain at least %2$d elements. Got: %d', - count($array), + \count($array), $min )); } @@ -1021,10 +1021,10 @@ public static function minCount($array, $min, $message = '') public static function maxCount($array, $max, $message = '') { - if (count($array) > $max) { - static::reportInvalidArgument(sprintf( + if (\count($array) > $max) { + static::reportInvalidArgument(\sprintf( $message ?: 'Expected an array to contain at most %2$d elements. Got: %d', - count($array), + \count($array), $max )); } @@ -1032,10 +1032,10 @@ public static function maxCount($array, $max, $message = '') public static function countBetween($array, $min, $max, $message = '') { - $count = count($array); + $count = \count($array); if ($count < $min || $count > $max) { - static::reportInvalidArgument(sprintf( + static::reportInvalidArgument(\sprintf( $message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d', $count, $min, @@ -1046,7 +1046,7 @@ public static function countBetween($array, $min, $max, $message = '') public static function isList($array, $message = '') { - if (!is_array($array) || !$array || array_keys($array) !== range(0, count($array) - 1)) { + if (!\is_array($array) || !$array || \array_keys($array) !== \range(0, \count($array) - 1)) { static::reportInvalidArgument( $message ?: 'Expected list - non-associative array.' ); @@ -1056,10 +1056,10 @@ public static function isList($array, $message = '') public static function isMap($array, $message = '') { if ( - !is_array($array) || + !\is_array($array) || !$array || - array_keys($array) !== array_filter(array_keys($array), function ($key) { - return is_string($key); + \array_keys($array) !== \array_filter(\array_keys($array), function ($key) { + return \is_string($key); }) ) { static::reportInvalidArgument( @@ -1070,7 +1070,7 @@ public static function isMap($array, $message = '') public static function uuid($value, $message = '') { - $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value); + $value = \str_replace(array('urn:', 'uuid:', '{', '}'), '', $value); // The nil UUID is special form of UUID that is specified to have all // 128 bits set to zero. @@ -1078,8 +1078,8 @@ public static function uuid($value, $message = '') return; } - if (!preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/', $value)) { - static::reportInvalidArgument(sprintf( + if (!\preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/', $value)) { + static::reportInvalidArgument(\sprintf( $message ?: 'Value %s is not a valid UUID.', static::valueToString($value) )); @@ -1095,18 +1095,18 @@ public static function throws(Closure $expression, $class = 'Exception', $messag try { $expression(); } catch (Exception $e) { - $actual = get_class($e); + $actual = \get_class($e); if ($e instanceof $class) { return; } } catch (Throwable $e) { - $actual = get_class($e); + $actual = \get_class($e); if ($e instanceof $class) { return; } } - static::reportInvalidArgument($message ?: sprintf( + static::reportInvalidArgument($message ?: \sprintf( 'Expected to throw "%s", got "%s"', $class, $actual @@ -1115,25 +1115,25 @@ public static function throws(Closure $expression, $class = 'Exception', $messag public static function __callStatic($name, $arguments) { - if ('nullOr' === substr($name, 0, 6)) { + if ('nullOr' === \substr($name, 0, 6)) { if (null !== $arguments[0]) { - $method = lcfirst(substr($name, 6)); - call_user_func_array(array('static', $method), $arguments); + $method = \lcfirst(\substr($name, 6)); + \call_user_func_array(array('static', $method), $arguments); } return; } - if ('all' === substr($name, 0, 3)) { + if ('all' === \substr($name, 0, 3)) { static::isIterable($arguments[0]); - $method = lcfirst(substr($name, 3)); + $method = \lcfirst(\substr($name, 3)); $args = $arguments; foreach ($arguments[0] as $entry) { $args[0] = $entry; - call_user_func_array(array('static', $method), $args); + \call_user_func_array(array('static', $method), $args); } return; @@ -1156,23 +1156,23 @@ protected static function valueToString($value) return 'false'; } - if (is_array($value)) { + if (\is_array($value)) { return 'array'; } - if (is_object($value)) { - if (method_exists($value, '__toString')) { - return get_class($value).': '.self::valueToString($value->__toString()); + if (\is_object($value)) { + if (\method_exists($value, '__toString')) { + return \get_class($value).': '.self::valueToString($value->__toString()); } - return get_class($value); + return \get_class($value); } - if (is_resource($value)) { + if (\is_resource($value)) { return 'resource'; } - if (is_string($value)) { + if (\is_string($value)) { return '"'.$value.'"'; } @@ -1181,20 +1181,20 @@ protected static function valueToString($value) protected static function typeToString($value) { - return is_object($value) ? get_class($value) : gettype($value); + return \is_object($value) ? \get_class($value) : \gettype($value); } protected static function strlen($value) { - if (!function_exists('mb_detect_encoding')) { - return strlen($value); + if (!\function_exists('mb_detect_encoding')) { + return \strlen($value); } - if (false === $encoding = mb_detect_encoding($value)) { - return strlen($value); + if (false === $encoding = \mb_detect_encoding($value)) { + return \strlen($value); } - return mb_strlen($value, $encoding); + return \mb_strlen($value, $encoding); } protected static function reportInvalidArgument($message)