Skip to content

Commit

Permalink
Added OffsetType
Browse files Browse the repository at this point in the history
Closes #27
  • Loading branch information
vudaltsov committed Feb 22, 2024
1 parent 8062f17 commit 888a1e3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/OffsetType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Typhoon\Type;

/**
* @api
* @implements Type<mixed>
*/
final class OffsetType implements Type
{
public readonly Type $subject;

public readonly Type $offset;

/**
* @internal
* @psalm-internal Typhoon\Type
*/
public function __construct(
Type $subject,
Type $offset,
) {
$this->subject = $subject;
$this->offset = $offset;
}

public function accept(TypeVisitor $visitor): mixed
{
return $visitor->visitOffset($this);
}
}
3 changes: 3 additions & 0 deletions src/TypeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ public function visitKeyOf(KeyOfType $type): mixed;
/** @return TReturn */
public function visitValueOf(ValueOfType $type): mixed;

/** @return TReturn */
public function visitOffset(OffsetType $type): mixed;

/** @return TReturn */
public function visitTemplate(TemplateType $type): mixed;

Expand Down
5 changes: 5 additions & 0 deletions src/types.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ public static function valueOf(Type $type): ValueOfType
return new ValueOfType($type);
}

public static function offset(Type $subject, Type $offset): OffsetType
{
return new OffsetType($subject, $offset);
}

/**
* @template TType
* @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 @@ -64,6 +64,7 @@ public static function extractType(Type $_type): mixed
#[TestWith([__DIR__ . '/psalm/NumericType.phpt'])]
#[TestWith([__DIR__ . '/psalm/ObjectShapeType.phpt'])]
#[TestWith([__DIR__ . '/psalm/ObjectType.phpt'])]
#[TestWith([__DIR__ . '/psalm/OffsetType.phpt'])]
#[TestWith([__DIR__ . '/psalm/ResourceType.phpt'])]
#[TestWith([__DIR__ . '/psalm/ScalarType.phpt'])]
#[TestWith([__DIR__ . '/psalm/StaticType.phpt'])]
Expand Down
9 changes: 9 additions & 0 deletions tests/psalm/OffsetType.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--FILE--
<?php

namespace Typhoon\Type;

$_type = PsalmTest::extractType(new OffsetType(new ListType(), new IntLiteralType(0)));
/** @psalm-check-type-exact $_type = mixed */

--EXPECT--

0 comments on commit 888a1e3

Please sign in to comment.