From af149bd4dca35868c0602cb3a60b3057e893b396 Mon Sep 17 00:00:00 2001 From: Dave Liddament Date: Thu, 11 Oct 2018 15:11:10 +0100 Subject: [PATCH] ADD extra documentation about psalm-assert Maybe this will be obvious to others, but it probably took me about 3 hours to realise this was possible. Hopefully this doc update will save others time too. --- docs/supported_annotations.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/docs/supported_annotations.md b/docs/supported_annotations.md index 75b24567a..90c3c6923 100644 --- a/docs/supported_annotations.md +++ b/docs/supported_annotations.md @@ -150,6 +150,33 @@ function takesA(A $a) : void { } ``` +As well as getting Psalm to understand that the given data must be a certain type, you can also show that a variable must be not null: + +```php + +/** + * @psalm-assert !null $value + */ +function assertNotNull($value): void { + // Some check that will mean the method will only complete if $value is not null. +} + +``` + +And you can check on null values: + +```php + +/** + * @psalm-assert-if-true null $value + */ +function isNull($value): void { + return ($value === null); +} + +``` + + ### `@psalm-ignore-nullable-return` This can be used to tell Psalm not to worry if a function/method returns null. It’s a bit of a hack, but occasionally useful for scenarios where you either have a very high confidence of a non-null value, or some other function guarantees a non-null value for that particular code path.