From a6d9b1b2212712bdba749f5dee4ac82f38e8fb93 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Sat, 8 Jun 2019 13:49:14 -0400 Subject: [PATCH 1/2] Add Psalm assertions --- lib/Assert/Assertion.php | 96 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/lib/Assert/Assertion.php b/lib/Assert/Assertion.php index 247fa62f..6e43d893 100644 --- a/lib/Assert/Assertion.php +++ b/lib/Assert/Assertion.php @@ -317,6 +317,10 @@ public static function eq($value, $value2, $message = null, $propertyPath = null * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-template ExpectedType + * @psalm-param ExpectedType $value2 + * @psalm-assert =ExpectedType $value + * * @return bool */ public static function same($value, $value2, $message = null, $propertyPath = null) @@ -366,6 +370,10 @@ public static function notEq($value1, $value2, $message = null, $propertyPath = * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-template ExpectedType + * @psalm-param ExpectedType $value2 + * @psalm-assert !=ExpectedType $value + * * @return bool */ public static function notSame($value1, $value2, $message = null, $propertyPath = null) @@ -413,6 +421,8 @@ public static function notInArray($value, array $choices, $message = null, $prop * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert int $value + * * @return bool */ public static function integer($value, $message = null, $propertyPath = null) @@ -436,6 +446,8 @@ public static function integer($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert float $value + * * @return bool */ public static function float($value, $message = null, $propertyPath = null) @@ -459,6 +471,8 @@ public static function float($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert =numeric $value + * * @return bool */ public static function digit($value, $message = null, $propertyPath = null) @@ -518,6 +532,8 @@ public static function integerish($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert bool $value + * * @return bool */ public static function boolean($value, $message = null, $propertyPath = null) @@ -541,6 +557,8 @@ public static function boolean($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert scalar $value + * * @return bool */ public static function scalar($value, $message = null, $propertyPath = null) @@ -564,6 +582,8 @@ public static function scalar($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert !empty $value + * * @return bool */ public static function notEmpty($value, $message = null, $propertyPath = null) @@ -587,6 +607,8 @@ public static function notEmpty($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert empty $value + * * @return bool */ public static function noContent($value, $message = null, $propertyPath = null) @@ -610,6 +632,8 @@ public static function noContent($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert null $value + * * @return bool */ public static function null($value, $message = null, $propertyPath = null) @@ -633,6 +657,8 @@ public static function null($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert !null $value + * * @return bool */ public static function notNull($value, $message = null, $propertyPath = null) @@ -656,6 +682,8 @@ public static function notNull($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert string $value + * * @return bool */ public static function string($value, $message = null, $propertyPath = null) @@ -681,6 +709,8 @@ public static function string($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert =string $value + * * @return bool */ public static function regex($value, $pattern, $message = null, $propertyPath = null) @@ -709,6 +739,8 @@ public static function regex($value, $pattern, $message = null, $propertyPath = * * @return bool * + * @psalm-assert !=string $value + * * @throws \Assert\AssertionFailedException */ public static function notRegex($value, $pattern, $message = null, $propertyPath = null) @@ -736,6 +768,8 @@ public static function notRegex($value, $pattern, $message = null, $propertyPath * @param string|null $propertyPath * @param string $encoding * + * @psalm-assert =string $value + * * @return bool */ public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8') @@ -765,6 +799,8 @@ public static function length($value, $length, $message = null, $propertyPath = * @param string|null $propertyPath * @param string $encoding * + * @psalm-assert =string $value + * * @return bool */ public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8') @@ -794,6 +830,8 @@ public static function minLength($value, $minLength, $message = null, $propertyP * @param string|null $propertyPath * @param string $encoding * + * @psalm-assert =string $value + * * @return bool */ public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') @@ -824,6 +862,8 @@ public static function maxLength($value, $maxLength, $message = null, $propertyP * @param string|null $propertyPath * @param string $encoding * + * @psalm-assert =string $value + * * @return bool */ public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') @@ -844,6 +884,8 @@ public static function betweenLength($value, $minLength, $maxLength, $message = * @param string|null $propertyPath * @param string $encoding * + * @psalm-assert =string $string + * * @return bool */ public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') @@ -872,6 +914,8 @@ public static function startsWith($string, $needle, $message = null, $propertyPa * @param string|null $propertyPath * @param string $encoding * + * @psalm-assert =string $string + * * @return bool */ public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') @@ -902,6 +946,8 @@ public static function endsWith($string, $needle, $message = null, $propertyPath * @param string|null $propertyPath * @param string $encoding * + * @psalm-assert =string $string + * * @return bool */ public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') @@ -930,6 +976,8 @@ public static function contains($string, $needle, $message = null, $propertyPath * @param string|null $propertyPath * @param string $encoding * + * @psalm-assert =string $string + * * @return bool */ public static function notContains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') @@ -998,6 +1046,8 @@ public static function inArray($value, array $choices, $message = null, $propert * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert numeric $string + * * @return bool */ public static function numeric($value, $message = null, $propertyPath = null) @@ -1021,6 +1071,8 @@ public static function numeric($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert resource $string + * * @return bool */ public static function isResource($value, $message = null, $propertyPath = null) @@ -1044,6 +1096,8 @@ public static function isResource($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert array $string + * * @return bool */ public static function isArray($value, $message = null, $propertyPath = null) @@ -1067,6 +1121,8 @@ public static function isArray($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert iterable $string + * * @return bool */ public static function isTraversable($value, $message = null, $propertyPath = null) @@ -1113,6 +1169,8 @@ public static function isArrayAccessible($value, $message = null, $propertyPath * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert countable $string + * * @return bool */ public static function isCountable($value, $message = null, $propertyPath = null) @@ -1262,6 +1320,10 @@ public static function notBlank($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-template ExpectedType of object + * @psalm-param class-string $className + * @psalm-assert ExpectedType $value + * * @return bool */ public static function isInstanceOf($value, $className, $message = null, $propertyPath = null) @@ -1287,6 +1349,10 @@ public static function isInstanceOf($value, $className, $message = null, $proper * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-template ExpectedType of object + * @psalm-param class-string $className + * @psalm-assert !ExpectedType $value + * * @return bool */ public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null) @@ -1312,6 +1378,10 @@ public static function notIsInstanceOf($value, $className, $message = null, $pro * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-template ExpectedType of object + * @psalm-param class-string $className + * @psalm-assert ExpectedType $value + * * @return bool */ public static function subclassOf($value, $className, $message = null, $propertyPath = null) @@ -1338,6 +1408,8 @@ public static function subclassOf($value, $className, $message = null, $property * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert =numeric $value + * * @return bool */ public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null) @@ -1366,6 +1438,8 @@ public static function range($value, $minValue, $maxValue, $message = null, $pro * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert =numeric $value + * * @return bool */ public static function min($value, $minValue, $message = null, $propertyPath = null) @@ -1393,6 +1467,8 @@ public static function min($value, $minValue, $message = null, $propertyPath = n * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert =numeric $value + * * @return bool */ public static function max($value, $maxValue, $message = null, $propertyPath = null) @@ -1520,6 +1596,8 @@ public static function writeable($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert =string $value + * * @return bool */ public static function email($value, $message = null, $propertyPath = null) @@ -1547,6 +1625,8 @@ public static function email($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert =string $value + * * @return bool * * @see https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php @@ -1622,6 +1702,8 @@ public static function alnum($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert true $value + * * @return bool */ public static function true($value, $message = null, $propertyPath = null) @@ -1645,6 +1727,8 @@ public static function true($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert false $value + * * @return bool */ public static function false($value, $message = null, $propertyPath = null) @@ -1668,6 +1752,8 @@ public static function false($value, $message = null, $propertyPath = null) * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert class-string $value + * * @return bool */ public static function classExists($value, $message = null, $propertyPath = null) @@ -1691,6 +1777,8 @@ public static function classExists($value, $message = null, $propertyPath = null * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert class-string $value + * * @return bool */ public static function interfaceExists($value, $message = null, $propertyPath = null) @@ -1754,6 +1842,8 @@ public static function implementsInterface($class, $interfaceName, $message = nu * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert =string $value + * * @return bool */ public static function isJsonString($value, $message = null, $propertyPath = null) @@ -1834,6 +1924,8 @@ public static function e164($value, $message = null, $propertyPath = null) * @param string|null $message * @param string|null $propertyPath * + * @psalm-assert non-empty-countable $value + * * @return bool */ public static function count($countable, $count, $message = null, $propertyPath = null) @@ -2002,6 +2094,8 @@ public static function methodExists($value, $object, $message = null, $propertyP * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert object $value + * * @return bool */ public static function isObject($value, $message = null, $propertyPath = null) @@ -2379,6 +2473,8 @@ public static function extensionVersion($extension, $operator, $version, $messag * @param string|callable|null $message * @param string|null $propertyPath * + * @psalm-assert callable $value + * * @return bool */ public static function isCallable($value, $message = null, $propertyPath = null) From ea0bb8a5ea1081f371c89d2c0582b698c13c08fc Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Sun, 9 Jun 2019 09:54:06 -0400 Subject: [PATCH 2/2] Remove dodgy is_subclass_of --- lib/Assert/Assertion.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/Assert/Assertion.php b/lib/Assert/Assertion.php index 6e43d893..aa4bed36 100644 --- a/lib/Assert/Assertion.php +++ b/lib/Assert/Assertion.php @@ -1378,10 +1378,6 @@ public static function notIsInstanceOf($value, $className, $message = null, $pro * @param string|callable|null $message * @param string|null $propertyPath * - * @psalm-template ExpectedType of object - * @psalm-param class-string $className - * @psalm-assert ExpectedType $value - * * @return bool */ public static function subclassOf($value, $className, $message = null, $propertyPath = null)