Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Commit

Permalink
don't use is_* functions for arrays
Browse files Browse the repository at this point in the history
is_array not supported in HHVM 4.71+
others not supported before HHVM 4.45
is/as expressions work across all versions

we don't care about the exact array type here, so using the most generic available interface everywhere
  • Loading branch information
jjergus committed Aug 17, 2020
1 parent 667cc81 commit 0cae9ed
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
7 changes: 4 additions & 3 deletions src/HHClientFallbackHandler.hack
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,14 @@ class HHClientFallbackHandler extends FailureHandler {
}

$data = \json_decode($last, /* assoc = */ true);
if (!\HH\is_any_array($data)) {
if (!$data is Traversable<_>) {
return null;
}
foreach ($data as $row) {
$row as KeyedContainer<_, _>;
if ($row['name'] === $name) {
$file = $row['filename'];
if (\substr($file, -4) === '.hhi') {
$file = $row['filename'] as ?string;
if ($file is null || \substr($file, -4) === '.hhi') {
return null;
}
return $file;
Expand Down
18 changes: 5 additions & 13 deletions src/TypeAssert.hack
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function is_nullable_bool(mixed $value, string $field): ?bool {
}

function is_array_of_strings(mixed $value, string $field): varray<string> {
invariant(\HH\is_any_array($value), '%s should be an array<string>', $field);
invariant($value is Container<_>, '%s should be an array<string>', $field);
$out = varray[];
foreach ($value as $it) {
invariant($it is string, '%s should be an array<string>', $field);
Expand All @@ -42,11 +42,7 @@ function is_array_of_strings(mixed $value, string $field): varray<string> {
}

function is_vec_like_of_strings(mixed $value, string $field): vec<string> {
invariant(
$value is vec<_> || \HH\is_php_array($value),
'%s should be a vec<string>',
$field,
);
invariant($value is Traversable<_>, '%s should be a vec<string>', $field);
$out = vec[];
foreach ($value as $el) {
invariant($el is string, '%s should be a vec<string>', $field);
Expand All @@ -63,11 +59,7 @@ function is_nullable_vec_like_of_strings(
return null;
}

invariant(
\HH\is_php_array($value) || $value is vec<_>,
'%s should be an ?vec<string>',
$field,
);
invariant($value is Traversable<_>, '%s should be an ?vec<string>', $field);
$out = vec[];
foreach ($value as $it) {
invariant($it is string, '%s should be an ?vec<string>', $field);
Expand Down Expand Up @@ -95,10 +87,10 @@ function is_array_of_shapes_with_name_field(
string $field,
): varray<shape('name' => string)> {
$msg = $field.'should be an array<shape(\'name\' => string)>';
invariant(\HH\is_any_array($value), '%s', $msg);
invariant($value is Traversable<_>, '%s', $msg);
$out = varray[];
foreach ($value as $it) {
invariant(\HH\is_any_array($it), '%s', $msg);
invariant($it is KeyedContainer<_, _>, '%s', $msg);
$name = $it['name'] ?? null;
invariant($name is string, '%s', $msg);
$out[] = shape('name' => $name);
Expand Down
5 changes: 4 additions & 1 deletion src/builders/FactParseScanner.hack
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ final class FactParseScanner implements Builder {
)>;

private static function untypedToShape(mixed $data): self::TFacts {
invariant(\HH\is_any_array($data), 'FactsParse did not give us an array');
invariant(
$data is KeyedTraversable<_, _>,
'FactsParse did not give us an array',
);

$out = darray[];
foreach ($data as $file => $facts) {
Expand Down

0 comments on commit 0cae9ed

Please sign in to comment.