Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ public static function union(Type ...$types): Type
self::processArrayTypes($arrayTypes, $arrayAccessoryTypesToProcess)
)
);
$typesCount = count($types);

// simplify string[] | int[] to (string|int)[]
$typesCount = count($types);
for ($i = 0; $i < $typesCount; $i++) {
for ($j = $i + 1; $j < $typesCount; $j++) {
if ($types[$i] instanceof IterableType && $types[$j] instanceof IterableType) {
Expand All @@ -301,11 +301,11 @@ public static function union(Type ...$types): Type
}
if ($classType === ConstantBooleanType::class && count($scalarTypeItems) === 2) {
$types[] = new BooleanType();
$typesCount++;
unset($scalarTypes[$classType]);
continue;
}

$typesCount = count($types);
$scalarTypeItemsCount = count($scalarTypeItems);
for ($i = 0; $i < $typesCount; $i++) {
for ($j = 0; $j < $scalarTypeItemsCount; $j++) {
Expand Down Expand Up @@ -339,11 +339,11 @@ public static function union(Type ...$types): Type
$newTypes[$type->describe(VerbosityLevel::cache())] = $type;
}
$types = array_values($newTypes);
$typesCount = count($types);
}

// transform A | A to A
// transform A | never to A
$typesCount = count($types);
for ($i = 0; $i < $typesCount; $i++) {
for ($j = $i + 1; $j < $typesCount; $j++) {
$compareResult = self::compareTypesInUnion($types[$i], $types[$j]);
Expand All @@ -370,18 +370,18 @@ public static function union(Type ...$types): Type
foreach ($scalarTypes as $scalarTypeItems) {
foreach ($scalarTypeItems as $scalarType) {
$types[] = $scalarType;
$typesCount++;
}
}

$typesCount = count($types);
if ($typesCount === 0) {
return new NeverType();
}
if ($typesCount === 1) {
return $types[0];
}

if (count($benevolentTypes) > 0) {
if ($benevolentTypes !== []) {
$tempTypes = $types;
foreach ($tempTypes as $i => $type) {
if (!isset($benevolentTypes[$type->describe(VerbosityLevel::value())])) {
Expand All @@ -391,7 +391,7 @@ public static function union(Type ...$types): Type
unset($tempTypes[$i]);
}

if (count($tempTypes) === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should replace count($xy) === 0 with $xy === [] all over the codebase via a cs fixing rule?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the same is true for count($xy) !== 0 and $xy !== []

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should replace count($xy) === 0 with $xy === [] all over the codebase via a cs fixing rule?

At work I always write "count($x) === 0" because its more readable, but here the code will be called very very often and it seems to make a difference if we call it 100000x so I don't know if we should change it everywhere? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no significant performance gain between them.

See: symfony/symfony#43779

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I also tested it and had a quick research and count() is O(1) since the number of elements is stored in the
HashTable. So, the performance benefit in this PR was maybe from not interacting with the array at all?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly

if ($tempTypes === []) {
if ($benevolentUnionObject instanceof TemplateBenevolentUnionType) {
return $benevolentUnionObject->withTypes($types);
}
Expand Down Expand Up @@ -575,9 +575,11 @@ private static function processArrayTypes(array $arrayTypes, array $accessoryTyp
break 2;
}
}
if (count($arrayTypes) === 0) {

if ($arrayTypes === []) {
return [];
} elseif (count($arrayTypes) === 1) {
}
if (count($arrayTypes) === 1) {
return [
self::intersect($arrayTypes[0], ...$accessoryTypes),
];
Expand Down Expand Up @@ -793,8 +795,9 @@ public static function intersect(Type ...$types): Type
// transform callable & int to never
// transform A & ~A to never
// transform int & string to never
for ($i = 0; $i < count($types); $i++) {
for ($j = $i + 1; $j < count($types); $j++) {
$typesCount = count($types);
for ($i = 0; $i < $typesCount; $i++) {
for ($j = $i + 1; $j < $typesCount; $j++) {
if ($types[$j] instanceof SubtractableType) {
$typeWithoutSubtractedTypeA = $types[$j]->getTypeWithoutSubtractedType();

Expand All @@ -806,6 +809,7 @@ public static function intersect(Type ...$types): Type
if ($isSuperTypeSubtractableA->yes()) {
$types[$i] = self::unionWithSubtractedType($types[$i], $types[$j]->getSubtractedType());
array_splice($types, $j--, 1);
$typesCount--;
continue 1;
}
}
Expand All @@ -821,6 +825,7 @@ public static function intersect(Type ...$types): Type
if ($isSuperTypeSubtractableB->yes()) {
$types[$j] = self::unionWithSubtractedType($types[$j], $types[$i]->getSubtractedType());
array_splice($types, $i--, 1);
$typesCount--;
continue 2;
}
}
Expand All @@ -830,6 +835,7 @@ public static function intersect(Type ...$types): Type
if ($intersectionType !== null) {
$types[$j] = $intersectionType;
array_splice($types, $i--, 1);
$typesCount--;
continue 2;
}
}
Expand All @@ -842,6 +848,7 @@ public static function intersect(Type ...$types): Type

if ($isSuperTypeA->yes()) {
array_splice($types, $j--, 1);
$typesCount--;
continue;
}

Expand All @@ -855,12 +862,14 @@ public static function intersect(Type ...$types): Type
if ($types[$i] instanceof ConstantArrayType && $types[$j] instanceof HasOffsetType) {
$types[$i] = $types[$i]->makeOffsetRequired($types[$j]->getOffsetType());
array_splice($types, $j--, 1);
$typesCount--;
continue;
}

if ($types[$j] instanceof ConstantArrayType && $types[$i] instanceof HasOffsetType) {
$types[$j] = $types[$j]->makeOffsetRequired($types[$i]->getOffsetType());
array_splice($types, $i--, 1);
$typesCount--;
continue 2;
}

Expand All @@ -876,6 +885,7 @@ public static function intersect(Type ...$types): Type
$types[$j] = new ArrayType($keyType, $itemType);
}
array_splice($types, $i--, 1);
$typesCount--;
continue 2;
}

Expand All @@ -884,6 +894,7 @@ public static function intersect(Type ...$types): Type

if ($isSuperTypeB->yes()) {
array_splice($types, $i--, 1);
$typesCount--;
continue 2;
}

Expand All @@ -893,9 +904,8 @@ public static function intersect(Type ...$types): Type
}
}

if (count($types) === 1) {
if ($typesCount === 1) {
return $types[0];

}

return new IntersectionType($types);
Expand Down