Skip to content

Commit

Permalink
Added ClassStringLiteralType
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Sep 28, 2023
1 parent 71e5be0 commit 21cc464
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/ClassStringLiteralType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Typhoon\Type;

/**
* @api
* @psalm-immutable
* @template-covariant TClass of class-string
* @implements Type<TClass>
*/
final class ClassStringLiteralType implements Type
{
/**
* @var TClass
*/
public readonly string $class;

/**
* @internal
* @psalm-internal Typhoon\Type
* @param TClass $class
*/
public function __construct(
string $class,
) {
$this->class = $class;
}

public function accept(TypeVisitor $visitor): mixed
{
return $visitor->visitClassStringLiteral($this);
}
}
3 changes: 3 additions & 0 deletions src/TypeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public function visitLiteralString(LiteralStringType $type): mixed;
/** @return TReturn */
public function visitNumericString(NumericStringType $type): mixed;

/** @return TReturn */
public function visitClassStringLiteral(ClassStringLiteralType $type): mixed;

/** @return TReturn */
public function visitNamedClassString(NamedClassStringType $type): mixed;

Expand Down
11 changes: 11 additions & 0 deletions src/types.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ public static function stringLiteral(string $value): StringLiteralType
return new StringLiteralType($value);
}

/**
* @psalm-pure
* @template TClass of class-string
* @param TClass $class
* @return ClassStringLiteralType<TClass>
*/
public static function classStringLiteral(string $class): ClassStringLiteralType
{
return new ClassStringLiteralType($class);
}

/**
* @psalm-pure
* @template TObject of object
Expand Down
11 changes: 11 additions & 0 deletions tests/psalm/ClassStringLiteralTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Typhoon\Type\ClassStringLiteralTest;

use Typhoon\Type\ClassStringLiteralType;
use function Typhoon\Type\extractType;

/** @psalm-check-type-exact $_classLiteral = stdClass::class */
$_classLiteral = extractType(new ClassStringLiteralType(\stdClass::class));

0 comments on commit 21cc464

Please sign in to comment.