Skip to content

Commit 2b7a144

Browse files
committed
Support non-empty-array<K, V>
1 parent 9d318e1 commit 2b7a144

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

composer.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Internal/ContextualTypeParser.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use function Typhoon\Type\floatT;
2828
use function Typhoon\Type\intRangeT;
2929
use function Typhoon\Type\intT;
30+
use function Typhoon\Type\nonEmptyArrayT;
3031
use function Typhoon\Type\nullOrT;
3132
use function Typhoon\Type\orT;
3233
use function Typhoon\Type\stringT;
@@ -154,6 +155,15 @@ private function parseIdentifier(string $name, array $genericNodes = []): Type
154155
};
155156
}
156157

158+
if ($name === 'non-empty-array') {
159+
return match ($number = \count($templateArguments)) {
160+
0 => nonEmptyArrayT(),
161+
1 => nonEmptyArrayT(valueType: $templateArguments[0]),
162+
2 => nonEmptyArrayT($templateArguments[0], $templateArguments[1]),
163+
default => throw new \LogicException(\sprintf('non-empty-array type should have at most 2 type arguments, got %d', $number)),
164+
};
165+
}
166+
157167
return $this->customTypeParser->parseCustomType($name, $templateArguments, $this->context)
158168
?? $this->context->resolveNameAsType($name, $templateArguments);
159169
}

tests/PHPStanTypeParserTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use function Typhoon\Type\floatT;
1616
use function Typhoon\Type\intRangeT;
1717
use function Typhoon\Type\intT;
18+
use function Typhoon\Type\nonEmptyArrayT;
1819
use function Typhoon\Type\nullOrT;
1920
use function Typhoon\Type\objectT;
2021
use function Typhoon\Type\orT;
@@ -101,6 +102,9 @@ private static function cases(): \Generator
101102
yield 'array' => arrayT;
102103
yield 'array<string>' => arrayT(valueType: stringT);
103104
yield 'array<int, string>' => arrayT(intT, stringT);
105+
yield 'non-empty-array' => nonEmptyArrayT();
106+
yield 'non-empty-array<string>' => nonEmptyArrayT(valueType: stringT);
107+
yield 'non-empty-array<int, string>' => nonEmptyArrayT(intT, stringT);
104108
yield 'mixed' => mixedT;
105109
yield \stdClass::class => objectT(\stdClass::class);
106110
yield \Stringable::class => objectT(\Stringable::class);

0 commit comments

Comments
 (0)