1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

gen_stub: use match rather than switch when possible

This commit is contained in:
Daniel Scherzer
2026-03-18 14:22:09 -07:00
parent ef5771dce2
commit 362f5fdb09

View File

@@ -329,24 +329,16 @@ class SimpleType {
*/ */
public static function fromValue($value): SimpleType public static function fromValue($value): SimpleType
{ {
switch (gettype($value)) { return match (gettype($value)) {
case "NULL": "NULL" => SimpleType::null(),
return SimpleType::null(); "boolean" => new SimpleType("bool", true),
case "boolean": "integer" => new SimpleType("int", true),
return new SimpleType("bool", true); "double" => new SimpleType("float", true),
case "integer": "string" => new SimpleType("string", true),
return new SimpleType("int", true); "array" => new SimpleType("array", true),
case "double": "object" => new SimpleType("object", true),
return new SimpleType("float", true); default => throw new Exception("Type \"" . gettype($value) . "\" cannot be inferred based on value"),
case "string": };
return new SimpleType("string", true);
case "array":
return new SimpleType("array", true);
case "object":
return new SimpleType("object", true);
default:
throw new Exception("Type \"" . gettype($value) . "\" cannot be inferred based on value");
}
} }
public static function null(): SimpleType public static function null(): SimpleType
@@ -394,38 +386,23 @@ class SimpleType {
private function toTypeInfo(): array { private function toTypeInfo(): array {
assert($this->isBuiltin); assert($this->isBuiltin);
switch ($this->name) { return match ($this->name) {
case "null": "null" => ["IS_NULL", "MAY_BE_NULL"],
return ["IS_NULL", "MAY_BE_NULL"]; "false" => ["IS_FALSE", "MAY_BE_FALSE"],
case "false": "true" => ["IS_TRUE", "MAY_BE_TRUE"],
return ["IS_FALSE", "MAY_BE_FALSE"]; "bool" => ["_IS_BOOL", "MAY_BE_BOOL"],
case "true": "int" => ["IS_LONG", "MAY_BE_LONG"],
return ["IS_TRUE", "MAY_BE_TRUE"]; "float" => ["IS_DOUBLE", "MAY_BE_DOUBLE"],
case "bool": "string" => ["IS_STRING", "MAY_BE_STRING"],
return ["_IS_BOOL", "MAY_BE_BOOL"]; "array" => ["IS_ARRAY", "MAY_BE_ARRAY"],
case "int": "object" => ["IS_OBJECT", "MAY_BE_OBJECT"],
return ["IS_LONG", "MAY_BE_LONG"]; "callable" => ["IS_CALLABLE", "MAY_BE_CALLABLE"],
case "float": "mixed" => ["IS_MIXED", "MAY_BE_ANY"],
return ["IS_DOUBLE", "MAY_BE_DOUBLE"]; "void" => ["IS_VOID", "MAY_BE_VOID"],
case "string": "static" => ["IS_STATIC", "MAY_BE_STATIC"],
return ["IS_STRING", "MAY_BE_STRING"]; "never" => ["IS_NEVER", "MAY_BE_NEVER"],
case "array": default => throw new Exception("Not implemented: $this->name"),
return ["IS_ARRAY", "MAY_BE_ARRAY"]; };
case "object":
return ["IS_OBJECT", "MAY_BE_OBJECT"];
case "callable":
return ["IS_CALLABLE", "MAY_BE_CALLABLE"];
case "mixed":
return ["IS_MIXED", "MAY_BE_ANY"];
case "void":
return ["IS_VOID", "MAY_BE_VOID"];
case "static":
return ["IS_STATIC", "MAY_BE_STATIC"];
case "never":
return ["IS_NEVER", "MAY_BE_NEVER"];
default:
throw new Exception("Not implemented: $this->name");
}
} }
public function toTypeCode(): string { public function toTypeCode(): string {
@@ -439,14 +416,11 @@ class SimpleType {
public function toOptimizerTypeMaskForArrayKey(): string { public function toOptimizerTypeMaskForArrayKey(): string {
assert($this->isBuiltin); assert($this->isBuiltin);
switch ($this->name) { return match ($this->name) {
case "int": "int" => "MAY_BE_ARRAY_KEY_LONG",
return "MAY_BE_ARRAY_KEY_LONG"; "string" => "MAY_BE_ARRAY_KEY_STRING",
case "string": default => throw new Exception("Type $this->name cannot be an array key"),
return "MAY_BE_ARRAY_KEY_STRING"; };
default:
throw new Exception("Type $this->name cannot be an array key");
}
} }
public function toOptimizerTypeMaskForArrayValue(): string { public function toOptimizerTypeMaskForArrayValue(): string {
@@ -454,34 +428,21 @@ class SimpleType {
return "MAY_BE_ARRAY_OF_OBJECT"; return "MAY_BE_ARRAY_OF_OBJECT";
} }
switch ($this->name) { return match ($this->name) {
case "null": "null" => "MAY_BE_ARRAY_OF_NULL",
return "MAY_BE_ARRAY_OF_NULL"; "false" => "MAY_BE_ARRAY_OF_FALSE",
case "false": "true" => "MAY_BE_ARRAY_OF_TRUE",
return "MAY_BE_ARRAY_OF_FALSE"; "bool" => "MAY_BE_ARRAY_OF_FALSE|MAY_BE_ARRAY_OF_TRUE",
case "true": "int" => "MAY_BE_ARRAY_OF_LONG",
return "MAY_BE_ARRAY_OF_TRUE"; "float" => "MAY_BE_ARRAY_OF_DOUBLE",
case "bool": "string" => "MAY_BE_ARRAY_OF_STRING",
return "MAY_BE_ARRAY_OF_FALSE|MAY_BE_ARRAY_OF_TRUE"; "array" => "MAY_BE_ARRAY_OF_ARRAY",
case "int": "object" => "MAY_BE_ARRAY_OF_OBJECT",
return "MAY_BE_ARRAY_OF_LONG"; "resource" => "MAY_BE_ARRAY_OF_RESOURCE",
case "float": "mixed" => "MAY_BE_ARRAY_OF_ANY",
return "MAY_BE_ARRAY_OF_DOUBLE"; "ref" => "MAY_BE_ARRAY_OF_REF",
case "string": default => throw new Exception("Type $this->name cannot be an array value"),
return "MAY_BE_ARRAY_OF_STRING"; };
case "array":
return "MAY_BE_ARRAY_OF_ARRAY";
case "object":
return "MAY_BE_ARRAY_OF_OBJECT";
case "resource":
return "MAY_BE_ARRAY_OF_RESOURCE";
case "mixed":
return "MAY_BE_ARRAY_OF_ANY";
case "ref":
return "MAY_BE_ARRAY_OF_REF";
default:
throw new Exception("Type $this->name cannot be an array value");
}
} }
public function toOptimizerTypeMask(): string { public function toOptimizerTypeMask(): string {
@@ -489,18 +450,13 @@ class SimpleType {
return "MAY_BE_OBJECT"; return "MAY_BE_OBJECT";
} }
switch ($this->name) { return match ($this->name) {
case "resource": "resource" => "MAY_BE_RESOURCE",
return "MAY_BE_RESOURCE"; "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",
case "callable": "iterable" => "MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_OBJECT",
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"; "mixed" => "MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY",
case "iterable": default => $this->toTypeMask(),
return "MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_OBJECT"; };
case "mixed":
return "MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY";
}
return $this->toTypeMask();
} }
public function toEscapedName(): string { public function toEscapedName(): string {
@@ -824,16 +780,11 @@ class ArgInfo {
} }
public function getDefaultValueAsMethodSynopsisString(): ?string { public function getDefaultValueAsMethodSynopsisString(): ?string {
switch ($this->defaultValue) { return match ($this->defaultValue) {
case 'UNKNOWN': 'UNKNOWN' => null,
return null; 'false' | 'true' | 'null' => "&{$this->defaultValue};",
case 'false': default => $this->defaultValue,
case 'true': };
case 'null':
return "&{$this->defaultValue};";
}
return $this->defaultValue;
} }
public function toZendInfo(): string { public function toZendInfo(): string {
@@ -1940,20 +1891,12 @@ ENDCOMMENT
} else if (count($returnType->types) === 1) { } else if (count($returnType->types) === 1) {
$type = $returnType->types[0]; $type = $returnType->types[0];
switch ($type->name) { $descriptionNode = match ($type->name) {
case 'void': 'void' => $doc->createEntityReference('return.void'),
$descriptionNode = $doc->createEntityReference('return.void'); 'true' => $doc->createEntityReference('return.true.always'),
break; 'bool' => $doc->createEntityReference('return.success'),
case 'true': default => new DOMText("Description."),
$descriptionNode = $doc->createEntityReference('return.true.always'); };
break;
case 'bool':
$descriptionNode = $doc->createEntityReference('return.success');
break;
default:
$descriptionNode = new DOMText("Description.");
break;
}
$returnDescriptionPara->appendChild($descriptionNode); $returnDescriptionPara->appendChild($descriptionNode);
} else { } else {
$returnDescriptionPara->appendChild(new DOMText("Description.")); $returnDescriptionPara->appendChild(new DOMText("Description."));