Skip to content

Commit 9aaa860

Browse files
committed
Address reviews
1 parent 8f14d0f commit 9aaa860

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

build/gen_stub.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -548,31 +548,26 @@ public function equals(SimpleType $other): bool {
548548
class Type {
549549
/** @var SimpleType[] */
550550
public $types;
551+
/** @var bool */
552+
public $isIntersection = false;
551553

552554
public static function fromNode(Node $node): Type {
553-
if ($node instanceof Node\UnionType) {
555+
if ($node instanceof Node\UnionType || $node instanceof Node\IntersectionType) {
554556
$nestedTypeObjects = array_map(['Type', 'fromNode'], $node->types);
555557
$types = [];
556558
foreach ($nestedTypeObjects as $typeObject) {
557559
array_push($types, ...$typeObject->types);
558560
}
559-
return new Type($types);
560-
}
561-
if ($node instanceof Node\IntersectionType) {
562-
$nestedTypeObjects = array_map(['Type', 'fromNode'], $node->types);
563-
$types = [];
564-
foreach ($nestedTypeObjects as $typeObject) {
565-
array_push($types, ...$typeObject->types);
566-
}
567-
return new Type($types, true);
561+
return new Type($types, ($node instanceof Node\IntersectionType));
568562
}
569563

570564
if ($node instanceof Node\NullableType) {
571565
return new Type(
572566
[
573567
...Type::fromNode($node->type)->types,
574568
SimpleType::null(),
575-
]
569+
],
570+
false
576571
);
577572
}
578573

@@ -581,18 +576,20 @@ public static function fromNode(Node $node): Type {
581576
[
582577
SimpleType::fromString("Traversable"),
583578
ArrayType::createGenericArray(),
584-
]
579+
],
580+
false
585581
);
586582
}
587583

588-
return new Type([SimpleType::fromNode($node)]);
584+
return new Type([SimpleType::fromNode($node)], false);
589585
}
590586

591587
public static function fromString(string $typeString): self {
592588
$typeString .= "|";
593589
$simpleTypes = [];
594590
$simpleTypeOffset = 0;
595591
$inArray = false;
592+
$isIntersection = false;
596593

597594
$typeStringLength = strlen($typeString);
598595
for ($i = 0; $i < $typeStringLength; $i++) {
@@ -612,7 +609,8 @@ public static function fromString(string $typeString): self {
612609
continue;
613610
}
614611

615-
if ($char === "|") {
612+
if ($char === "|" || $char === "&") {
613+
$isIntersection = ($char === "&");
616614
$simpleTypeName = trim(substr($typeString, $simpleTypeOffset, $i - $simpleTypeOffset));
617615

618616
$simpleTypes[] = SimpleType::fromString($simpleTypeName);
@@ -621,14 +619,15 @@ public static function fromString(string $typeString): self {
621619
}
622620
}
623621

624-
return new Type($simpleTypes);
622+
return new Type($simpleTypes, $isIntersection);
625623
}
626624

627625
/**
628626
* @param SimpleType[] $types
629627
*/
630-
private function __construct(array $types, public readonly bool $isIntersection = false) {
628+
private function __construct(array $types, bool $isIntersection) {
631629
$this->types = $types;
630+
$this->isIntersection = $isIntersection;
632631
}
633632

634633
public function isScalar(): bool {
@@ -658,7 +657,8 @@ public function getWithoutNull(): Type {
658657
function(SimpleType $type) {
659658
return !$type->isNull();
660659
}
661-
)
660+
),
661+
false
662662
);
663663
}
664664

@@ -691,6 +691,7 @@ public function toOptimizerTypeMask(): string {
691691
$optimizerTypes = [];
692692

693693
foreach ($this->types as $type) {
694+
// TODO Support for toOptimizerMask for intersection
694695
$optimizerTypes[] = $type->toOptimizerTypeMask();
695696
}
696697

@@ -719,8 +720,9 @@ public function toOptimizerTypeMaskForArrayValue(): string {
719720

720721
public function getTypeForDoc(DOMDocument $doc): DOMElement {
721722
if (count($this->types) > 1) {
723+
$typeSort = $this->isIntersection ? "intersection" : "union";
722724
$typeElement = $doc->createElement('type');
723-
$typeElement->setAttribute("class", "union");
725+
$typeElement->setAttribute("class", $typeSort);
724726

725727
foreach ($this->types as $type) {
726728
$unionTypeElement = $doc->createElement('type', $type->name);
@@ -763,7 +765,8 @@ public function __toString() {
763765
return 'mixed';
764766
}
765767

766-
return implode('|', array_map(
768+
$char = $this->isIntersection ? '&' : '|';
769+
return implode($char, array_map(
767770
function ($type) { return $type->name; },
768771
$this->types)
769772
);

0 commit comments

Comments
 (0)