Skip to content

Commit

Permalink
Keep duplicate types when using precise
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Mar 18, 2024
1 parent 0241e54 commit 851f7c3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,19 @@ public function describe(VerbosityLevel $level): string
}
}

$typeNames = array_unique($typeNames);
if ($level->isPrecise()) {
$duplicates = array_diff_assoc($typeNames, array_unique($typeNames));
if (count($duplicates) > 0) {
$indexByDuplicate = array_fill_keys($duplicates, 0);
foreach ($typeNames as $key => $typeName) {
if (isset($indexByDuplicate[$typeName])) {
$typeNames[$key] = $typeName.'#'.++$indexByDuplicate[$typeName];
}
}
}
} else {
$typeNames = array_unique($typeNames);
}

if (count($typeNames) > 1024) {
return implode('|', array_slice($typeNames, 0, 1024)) . "|\u{2026}";
Expand Down
5 changes: 5 additions & 0 deletions src/Type/VerbosityLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public function isValue(): bool
return $this->value === self::VALUE;
}

public function isPrecise(): bool
{
return $this->value === self::PRECISE;
}

/** @api */
public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acceptedType = null): self
{
Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Type/UnionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -718,11 +718,13 @@ public function dataDescribe(): array
new UnionType([new IntegerType(), new StringType()]),
'int|string',
'int|string',
'int|string',
],
[
new UnionType([new IntegerType(), new StringType(), new NullType()]),
'int|string|null',
'int|string|null',
'int|string|null',
],
[
new UnionType([
Expand All @@ -743,6 +745,7 @@ public function dataDescribe(): array
new ConstantStringType('1'),
]),
"1|2|2.2|10|'1'|'10'|'10aaa'|'11aaa'|'1aaa'|'2'|'2aaa'|'foo'|stdClass|true|null",
"1|2|2.2|10|'1'|'10'|'10aaa'|'11aaa'|'1aaa'|'2'|'2aaa'|'foo'|stdClass|true|null",
'float|int|stdClass|string|true|null',
],
[
Expand All @@ -764,6 +767,7 @@ public function dataDescribe(): array
new ConstantStringType('aaa'),
),
'\'aaa\'|array{a: int, b: float}|array{a: string, b: bool}',
'\'aaa\'|array{a: int, b: float}|array{a: string, b: bool}',
'array<string, bool|float|int|string>|string',
],
[
Expand All @@ -785,6 +789,7 @@ public function dataDescribe(): array
new ConstantStringType('aaa'),
),
'\'aaa\'|array{a: string, b: bool}|array{b: int, c: float}',
'\'aaa\'|array{a: string, b: bool}|array{b: int, c: float}',
'array<string, bool|float|int|string>|string',
],
[
Expand All @@ -806,6 +811,7 @@ public function dataDescribe(): array
new ConstantStringType('aaa'),
),
'\'aaa\'|array{a: string, b: bool}|array{c: int, d: float}',
'\'aaa\'|array{a: string, b: bool}|array{c: int, d: float}',
'array<string, bool|float|int|string>|string',
],
[
Expand All @@ -826,6 +832,7 @@ public function dataDescribe(): array
]),
),
'array{int, bool, float}|array{string}',
'array{int, bool, float}|array{string}',
'array<int, bool|float|int|string>',
],
[
Expand All @@ -838,6 +845,7 @@ public function dataDescribe(): array
]),
),
'array{}|array{foooo: \'barrr\'}',
'array{}|array{foooo: \'barrr\'}',
'array<string, string>',
],
[
Expand All @@ -849,6 +857,7 @@ public function dataDescribe(): array
]),
),
'int|numeric-string',
'int|numeric-string',
'int|string',
],
[
Expand All @@ -858,6 +867,7 @@ public function dataDescribe(): array
),
'int<0, 4>|int<6, 10>',
'int<0, 4>|int<6, 10>',
'int<0, 4>|int<6, 10>',
],
[
TypeCombinator::union(
Expand All @@ -869,6 +879,7 @@ public function dataDescribe(): array
),
new NullType(),
),
'TFoo of int (class foo, parameter)|null',
'(TFoo of int)|null',
'(TFoo of int)|null',
],
Expand All @@ -882,6 +893,7 @@ public function dataDescribe(): array
),
new GenericClassStringType(new ObjectType('Abc')),
),
'class-string<Abc>|TFoo of int (class foo, parameter)',
'class-string<Abc>|TFoo of int',
'class-string<Abc>|TFoo of int',
],
Expand All @@ -895,6 +907,7 @@ public function dataDescribe(): array
),
new NullType(),
),
'TFoo (class foo, parameter)|null',
'TFoo|null',
'TFoo|null',
],
Expand All @@ -913,11 +926,13 @@ public function dataDescribe(): array
),
new NullType(),
),
'TFoo of TBar (class foo, parameter) (class foo, parameter)|null',
'(TFoo of TBar)|null',
'(TFoo of TBar)|null',
],
[
new UnionType([new QueryType('foo'), new QueryType('bar')]),
'Doctrine\ORM\Query<mixed, mixed>#1|Doctrine\ORM\Query<mixed, mixed>#2',
'Doctrine\ORM\Query<mixed, mixed>',
'Doctrine\ORM\Query<mixed, mixed>',
],
Expand All @@ -929,10 +944,12 @@ public function dataDescribe(): array
*/
public function testDescribe(
Type $type,
string $expectedDescribeDescription,
string $expectedValueDescription,
string $expectedTypeOnlyDescription,
): void
{
$this->assertSame($expectedDescribeDescription, $type->describe(VerbosityLevel::precise()));
$this->assertSame($expectedValueDescription, $type->describe(VerbosityLevel::value()));
$this->assertSame($expectedTypeOnlyDescription, $type->describe(VerbosityLevel::typeOnly()));
}
Expand Down

0 comments on commit 851f7c3

Please sign in to comment.