Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add psalm assertions #282

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
92 changes: 92 additions & 0 deletions lib/Assert/Assertion.php
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<ExpectedType> $className
* @psalm-assert ExpectedType $value
*
* @return bool
*/
public static function isInstanceOf($value, $className, $message = null, $propertyPath = null)
Expand All @@ -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<ExpectedType> $className
* @psalm-assert !ExpectedType $value
*
* @return bool
*/
public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null)
Expand Down Expand Up @@ -1338,6 +1404,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)
Expand Down Expand Up @@ -1366,6 +1434,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)
Expand Down Expand Up @@ -1393,6 +1463,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)
Expand Down Expand Up @@ -1520,6 +1592,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)
Expand Down Expand Up @@ -1547,6 +1621,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
Expand Down Expand Up @@ -1622,6 +1698,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)
Expand All @@ -1645,6 +1723,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)
Expand All @@ -1668,6 +1748,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)
Expand All @@ -1691,6 +1773,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)
Expand Down Expand Up @@ -1754,6 +1838,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)
Expand Down Expand Up @@ -1834,6 +1920,8 @@ public static function e164($value, $message = null, $propertyPath = null)
* @param string|null $message
* @param string|null $propertyPath
*
* @psalm-assert non-empty-countable $value
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives me

PHP Fatal error:  Uncaught Psalm\Exception\TypeParseTreeException: no hyphens allowed in projectdir/vendor/vimeo/psalm/src/Psalm/Type/Atomic.php:187
Stack trace:
#0 projectdir/vendor/vimeo/psalm/src/Psalm/Type.php(129): Psalm\Type\Atomic::create('Assert\\non-empt...', NULL, Array)
#1 projectdir/vendor/vimeo/psalm/src/Psalm/Type.php(102): Psalm\Type::parseTokens(Array, NULL, Array)
#2 projectdir/vendor/vimeo/psalm/src/Psalm/Type/Reconciler.php(1123): Psalm\Type::parseString('Assert\\non-empt...', NULL, Array)
#3 projectdir/vendor/vimeo/psalm/src/Psalm/Type/Reconciler.php(205): Psalm\Type\Reconciler::reconcileTypes('Assert\\non-empt...', Object(Psalm\Type\Union), '$value', Object(Psalm\Internal\Analyzer\StatementsAnalyzer), false, Array, Object(Psalm\CodeLocation), Array, 0)
#4 projectdir/vendor/vimeo/psalm/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer. in projectdir/vendor/vimeo/psalm/src/Psalm/Type/Atomic.php on line 187

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry - fixed in 7a14e87 - will release tonight

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't be sorry ;) Thanks

*
* @return bool
*/
public static function count($countable, $count, $message = null, $propertyPath = null)
Expand Down Expand Up @@ -2002,6 +2090,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)
Expand Down Expand Up @@ -2379,6 +2469,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)
Expand Down