-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(type): Notnull type optimizations
- Loading branch information
Showing
4 changed files
with
89 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\StaticAnalysis\Type; | ||
|
||
use Psl\Type; | ||
|
||
/** | ||
* @throws Type\Exception\AssertException | ||
*/ | ||
function returns_non_null_assertion(?string $state): string | ||
{ | ||
return Type\nonnull()->assert($state); | ||
} | ||
|
||
/** | ||
* @throws Type\Exception\AssertException | ||
* | ||
* Asserting still leeds to mixed (including null) return type. | ||
* This won't be solvable until psalm supports a TNotNull type. | ||
*/ | ||
function returns_non_null_assertion_asserted(?string $state): ?string | ||
{ | ||
Type\nonnull()->assert($state); | ||
|
||
return $state; | ||
} | ||
|
||
/** | ||
* @throws Type\Exception\CoercionException | ||
*/ | ||
function returns_non_null_coercion(?string $state): string | ||
{ | ||
return Type\nonnull()->coerce($state); | ||
} | ||
|
||
/** | ||
* @return true | ||
*/ | ||
function returns_truthy_match(string $state): bool | ||
{ | ||
return Type\nonnull()->matches($state); | ||
} | ||
|
||
/** | ||
* @return false | ||
*/ | ||
function returns_falsy_match(null $state = null): bool | ||
{ | ||
return Type\nonnull()->matches($state); | ||
} | ||
|
||
/** | ||
* @throws Type\Exception\CoercionException | ||
* | ||
* Wrapping it in container types still leeds to mixed (including null) return type. | ||
* This won't be solvable until psalm supports a TNotNull type. | ||
* | ||
* @return array<'mightBeNull', mixed> | ||
*/ | ||
function returns_mixed_in_shape(mixed $data): array | ||
{ | ||
return Type\shape([ | ||
'mightBeNull' => Type\nonnull(), | ||
])->coerce($data); | ||
} |