Skip to content

Commit 362f5fd

Browse files
gen_stub: use match rather than switch when possible
1 parent ef5771d commit 362f5fd

File tree

1 file changed

+65
-122
lines changed

1 file changed

+65
-122
lines changed

build/gen_stub.php

Lines changed: 65 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -329,24 +329,16 @@ public static function fromString(string $typeString): SimpleType
329329
*/
330330
public static function fromValue($value): SimpleType
331331
{
332-
switch (gettype($value)) {
333-
case "NULL":
334-
return SimpleType::null();
335-
case "boolean":
336-
return new SimpleType("bool", true);
337-
case "integer":
338-
return new SimpleType("int", true);
339-
case "double":
340-
return new SimpleType("float", true);
341-
case "string":
342-
return new SimpleType("string", true);
343-
case "array":
344-
return new SimpleType("array", true);
345-
case "object":
346-
return new SimpleType("object", true);
347-
default:
348-
throw new Exception("Type \"" . gettype($value) . "\" cannot be inferred based on value");
349-
}
332+
return match (gettype($value)) {
333+
"NULL" => SimpleType::null(),
334+
"boolean" => new SimpleType("bool", true),
335+
"integer" => new SimpleType("int", true),
336+
"double" => new SimpleType("float", true),
337+
"string" => new SimpleType("string", true),
338+
"array" => new SimpleType("array", true),
339+
"object" => new SimpleType("object", true),
340+
default => throw new Exception("Type \"" . gettype($value) . "\" cannot be inferred based on value"),
341+
};
350342
}
351343

352344
public static function null(): SimpleType
@@ -394,38 +386,23 @@ public function isMixed(): bool {
394386
private function toTypeInfo(): array {
395387
assert($this->isBuiltin);
396388

397-
switch ($this->name) {
398-
case "null":
399-
return ["IS_NULL", "MAY_BE_NULL"];
400-
case "false":
401-
return ["IS_FALSE", "MAY_BE_FALSE"];
402-
case "true":
403-
return ["IS_TRUE", "MAY_BE_TRUE"];
404-
case "bool":
405-
return ["_IS_BOOL", "MAY_BE_BOOL"];
406-
case "int":
407-
return ["IS_LONG", "MAY_BE_LONG"];
408-
case "float":
409-
return ["IS_DOUBLE", "MAY_BE_DOUBLE"];
410-
case "string":
411-
return ["IS_STRING", "MAY_BE_STRING"];
412-
case "array":
413-
return ["IS_ARRAY", "MAY_BE_ARRAY"];
414-
case "object":
415-
return ["IS_OBJECT", "MAY_BE_OBJECT"];
416-
case "callable":
417-
return ["IS_CALLABLE", "MAY_BE_CALLABLE"];
418-
case "mixed":
419-
return ["IS_MIXED", "MAY_BE_ANY"];
420-
case "void":
421-
return ["IS_VOID", "MAY_BE_VOID"];
422-
case "static":
423-
return ["IS_STATIC", "MAY_BE_STATIC"];
424-
case "never":
425-
return ["IS_NEVER", "MAY_BE_NEVER"];
426-
default:
427-
throw new Exception("Not implemented: $this->name");
428-
}
389+
return match ($this->name) {
390+
"null" => ["IS_NULL", "MAY_BE_NULL"],
391+
"false" => ["IS_FALSE", "MAY_BE_FALSE"],
392+
"true" => ["IS_TRUE", "MAY_BE_TRUE"],
393+
"bool" => ["_IS_BOOL", "MAY_BE_BOOL"],
394+
"int" => ["IS_LONG", "MAY_BE_LONG"],
395+
"float" => ["IS_DOUBLE", "MAY_BE_DOUBLE"],
396+
"string" => ["IS_STRING", "MAY_BE_STRING"],
397+
"array" => ["IS_ARRAY", "MAY_BE_ARRAY"],
398+
"object" => ["IS_OBJECT", "MAY_BE_OBJECT"],
399+
"callable" => ["IS_CALLABLE", "MAY_BE_CALLABLE"],
400+
"mixed" => ["IS_MIXED", "MAY_BE_ANY"],
401+
"void" => ["IS_VOID", "MAY_BE_VOID"],
402+
"static" => ["IS_STATIC", "MAY_BE_STATIC"],
403+
"never" => ["IS_NEVER", "MAY_BE_NEVER"],
404+
default => throw new Exception("Not implemented: $this->name"),
405+
};
429406
}
430407

431408
public function toTypeCode(): string {
@@ -439,68 +416,47 @@ public function toTypeMask(): string {
439416
public function toOptimizerTypeMaskForArrayKey(): string {
440417
assert($this->isBuiltin);
441418

442-
switch ($this->name) {
443-
case "int":
444-
return "MAY_BE_ARRAY_KEY_LONG";
445-
case "string":
446-
return "MAY_BE_ARRAY_KEY_STRING";
447-
default:
448-
throw new Exception("Type $this->name cannot be an array key");
449-
}
419+
return match ($this->name) {
420+
"int" => "MAY_BE_ARRAY_KEY_LONG",
421+
"string" => "MAY_BE_ARRAY_KEY_STRING",
422+
default => throw new Exception("Type $this->name cannot be an array key"),
423+
};
450424
}
451425

452426
public function toOptimizerTypeMaskForArrayValue(): string {
453427
if (!$this->isBuiltin) {
454428
return "MAY_BE_ARRAY_OF_OBJECT";
455429
}
456430

457-
switch ($this->name) {
458-
case "null":
459-
return "MAY_BE_ARRAY_OF_NULL";
460-
case "false":
461-
return "MAY_BE_ARRAY_OF_FALSE";
462-
case "true":
463-
return "MAY_BE_ARRAY_OF_TRUE";
464-
case "bool":
465-
return "MAY_BE_ARRAY_OF_FALSE|MAY_BE_ARRAY_OF_TRUE";
466-
case "int":
467-
return "MAY_BE_ARRAY_OF_LONG";
468-
case "float":
469-
return "MAY_BE_ARRAY_OF_DOUBLE";
470-
case "string":
471-
return "MAY_BE_ARRAY_OF_STRING";
472-
case "array":
473-
return "MAY_BE_ARRAY_OF_ARRAY";
474-
case "object":
475-
return "MAY_BE_ARRAY_OF_OBJECT";
476-
case "resource":
477-
return "MAY_BE_ARRAY_OF_RESOURCE";
478-
case "mixed":
479-
return "MAY_BE_ARRAY_OF_ANY";
480-
case "ref":
481-
return "MAY_BE_ARRAY_OF_REF";
482-
default:
483-
throw new Exception("Type $this->name cannot be an array value");
484-
}
431+
return match ($this->name) {
432+
"null" => "MAY_BE_ARRAY_OF_NULL",
433+
"false" => "MAY_BE_ARRAY_OF_FALSE",
434+
"true" => "MAY_BE_ARRAY_OF_TRUE",
435+
"bool" => "MAY_BE_ARRAY_OF_FALSE|MAY_BE_ARRAY_OF_TRUE",
436+
"int" => "MAY_BE_ARRAY_OF_LONG",
437+
"float" => "MAY_BE_ARRAY_OF_DOUBLE",
438+
"string" => "MAY_BE_ARRAY_OF_STRING",
439+
"array" => "MAY_BE_ARRAY_OF_ARRAY",
440+
"object" => "MAY_BE_ARRAY_OF_OBJECT",
441+
"resource" => "MAY_BE_ARRAY_OF_RESOURCE",
442+
"mixed" => "MAY_BE_ARRAY_OF_ANY",
443+
"ref" => "MAY_BE_ARRAY_OF_REF",
444+
default => throw new Exception("Type $this->name cannot be an array value"),
445+
};
485446
}
486447

487448
public function toOptimizerTypeMask(): string {
488449
if (!$this->isBuiltin) {
489450
return "MAY_BE_OBJECT";
490451
}
491452

492-
switch ($this->name) {
493-
case "resource":
494-
return "MAY_BE_RESOURCE";
495-
case "callable":
496-
return "MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_OBJECT";
497-
case "iterable":
498-
return "MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_OBJECT";
499-
case "mixed":
500-
return "MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY";
501-
}
502-
503-
return $this->toTypeMask();
453+
return match ($this->name) {
454+
"resource" => "MAY_BE_RESOURCE",
455+
"callable" => "MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_OBJECT",
456+
"iterable" => "MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_OBJECT",
457+
"mixed" => "MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY",
458+
default => $this->toTypeMask(),
459+
};
504460
}
505461

506462
public function toEscapedName(): string {
@@ -824,16 +780,11 @@ private function getDefaultValueAsArginfoString(): string {
824780
}
825781

826782
public function getDefaultValueAsMethodSynopsisString(): ?string {
827-
switch ($this->defaultValue) {
828-
case 'UNKNOWN':
829-
return null;
830-
case 'false':
831-
case 'true':
832-
case 'null':
833-
return "&{$this->defaultValue};";
834-
}
835-
836-
return $this->defaultValue;
783+
return match ($this->defaultValue) {
784+
'UNKNOWN' => null,
785+
'false' | 'true' | 'null' => "&{$this->defaultValue};",
786+
default => $this->defaultValue,
787+
};
837788
}
838789

839790
public function toZendInfo(): string {
@@ -1940,20 +1891,12 @@ private function getReturnValueSection(DOMDocument $doc): DOMElement {
19401891
} else if (count($returnType->types) === 1) {
19411892
$type = $returnType->types[0];
19421893

1943-
switch ($type->name) {
1944-
case 'void':
1945-
$descriptionNode = $doc->createEntityReference('return.void');
1946-
break;
1947-
case 'true':
1948-
$descriptionNode = $doc->createEntityReference('return.true.always');
1949-
break;
1950-
case 'bool':
1951-
$descriptionNode = $doc->createEntityReference('return.success');
1952-
break;
1953-
default:
1954-
$descriptionNode = new DOMText("Description.");
1955-
break;
1956-
}
1894+
$descriptionNode = match ($type->name) {
1895+
'void' => $doc->createEntityReference('return.void'),
1896+
'true' => $doc->createEntityReference('return.true.always'),
1897+
'bool' => $doc->createEntityReference('return.success'),
1898+
default => new DOMText("Description."),
1899+
};
19571900
$returnDescriptionPara->appendChild($descriptionNode);
19581901
} else {
19591902
$returnDescriptionPara->appendChild(new DOMText("Description."));

0 commit comments

Comments
 (0)