Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
125 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type\Internal; | ||
|
||
use Psl\Type; | ||
use Psl\Type\Exception\AssertException; | ||
use Psl\Type\Exception\CoercionException; | ||
|
||
/** | ||
* @ara-extends Type\Type<nonnull> | ||
* | ||
* @extends Type\Type<mixed> | ||
* | ||
* @internal | ||
*/ | ||
final class NonNullType extends Type\Type | ||
{ | ||
/** | ||
* @psalm-assert-if-true mixed $value | ||
* | ||
* @ara-assert-if-true nonnull $value | ||
*/ | ||
public function matches(mixed $value): bool | ||
{ | ||
return null !== $value; | ||
} | ||
|
||
/** | ||
* @ara-return nonnull | ||
* | ||
* @return mixed | ||
*/ | ||
public function coerce(mixed $value): mixed | ||
{ | ||
if (null !== $value) { | ||
return $value; | ||
} | ||
|
||
throw CoercionException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
/** | ||
* @ara-assert nonnull $value | ||
* | ||
* @psalm-assert mixed $value | ||
* | ||
* @ara-return nonnull | ||
* | ||
* @return mixed | ||
*/ | ||
public function assert(mixed $value): mixed | ||
{ | ||
if (null !== $value) { | ||
return $value; | ||
} | ||
|
||
throw AssertException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return 'nonnull'; | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type; | ||
|
||
/** | ||
* @ara-return TypeInterface<nonnull> | ||
* | ||
* @return TypeInterface<mixed> | ||
*/ | ||
function nonnull(): TypeInterface | ||
{ | ||
return new Internal\NonNullType(); | ||
} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Type; | ||
|
||
use Psl\Type; | ||
|
||
final class NonNullTypeTest extends TypeTest | ||
{ | ||
public function getType(): Type\TypeInterface | ||
{ | ||
return Type\nonnull(); | ||
} | ||
|
||
public function getValidCoercions(): iterable | ||
{ | ||
yield [$_ = Type\bool(), $_]; | ||
yield [$_ = 1, $_]; | ||
yield [$_ = 0, $_]; | ||
yield [$_ = false, $_]; | ||
yield [$_ = true, $_]; | ||
yield [$_ = '', $_]; | ||
yield [$_ = 'null', $_]; | ||
yield [$_ = 'foo', $_]; | ||
yield [$_ = [null], $_]; | ||
yield [$_ = [], $_]; | ||
yield [$_ = [1, 2, 3], $_]; | ||
yield [$_ = $this->stringable(''), $_]; | ||
} | ||
|
||
public function getInvalidCoercions(): iterable | ||
{ | ||
yield [null]; | ||
} | ||
|
||
public function getToStringExamples(): iterable | ||
{ | ||
yield [$this->getType(), 'nonnull']; | ||
} | ||
} |