Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/AtClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Typhoon\Type;

/**
* @api
* @psalm-immutable
*/
final class AtClass
{
/**
* @var class-string
*/
public readonly string $name;

/**
* @internal
* @psalm-internal Typhoon\Type
* @param class-string $name
*/
public function __construct(
string $name,
) {
$this->name = $name;
}
}
28 changes: 28 additions & 0 deletions src/AtFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Typhoon\Type;

/**
* @api
* @psalm-immutable
*/
final class AtFunction
{
/**
* @var callable-string
*/
public readonly string $name;

/**
* @internal
* @psalm-internal Typhoon\Type
* @param callable-string $name
*/
public function __construct(
string $name,
) {
$this->name = $name;
}
}
36 changes: 36 additions & 0 deletions src/AtMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Typhoon\Type;

/**
* @api
* @psalm-immutable
*/
final class AtMethod
{
/**
* @var class-string
*/
public readonly string $class;

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

/**
* @internal
* @psalm-internal Typhoon\Type
* @param class-string $class
* @param non-empty-string $name
*/
public function __construct(
string $class,
string $name,
) {
$this->class = $class;
$this->name = $name;
}
}
8 changes: 8 additions & 0 deletions src/StaticType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*/
final class StaticType implements Type
{
/**
* @var class-string<TObject>
*/
public readonly string $declaredAtClass;

/**
* @var list<Type>
*/
Expand All @@ -21,11 +26,14 @@ final class StaticType implements Type
* @internal
* @psalm-internal Typhoon\Type
* @no-named-arguments
* @param class-string<TObject> $declaredAtClass
* @param list<Type> $templateArguments
*/
public function __construct(
string $declaredAtClass,
array $templateArguments = [],
) {
$this->declaredAtClass = $declaredAtClass;
$this->templateArguments = $templateArguments;
}

Expand Down
12 changes: 12 additions & 0 deletions src/TemplateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,27 @@ final class TemplateType implements Type
*/
public readonly string $name;

public readonly AtMethod|AtClass|AtFunction $declaredAt;

/**
* @var Type<TType>
*/
public readonly Type $constraint;

/**
* @internal
* @psalm-internal Typhoon\Type
* @param non-empty-string $name
* @param Type<TType> $constraint
*/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@param Type<TType> $constraint

public function __construct(
string $name,
AtFunction|AtClass|AtMethod $declaredAt,
Type $constraint,
) {
$this->name = $name;
$this->declaredAt = $declaredAt;
$this->constraint = $constraint;
}

public function accept(TypeVisitor $visitor): mixed
Expand Down
39 changes: 35 additions & 4 deletions src/types.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,13 @@ public static function object(string $class, Type ...$templateArguments): NamedO
/**
* @psalm-pure
* @no-named-arguments
* @template TObject of object
* @param class-string<TObject> $declaredAtClass
* @return StaticType<TObject>
*/
public static function static(Type ...$templateArguments): StaticType
public static function static(string $declaredAtClass, Type ...$templateArguments): StaticType
{
return new StaticType($templateArguments);
return new StaticType($declaredAtClass, $templateArguments);
}

/**
Expand Down Expand Up @@ -367,11 +370,39 @@ public static function valueOf(Type $type): ValueOfType

/**
* @psalm-pure
* @template TType
* @param non-empty-string $name
* @param Type<TType> $constraint
* @return TemplateType<TType>
*/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@template TType
@param Type<TType> $constraint
@return TemplateType<TType>

public static function template(string $name, AtMethod|AtClass|AtFunction $declaredAt, Type $constraint = self::mixed): TemplateType
{
return new TemplateType($name, $declaredAt, $constraint);
}

/**
* @param callable-string $name
*/
public static function atFunction(string $name): AtFunction
{
return new AtFunction($name);
}

/**
* @param class-string $name
*/
public static function atClass(string $name): AtClass
{
return new AtClass($name);
}

/**
* @param class-string $class
* @param non-empty-string $name
*/
public static function template(string $name): TemplateType
public static function atMethod(string $class, string $name): AtMethod
{
return new TemplateType($name);
return new AtMethod($class, $name);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/psalm/StaticType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Typhoon\Type;

$_type = PsalmTest::extractType(new StaticType());
/** @psalm-check-type-exact $_type = \object */
$_type = PsalmTest::extractType(new StaticType(\stdClass::class));
/** @psalm-check-type-exact $_type = \stdClass */

--EXPECT--
4 changes: 2 additions & 2 deletions tests/psalm/TemplateType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Typhoon\Type;

$_type = PsalmTest::extractType(new TemplateType('T'));
/** @psalm-check-type-exact $_type = \mixed */
$_type = PsalmTest::extractType(new TemplateType('T', new AtFunction('trim'), ObjectType::type));
/** @psalm-check-type-exact $_type = \object */

--EXPECT--