Skip to content

Commit dfb6e69

Browse files
committed
use early return; improve comment
1 parent 1df5440 commit dfb6e69

File tree

1 file changed

+50
-48
lines changed

1 file changed

+50
-48
lines changed

src/Codegen/Constraints/BaseBuilder.php

+50-48
Original file line numberDiff line numberDiff line change
@@ -95,60 +95,62 @@ protected function addEnumConstraintCheck(HackBuilder $hb): void {
9595

9696
protected function addHackEnumConstraintCheck(HackBuilder $hb): void {
9797
$schema = type_assert_type($this->typed_schema, TSchema::class);
98-
if (Shapes::keyExists($schema, 'hackEnum')) {
99-
try {
100-
$rc = new \ReflectionClass($schema['hackEnum']);
101-
} catch (\ReflectionException $e) {
102-
throw new \Exception(Str\format("Hack enum '%s' does not exist", $schema['hackEnum']));
103-
}
98+
if (!Shapes::keyExists($schema, 'hackEnum')) {
99+
return;
100+
}
104101

105-
invariant($rc->isEnum(), "'%s' is not an enum", $schema['hackEnum']);
102+
try {
103+
$rc = new \ReflectionClass($schema['hackEnum']);
104+
} catch (\ReflectionException $e) {
105+
throw new \Exception(Str\format("Hack enum '%s' does not exist", $schema['hackEnum']));
106+
}
106107

107-
$schema_type = $schema['type'] ?? null;
108-
$hack_enum_values = keyset[];
109-
foreach ($rc->getConstants() as $hack_enum_value) {
110-
if ($schema_type === TSchemaType::INTEGER_T) {
111-
$hack_enum_value = $hack_enum_value ?as int;
112-
} else {
113-
$hack_enum_value = $hack_enum_value ?as string;
114-
}
115-
invariant(
116-
$hack_enum_value is nonnull,
117-
"'%s' must contain only values of type %s",
118-
$rc->getName(),
119-
$schema_type === TSchemaType::INTEGER_T ? 'int' : 'string'
120-
);
121-
$hack_enum_values[] = $hack_enum_value;
122-
}
108+
invariant($rc->isEnum(), "'%s' is not an enum", $schema['hackEnum']);
123109

124-
if (Shapes::keyExists($schema, 'enum')) {
125-
// If both `enum` and `hackEnum` are specified, assert that `enum` is a subset of
126-
// `hackEnum` values.
127-
foreach ($schema['enum'] as $enum_value) {
128-
invariant(
129-
$enum_value is string,
130-
"Enum value '%s' is not a valid value for '%s'",
131-
\print_r($enum_value, true),
132-
$rc->getName()
133-
);
134-
invariant(
135-
C\contains_key($hack_enum_values, $enum_value),
136-
"Enum value '%s' is unexpectedly not present in '%s'",
137-
\print_r($enum_value, true),
138-
$rc->getName()
139-
);
140-
}
110+
$schema_type = $schema['type'] ?? null;
111+
$hack_enum_values = keyset[];
112+
foreach ($rc->getConstants() as $hack_enum_value) {
113+
if ($schema_type === TSchemaType::INTEGER_T) {
114+
$hack_enum_value = $hack_enum_value ?as int;
115+
} else {
116+
$hack_enum_value = $hack_enum_value ?as string;
141117
}
142-
143-
$hb->addMultilineCall(
144-
'$typed = Constraints\HackEnumConstraint::check',
145-
vec[
146-
'$typed',
147-
Str\format('\%s::class', $rc->getName()),
148-
'$pointer'
149-
]
118+
invariant(
119+
$hack_enum_value is nonnull,
120+
"'%s' must contain only values of type %s",
121+
$rc->getName(),
122+
$schema_type === TSchemaType::INTEGER_T ? 'int' : 'string'
150123
);
124+
$hack_enum_values[] = $hack_enum_value;
151125
}
126+
127+
if (Shapes::keyExists($schema, 'enum')) {
128+
// If both `enum` and `hackEnum` are specified, assert that `enum` is a subset of
129+
// `hackEnum` values. Any value not also in `hackEnum` can't be valid.
130+
foreach ($schema['enum'] as $enum_value) {
131+
invariant(
132+
$enum_value is string,
133+
"Enum value '%s' is not a valid value for '%s'",
134+
\print_r($enum_value, true),
135+
$rc->getName()
136+
);
137+
invariant(
138+
C\contains_key($hack_enum_values, $enum_value),
139+
"Enum value '%s' is unexpectedly not present in '%s'",
140+
\print_r($enum_value, true),
141+
$rc->getName()
142+
);
143+
}
144+
}
145+
146+
$hb->addMultilineCall(
147+
'$typed = Constraints\HackEnumConstraint::check',
148+
vec[
149+
'$typed',
150+
Str\format('\%s::class', $rc->getName()),
151+
'$pointer'
152+
]
153+
);
152154
}
153155

154156
public function addBuilderClass(CodegenClass $class): void {

0 commit comments

Comments
 (0)