Skip to content

Commit

Permalink
Lax NamedObjectType and ClassStringLiteralType constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Feb 22, 2024
1 parent 4b99bc5 commit d2c7d78
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/ClassStringLiteralType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
* @api
* @template-covariant TClass of class-string
* @template-covariant TClass of non-empty-string
* @implements Type<TClass>
*/
final class ClassStringLiteralType implements Type
Expand Down
4 changes: 2 additions & 2 deletions src/NamedObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
final class NamedObjectType implements Type
{
/**
* @var class-string<TObject>
* @var class-string<TObject>|non-empty-string
*/
public readonly string $class;

Expand All @@ -24,7 +24,7 @@ final class NamedObjectType implements Type
/**
* @internal
* @psalm-internal Typhoon\Type
* @param class-string<TObject> $class
* @param class-string<TObject>|non-empty-string $class
* @param list<Type> $templateArguments
*/
public function __construct(
Expand Down
7 changes: 4 additions & 3 deletions src/types.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static function literal(bool|int|float|string $value): LiteralType
}

/**
* @template TClass of class-string
* @template TClass of non-empty-string
* @param TClass $class
* @return ClassStringLiteralType<TClass>
*/
Expand Down Expand Up @@ -214,8 +214,9 @@ public static function prop(Type $type, bool $optional = false): Property
/**
* @no-named-arguments
* @template TObject of object
* @param class-string<TObject> $class
* @return NamedObjectType<TObject>
* @param class-string<TObject>|non-empty-string $class
* @return ($class is class-string ? NamedObjectType<TObject> : NamedObjectType<object>)
* @psalm-suppress MixedReturnTypeCoercion
*/
public static function object(string $class, Type ...$templateArguments): NamedObjectType
{
Expand Down
7 changes: 5 additions & 2 deletions tests/psalm/NamedObjectType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

namespace Typhoon\Type;

$_type = PsalmTest::extractType(new NamedObjectType(\stdClass::class));
/** @psalm-check-type-exact $_type = \stdClass */
$_nonExistingClassObject = PsalmTest::extractType(new NamedObjectType('SomeClass'));
/** @psalm-check-type-exact $_nonExistingClassObject = mixed */

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

--EXPECT--
5 changes: 5 additions & 0 deletions tests/psalm/types.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ namespace Typhoon\Type;
$_ints = PsalmTest::extractType(types::list(valueType: types::int));
/** @psalm-check-type-exact $_ints = list<int> */

$_nonExistingClassObject = PsalmTest::extractType(types::object('SomeClass'));
/** @psalm-check-type-exact $_nonExistingClassObject = object */

$_existingClassObject = PsalmTest::extractType(types::object(\stdClass::class));
/** @psalm-check-type-exact $_existingClassObject = \stdClass */
--EXPECT--

0 comments on commit d2c7d78

Please sign in to comment.