Skip to content

Commit ccc23ce

Browse files
committed
Address reviews
1 parent 767243d commit ccc23ce

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
@@ -503,31 +503,26 @@ public function equals(SimpleType $other): bool {
503503
class Type {
504504
/** @var SimpleType[] */
505505
public $types;
506+
/** @var bool */
507+
public $isIntersection = false;
506508

507509
public static function fromNode(Node $node): Type {
508-
if ($node instanceof Node\UnionType) {
510+
if ($node instanceof Node\UnionType || $node instanceof Node\IntersectionType) {
509511
$nestedTypeObjects = array_map(['Type', 'fromNode'], $node->types);
510512
$types = [];
511513
foreach ($nestedTypeObjects as $typeObject) {
512514
array_push($types, ...$typeObject->types);
513515
}
514-
return new Type($types);
515-
}
516-
if ($node instanceof Node\IntersectionType) {
517-
$nestedTypeObjects = array_map(['Type', 'fromNode'], $node->types);
518-
$types = [];
519-
foreach ($nestedTypeObjects as $typeObject) {
520-
array_push($types, ...$typeObject->types);
521-
}
522-
return new Type($types, true);
516+
return new Type($types, ($node instanceof Node\IntersectionType));
523517
}
524518

525519
if ($node instanceof Node\NullableType) {
526520
return new Type(
527521
[
528522
...Type::fromNode($node->type)->types,
529523
SimpleType::null(),
530-
]
524+
],
525+
false
531526
);
532527
}
533528

@@ -536,18 +531,20 @@ public static function fromNode(Node $node): Type {
536531
[
537532
SimpleType::fromString("Traversable"),
538533
ArrayType::createGenericArray(),
539-
]
534+
],
535+
false
540536
);
541537
}
542538

543-
return new Type([SimpleType::fromNode($node)]);
539+
return new Type([SimpleType::fromNode($node)], false);
544540
}
545541

546542
public static function fromString(string $typeString): self {
547543
$typeString .= "|";
548544
$simpleTypes = [];
549545
$simpleTypeOffset = 0;
550546
$inArray = false;
547+
$isIntersection = false;
551548

552549
$typeStringLength = strlen($typeString);
553550
for ($i = 0; $i < $typeStringLength; $i++) {
@@ -567,7 +564,8 @@ public static function fromString(string $typeString): self {
567564
continue;
568565
}
569566

570-
if ($char === "|") {
567+
if ($char === "|" || $char === "&") {
568+
$isIntersection = ($char === "&");
571569
$simpleTypeName = trim(substr($typeString, $simpleTypeOffset, $i - $simpleTypeOffset));
572570

573571
$simpleTypes[] = SimpleType::fromString($simpleTypeName);
@@ -576,14 +574,15 @@ public static function fromString(string $typeString): self {
576574
}
577575
}
578576

579-
return new Type($simpleTypes);
577+
return new Type($simpleTypes, $isIntersection);
580578
}
581579

582580
/**
583581
* @param SimpleType[] $types
584582
*/
585-
private function __construct(array $types, public readonly bool $isIntersection = false) {
583+
private function __construct(array $types, bool $isIntersection) {
586584
$this->types = $types;
585+
$this->isIntersection = $isIntersection;
587586
}
588587

589588
public function isScalar(): bool {
@@ -613,7 +612,8 @@ public function getWithoutNull(): Type {
613612
function(SimpleType $type) {
614613
return !$type->isNull();
615614
}
616-
)
615+
),
616+
false
617617
);
618618
}
619619

@@ -646,6 +646,7 @@ public function toOptimizerTypeMask(): string {
646646
$optimizerTypes = [];
647647

648648
foreach ($this->types as $type) {
649+
// TODO Support for toOptimizerMask for intersection
649650
$optimizerTypes[] = $type->toOptimizerTypeMask();
650651
}
651652

@@ -674,8 +675,9 @@ public function toOptimizerTypeMaskForArrayValue(): string {
674675

675676
public function getTypeForDoc(DOMDocument $doc): DOMElement {
676677
if (count($this->types) > 1) {
678+
$typeSort = $this->isIntersection ? "intersection" : "union";
677679
$typeElement = $doc->createElement('type');
678-
$typeElement->setAttribute("class", "union");
680+
$typeElement->setAttribute("class", $typeSort);
679681

680682
foreach ($this->types as $type) {
681683
$unionTypeElement = $doc->createElement('type', $type->name);
@@ -718,7 +720,8 @@ public function __toString() {
718720
return 'mixed';
719721
}
720722

721-
return implode('|', array_map(
723+
$char = $this->isIntersection ? '&' : '|';
724+
return implode($char, array_map(
722725
function ($type) { return $type->name; },
723726
$this->types)
724727
);

0 commit comments

Comments
 (0)