Skip to content

Instantly share code, notes, and snippets.

@dasl-
Created November 6, 2019 21:34
Show Gist options
  • Save dasl-/810360d4c60cc2eae179ef7de6e9df5c to your computer and use it in GitHub Desktop.
Save dasl-/810360d4c60cc2eae179ef7de6e9df5c to your computer and use it in GitHub Desktop.
diff --git a/src/Phan/Language/UnionTypeBuilder.php b/src/Phan/Language/UnionTypeBuilder.php
index 98c3587a..f0d0c13d 100644
--- a/src/Phan/Language/UnionTypeBuilder.php
+++ b/src/Phan/Language/UnionTypeBuilder.php
@@ -2,6 +2,8 @@
namespace Phan\Language;
+use Phan\Library\Set;
+
/**
* Utilities to build a union type.
* Mostly used internally when the number of types in the resulting union type may be large.
@@ -13,45 +15,36 @@ namespace Phan\Language;
*/
final class UnionTypeBuilder
{
- /** @var list<Type> the list of unique types in this builder instance. */
+ /** @var Set the list of unique types in this builder instance. */
private $type_set;
/** @param list<Type> $type_set (must be unique) */
public function __construct(array $type_set = [])
{
- $this->type_set = $type_set;
+ $this->type_set = new Set($type_set);
}
public function addType(Type $type) : void
{
- if (\in_array($type, $this->type_set, true)) {
+ if ($this->type_set->contains($type)) {
return;
}
- $this->type_set[] = $type;
+ $this->type_set->attach($type);
}
public function addUnionType(UnionType $union_type) : void
{
$old_type_set = $this->type_set;
foreach ($union_type->getTypeSet() as $type) {
- if (!\in_array($type, $old_type_set, true)) {
- $this->type_set[] = $type;
+ if (!$old_type_set->contains($type)) {
+ $this->type_set->attach($type);
}
}
}
public function removeType(Type $type) : void
{
- $i = \array_search($type, $this->type_set, true);
- if ($i !== false) {
- // equivalent to unset($new_type_set[$i]) but fills in the gap in array keys.
- // TODO: How do other ways of unsetting the type affect performance on large projects?
- $replacement_type = \array_pop($this->type_set);
- if ($replacement_type !== $type) {
- // @phan-suppress-next-line PhanPartialTypeMismatchProperty $replacement_type is guaranteed to not be false
- $this->type_set[$i] = $replacement_type;
- }
- }
+ $this->type_set->detach($type);
}
/**
@@ -67,12 +60,12 @@ final class UnionTypeBuilder
*/
public function getTypeSet() : array
{
- return $this->type_set;
+ return $this->type_set->toArray();
}
public function clearTypeSet() : void
{
- $this->type_set = [];
+ $this->type_set = new Set();;
}
/**
@@ -80,7 +73,7 @@ final class UnionTypeBuilder
*/
public function getPHPDocUnionType() : UnionType
{
- return UnionType::of($this->type_set, []);
+ return UnionType::of($this->type_set->toArray(), []);
}
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment