Skip to content

Commit

Permalink
Added AliasType
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Feb 22, 2024
1 parent 0c3cc4d commit 22d13bd
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/AliasType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Typhoon\Type;

/**
* @api
* @template-covariant TType
* @implements Type<TType>
*/
final class AliasType implements Type
{
/**
* @var non-empty-string
*/
public readonly string $class;

/**
* @var non-empty-string
*/
public readonly string $name;

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

public function accept(TypeVisitor $visitor): mixed
{
return $visitor->visitAlias($this);
}
}
3 changes: 3 additions & 0 deletions src/TypeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public function visitTemplate(TemplateType $type): mixed;
/** @return TReturn */
public function visitConditional(ConditionalType $type): mixed;

/** @return TReturn */
public function visitAlias(AliasType $type): mixed;

/** @return TReturn */
public function visitIntersection(IntersectionType $type): mixed;

Expand Down
9 changes: 9 additions & 0 deletions src/types.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,15 @@ public static function conditional(Argument|TemplateType $subject, Type $if, Typ
return new ConditionalType($subject, $if, $then, $else);
}

/**
* @param non-empty-string $class
* @param non-empty-string $name
*/
public static function alias(string $class, string $name): AliasType
{
return new AliasType($class, $name);
}

/**
* @param non-empty-string $name
*/
Expand Down
1 change: 1 addition & 0 deletions tests/PsalmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static function extractType(Type $_type): mixed
return null;
}

#[TestWith([__DIR__ . '/psalm/AliasType.phpt'])]
#[TestWith([__DIR__ . '/psalm/AnyLiteralIntType.phpt'])]
#[TestWith([__DIR__ . '/psalm/AnyLiteralStringType.phpt'])]
#[TestWith([__DIR__ . '/psalm/ArrayShapeType.phpt'])]
Expand Down
9 changes: 9 additions & 0 deletions tests/psalm/AliasType.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--FILE--
<?php

namespace Typhoon\Type;

$_type = PsalmTest::extractType(new AliasType('SomeClass', 'AliasType'));
/** @psalm-check-type-exact $_type = mixed */

--EXPECT--

0 comments on commit 22d13bd

Please sign in to comment.