-
-
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.
Browse files
Browse the repository at this point in the history
* feat(type): add class_string types (#432)
- Loading branch information
Showing
6 changed files
with
162 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,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type\Internal; | ||
|
||
use Psl\Type\Exception\AssertException; | ||
use Psl\Type\Exception\CoercionException; | ||
use Psl\Type\Type; | ||
|
||
/** | ||
* @template T as object | ||
* | ||
* @extends Type<class-string<T>> | ||
* | ||
* @internal | ||
*/ | ||
final class ClassStringType extends Type | ||
{ | ||
/** | ||
* @var class-string<T> $classname | ||
*/ | ||
private string $classname; | ||
|
||
/** | ||
* @param class-string<T> $classname | ||
*/ | ||
public function __construct( | ||
string $classname | ||
) { | ||
$this->classname = $classname; | ||
} | ||
|
||
/** | ||
* @psalm-assert-if-true class-string<T> $value | ||
*/ | ||
public function matches(mixed $value): bool | ||
{ | ||
return is_string($value) && is_a($value, $this->classname, true); | ||
} | ||
|
||
/** | ||
* @throws CoercionException | ||
* | ||
* @return class-string<T> | ||
*/ | ||
public function coerce(mixed $value): string | ||
{ | ||
if (is_string($value) && is_a($value, $this->classname, true)) { | ||
return $value; | ||
} | ||
|
||
throw CoercionException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
/** | ||
* @throws AssertException | ||
* | ||
* @return class-string<T> | ||
* | ||
* @psalm-assert class-string<T> $value | ||
*/ | ||
public function assert(mixed $value): string | ||
{ | ||
if (is_string($value) && is_a($value, $this->classname, true)) { | ||
return $value; | ||
} | ||
|
||
throw AssertException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return 'class-string<' . $this->classname . '>'; | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @param class-string<T> $classname | ||
* | ||
* @return TypeInterface<class-string<T>> | ||
*/ | ||
function class_string(string $classname): TypeInterface | ||
{ | ||
return new Internal\ClassStringType($classname); | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\StaticAnalysis\Type; | ||
|
||
use Psl; | ||
use Psl\Type; | ||
|
||
/** | ||
* @param class-string<Psl\Collection\CollectionInterface> $_foo | ||
*/ | ||
function take_collection_classname(string $_foo): void | ||
{ | ||
} | ||
|
||
/** | ||
* @throws Psl\Type\Exception\AssertException | ||
*/ | ||
function tests(): void | ||
{ | ||
take_collection_classname(Type\class_string(Psl\Collection\CollectionInterface::class)->assert('foo')); | ||
} |
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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Type; | ||
|
||
use Psl\Collection; | ||
use Psl\Type; | ||
|
||
final class ClassStringTypeTest extends TypeTest | ||
{ | ||
public function getType(): Type\TypeInterface | ||
{ | ||
return Type\class_string(Collection\CollectionInterface::class); | ||
} | ||
|
||
public function getValidCoercions(): iterable | ||
{ | ||
yield [$_ = Collection\Vector::class, $_]; | ||
yield [$_ = Collection\MutableVector::class, $_]; | ||
yield [$_ = Collection\Map::class, $_]; | ||
yield [$_ = Collection\MutableMap::class, $_]; | ||
yield [$_ = Collection\MutableMapInterface::class, $_]; | ||
yield [$_ = Collection\CollectionInterface::class, $_]; | ||
} | ||
|
||
public function getInvalidCoercions(): iterable | ||
{ | ||
yield [null]; | ||
yield [STDIN]; | ||
yield ['UnknownClass']; | ||
yield [$this->stringable('foo')]; | ||
yield [new class { | ||
}]; | ||
} | ||
|
||
public function getToStringExamples(): iterable | ||
{ | ||
yield [Type\class_string(Collection\MapInterface::class), 'class-string<Psl\Collection\MapInterface>']; | ||
yield [Type\class_string(Collection\VectorInterface::class), 'class-string<Psl\Collection\VectorInterface>']; | ||
yield [Type\class_string(Collection\Vector::class), 'class-string<Psl\Collection\Vector>']; | ||
yield [Type\class_string(Collection\Map::class), 'class-string<Psl\Collection\Map>']; | ||
} | ||
} |