Skip to content

Commit

Permalink
Synchronized with typhoon-php/type#30
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Feb 16, 2024
1 parent 297ec91 commit e715c43
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 67 deletions.
11 changes: 0 additions & 11 deletions src/Nullability/NullableChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
/**
* @internal
* @psalm-internal Typhoon\Reflection
* @psalm-immutable
* @implements Type\TypeVisitor<bool>
*/
final class NullableChecker implements Type\TypeVisitor
Expand Down Expand Up @@ -121,11 +120,6 @@ public function visitClassString(Type\ClassStringType $type): mixed
return false;
}

public function visitCallableString(Type\CallableStringType $type): mixed
{
return false;
}

public function visitInterfaceString(Type\InterfaceStringType $type): mixed
{
return false;
Expand Down Expand Up @@ -191,11 +185,6 @@ public function visitNonEmptyArray(Type\NonEmptyArrayType $type): mixed
return false;
}

public function visitCallableArray(Type\CallableArrayType $type): mixed
{
return false;
}

public function visitArray(Type\ArrayType $type): mixed
{
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/PhpDocParser/ContextualPhpDocTypeReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ private function reflectIdentifier(string $name, array $genericTypes = []): Type
'array-key' => types::arrayKey,
'literal-int' => types::literalInt,
'literal-string' => types::literalString,
'callable-string' => types::callableString,
'callable-string' => types::intersection(types::callable(), types::string),
'interface-string' => types::interfaceString,
'enum-string' => types::enumString,
'trait-string' => types::traitString,
'callable-array' => types::callableArray,
'callable-array' => types::intersection(types::callable(), types::array()),
'resource' => types::resource,
'closed-resource' => types::closedResource,
'object' => types::object,
Expand Down
1 change: 0 additions & 1 deletion src/TypeAlias/ImportedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
/**
* @internal
* @psalm-internal Typhoon\Reflection
* @psalm-immutable
* @implements Type<mixed>
*/
final class ImportedType implements Type
Expand Down
4 changes: 0 additions & 4 deletions src/TypeAlias/ImportedTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
/**
* @internal
* @psalm-internal Typhoon\Reflection
* @psalm-immutable
*/
final class ImportedTypeResolver extends RecursiveTypeReplacer
{
Expand All @@ -22,9 +21,6 @@ public function __construct(
private readonly ClassReflector $classReflector,
) {}

/**
* @psalm-suppress ImpureMethodCall
*/
public function visitImportedType(ImportedType $type): Type
{
return $this->classReflector->reflectClass($type->class)->getTypeAlias($type->name);
Expand Down
55 changes: 14 additions & 41 deletions src/TypeResolver/RecursiveTypeReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
/**
* @internal
* @psalm-internal Typhoon\Reflection
* @psalm-immutable
* @implements Type\TypeVisitor<Type\Type>
*/
abstract class RecursiveTypeReplacer implements Type\TypeVisitor
Expand Down Expand Up @@ -116,11 +115,6 @@ public function visitClassString(Type\ClassStringType $type): mixed
return $type;
}

public function visitCallableString(Type\CallableStringType $type): mixed
{
return $type;
}

public function visitInterfaceString(Type\InterfaceStringType $type): mixed
{
return $type;
Expand Down Expand Up @@ -178,12 +172,10 @@ public function visitList(Type\ListType $type): mixed

public function visitArrayShape(Type\ArrayShapeType $type): mixed
{
$visitor = $this;

return types::arrayShape(
array_map(
static fn(Type\ArrayElement $element): Type\ArrayElement => types::arrayElement(
$element->type->accept($visitor),
fn(Type\ArrayElement $element): Type\ArrayElement => types::arrayElement(
$element->type->accept($this),
$element->optional,
),
$type->elements,
Expand All @@ -198,11 +190,6 @@ public function visitNonEmptyArray(Type\NonEmptyArrayType $type): mixed
return types::nonEmptyArray($type->keyType->accept($this), $type->valueType->accept($this));
}

public function visitCallableArray(Type\CallableArrayType $type): mixed
{
return $type;
}

public function visitArray(Type\ArrayType $type): mixed
{
/** @psalm-suppress MixedArgumentTypeCoercion */
Expand All @@ -216,32 +203,26 @@ public function visitIterable(Type\IterableType $type): mixed

public function visitNamedObject(Type\NamedObjectType $type): mixed
{
$visitor = $this;

return types::object($type->class, ...array_map(
static fn(Type\Type $templateArgument): Type\Type => $templateArgument->accept($visitor),
fn(Type\Type $templateArgument): Type\Type => $templateArgument->accept($this),
$type->templateArguments,
));
}

public function visitStatic(Type\StaticType $type): mixed
{
$visitor = $this;

return types::static($type->declaredAtClass, ...array_map(
static fn(Type\Type $templateArgument): Type\Type => $templateArgument->accept($visitor),
fn(Type\Type $templateArgument): Type\Type => $templateArgument->accept($this),
$type->templateArguments,
));
}

public function visitObjectShape(Type\ObjectShapeType $type): mixed
{
$visitor = $this;

return types::objectShape(
array_map(
static fn(Type\Property $property): Type\Property => types::prop(
$property->type->accept($visitor),
fn(Type\Property $property): Type\Property => types::prop(
$property->type->accept($this),
$property->optional,
),
$type->properties,
Expand All @@ -266,35 +247,31 @@ public function visitClosedResource(Type\ClosedResourceType $type): mixed

public function visitClosure(Type\ClosureType $type): mixed
{
$visitor = $this;

return types::closure(
array_map(
static fn(Type\Parameter $parameter): Type\Parameter => types::param(
$parameter->type->accept($visitor),
fn(Type\Parameter $parameter): Type\Parameter => types::param(
$parameter->type->accept($this),
$parameter->hasDefault,
$parameter->variadic,
),
$type->parameters,
),
$type->returnType?->accept($visitor),
$type->returnType?->accept($this),
);
}

public function visitCallable(Type\CallableType $type): mixed
{
$visitor = $this;

return types::callable(
array_map(
static fn(Type\Parameter $parameter): Type\Parameter => types::param(
$parameter->type->accept($visitor),
fn(Type\Parameter $parameter): Type\Parameter => types::param(
$parameter->type->accept($this),
$parameter->hasDefault,
$parameter->variadic,
),
$type->parameters,
),
$type->returnType?->accept($visitor),
$type->returnType?->accept($this),
);
}

Expand Down Expand Up @@ -335,20 +312,16 @@ public function visitConditional(Type\ConditionalType $type): mixed

public function visitIntersection(Type\IntersectionType $type): mixed
{
$visitor = $this;

return types::intersection(...array_map(
static fn(Type\Type $part): Type\Type => $part->accept($visitor),
fn(Type\Type $part): Type\Type => $part->accept($this),
$type->types,
));
}

public function visitUnion(Type\UnionType $type): mixed
{
$visitor = $this;

return types::union(...array_map(
static fn(Type\Type $part): Type\Type => $part->accept($visitor),
fn(Type\Type $part): Type\Type => $part->accept($this),
$type->types,
));
}
Expand Down
5 changes: 1 addition & 4 deletions src/TypeResolver/StaticTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
/**
* @internal
* @psalm-internal Typhoon\Reflection
* @psalm-immutable
* @psalm-suppress UnusedClass
*/
final class StaticTypeResolver extends RecursiveTypeReplacer
Expand All @@ -24,10 +23,8 @@ public function __construct(

public function visitStatic(Type\StaticType $type): mixed
{
$visitor = $this;

return types::object($this->class, ...array_map(
static fn(Type\Type $templateArgument): Type\Type => $templateArgument->accept($visitor),
fn(Type\Type $templateArgument): Type\Type => $templateArgument->accept($this),
$type->templateArguments,
));
}
Expand Down
2 changes: 0 additions & 2 deletions src/TypeResolver/TemplateResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
/**
* @internal
* @psalm-internal Typhoon\Reflection
* @psalm-immutable
*/
final class TemplateResolver extends RecursiveTypeReplacer
{
Expand All @@ -22,7 +21,6 @@ private function __construct(
) {}

/**
* @psalm-pure
* @param array<TemplateReflection> $templates
* @param array<Type\Type> $templateArguments
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/PhpDocParser/ContextualPhpDocTypeReflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static function validTypes(): \Generator
yield ['literal-string', types::literalString];
yield ['numeric-string', types::numericString];
yield ['class-string', types::classString];
yield ['callable-string', types::callableString];
yield ['callable-string', types::intersection(types::callable(), types::string)];
yield ['interface-string', types::interfaceString];
yield ['enum-string', types::enumString];
yield ['trait-string', types::traitString];
Expand All @@ -81,7 +81,7 @@ public static function validTypes(): \Generator
yield ['string', types::string];
yield ['numeric', types::numeric];
yield ['scalar', types::scalar];
yield ['callable-array', types::callableArray];
yield ['callable-array', types::intersection(types::callable(), types::array())];
yield ['object', types::object];
yield ['resource', types::resource];
yield ['closed-resource', types::closedResource];
Expand Down

0 comments on commit e715c43

Please sign in to comment.