Skip to content

Commit

Permalink
Avoid duplicate in union type description
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Mar 17, 2024
1 parent 063f6a8 commit bd6bb86
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ public function describe(VerbosityLevel $level): string
}
}

$typeNames = array_unique($typeNames);

if (count($typeNames) > 1024) {
return implode('|', array_slice($typeNames, 0, 1024)) . "|\u{2026}";
}
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Type/UnionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\data\QueryType;
use PHPStan\Type\Enum\EnumCaseObjectType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\GenericObjectType;
Expand Down Expand Up @@ -915,6 +916,11 @@ public function dataDescribe(): array
'(TFoo of TBar)|null',
'(TFoo of TBar)|null',
],
[
new UnionType([new QueryType('foo'), new QueryType('bar')]),
'Doctrine\ORM\Query<mixed, mixed>',
'Doctrine\ORM\Query<mixed, mixed>',
]
];
}

Expand Down
46 changes: 46 additions & 0 deletions tests/PHPStan/Type/data/QueryType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace PHPStan\Type\data;

use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;

class QueryType extends GenericObjectType
{
/** @var Type */
private $indexType;

/** @var Type */
private $resultType;

/** @var string */
private $dql;

public function __construct(string $dql, ?Type $indexType = null, ?Type $resultType = null, ?Type $subtractedType = null)
{
$this->indexType = $indexType ?? new MixedType();
$this->resultType = $resultType ?? new MixedType();

parent::__construct('Doctrine\ORM\Query', [
$this->indexType,
$this->resultType,
], $subtractedType);

$this->dql = $dql;
}

public function getDql(): string {
return $this->dql;
}

public function equals(Type $type): bool
{
if ($type instanceof self) {
return $this->getDql() === $type->getDql();
}

return parent::equals($type);
}

}

0 comments on commit bd6bb86

Please sign in to comment.