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

Commit

Permalink
Some general cleanup: (#46)
Browse files Browse the repository at this point in the history
* Some general cleanup:

 - AutoloadMap.hack
  This type should be in sync with HH\autoload_set_paths(),
    but this function has an incorrect hhi.
    I'll make a PR to hhvm, trying to get this fixed.
 - ConfigurationLoader.hack
  Remove an HH_FIXME by making self::fromData accept a
    wider type.
  Use the null constructor on ImmVector instead of a varray.
 - HHClientFallbackHandler.hack
  Require is allowed from inside a function.
    This might have been a parse error in the past.
    disallow_top_level_requires .hhconfig
    option availability suggests that this is now preferred.
 - Merger.hack
  Use a darray typehint where an array typehint was used.
 - Writer.hack
  Prefer inst_meth over calling a varray as a callable.
 - ComposerImporter.hack
  Use darray/varray typehints where appropriate.
  Stop relying on falsey array promotion.
 - FactParseScanner.hack
  Remove an HH_FIXME, since HH\facts_parse does
    have an HHI these days.
  Use ImmVector::toValuesArray() to prefer varray use.

* Prefer darray(x) over x->toArray()
  • Loading branch information
lexidor authored and jjergus committed Dec 4, 2019
1 parent 2506257 commit 4f69e93
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 30 deletions.
13 changes: 9 additions & 4 deletions src/AutoloadMap.hack
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ namespace Facebook\AutoloadMap;
/** The main shape of an autoload map.
*
* Must match `\HH\autoload_set_paths()`
* However, this is nolonger accurate.
* The parameter of autoload_set_paths is a
* `KeyedContainer<string, KeyedContainer<string, string>>`.
* This does sadly not allow for the fallback key,
* which is a (function(string, string): bool).
*/
type AutoloadMap = shape(
'class' => array<string, string>,
'function' => array<string, string>,
'type' => array<string, string>,
'constant' => array<string, string>,
'class' => darray<string, string>,
'function' => darray<string, string>,
'type' => darray<string, string>,
'constant' => darray<string, string>,
);
16 changes: 5 additions & 11 deletions src/ConfigurationLoader.hack
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,19 @@ abstract final class ConfigurationLoader {
public static function fromJSON(string $json, string $path): Config {
$decoded = \json_decode($json, /* as array = */ true);
invariant(
\is_array($decoded),
$decoded is KeyedContainer<_, _>,
'Expected configuration file to contain a JSON object, got %s',
\gettype($decoded),
);
//hackfmt-ignore (else the comment applies to both arguments)
return self::fromData(
/* HH_IGNORE_ERROR[4110] */ $decoded,
$path
);
return self::fromData($decoded, $path);
}

/** Load configuration from decoded data.
*
* @param $path arbitrary string - used to create clearer error messages
*/
public static function fromData(
array<string, mixed> $data,
KeyedContainer<arraykey, mixed> $data,
string $path,
): Config {
$failure_handler = TypeAssert\is_nullable_string(
Expand All @@ -62,8 +58,7 @@ abstract final class ConfigurationLoader {
TypeAssert\is_nullable_array_of_strings(
$data['devRoots'] ?? null,
'devRoots',
) ??
varray[],
),
),
'autoloadFilesBehavior' => TypeAssert\is_nullable_enum(
AutoloadFilesBehavior::class,
Expand All @@ -85,8 +80,7 @@ abstract final class ConfigurationLoader {
TypeAssert\is_nullable_array_of_strings(
$data['extraFiles'] ?? null,
'extraFiles',
) ??
varray[],
),
),
'parser' => TypeAssert\is_nullable_enum(
Parser::class,
Expand Down
7 changes: 3 additions & 4 deletions src/HHClientFallbackHandler.hack
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class HHClientFallbackHandler extends FailureHandler {
$this->map = $map;
$map['failure'] = inst_meth($this, 'handleFailure');
\HH\autoload_set_paths(
/* HH_IGNORE_ERROR[4110] shape as array */ $map,
/* HH_IGNORE_ERROR[4110] incorrect hhi */ $map,
Generated\root(),
);

Expand Down Expand Up @@ -230,7 +230,7 @@ class HHClientFallbackHandler extends FailureHandler {
return null;
}

$data = \json_decode($last, /* arrays = */ true);
$data = \json_decode($last, /* assoc = */ true);
if (!\is_array($data)) {
return null;
}
Expand All @@ -247,7 +247,6 @@ class HHClientFallbackHandler extends FailureHandler {
}

private function requireFile(string $path): void {
/* HH_IGNORE_ERROR[1002] */
require ($path);
require $path;
}
}
4 changes: 2 additions & 2 deletions src/Merger.hack
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ abstract final class Merger {
}

private static function mergeImpl(
Iterable<array<string, string>> $maps,
): array<string, string> {
Traversable<darray<string, string>> $maps,
): darray<string, string> {
$out = darray[];
foreach ($maps as $map) {
foreach ($map as $def => $file) {
Expand Down
6 changes: 3 additions & 3 deletions src/Writer.hack
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ final class Writer {
$add_failure_handler = \sprintf(
"if (%s::isEnabled()) {\n".
" \$handler = new %s();\n".
" \$map['failure'] = varray[\$handler, 'handleFailure'];\n".
" \HH\autoload_set_paths(/* HH_FIXME[4110] */ \$map, Generated\\root());\n".
" \$map['failure'] = inst_meth(\$handler, 'handleFailure');\n".
" \HH\autoload_set_paths(/* HH_FIXME[4110] incorrect hhi */ \$map, Generated\\root());\n".
" \$handler->initialize();\n".
"}",
$failure_handler,
Expand Down Expand Up @@ -236,7 +236,7 @@ function initialize(): void {
_Private\bootstrap();
\$map = Generated\\map();
\HH\autoload_set_paths(/* HH_FIXME[4110] */ \$map, Generated\\root());
\HH\autoload_set_paths(/* HH_FIXME[4110] incorrect hhi */ \$map, Generated\\root());
foreach (\spl_autoload_functions() ?: varray[] as \$autoloader) {
\spl_autoload_unregister(\$autoloader);
}
Expand Down
6 changes: 4 additions & 2 deletions src/builders/ComposerImporter.hack
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class ComposerImporter implements Builder {
}
$this->root = \dirname($path);
$composer_json = \file_get_contents($path);
$composer_config = \json_decode($composer_json, /* as array = */ true);
$composer_config = \json_decode($composer_json, /* assoc = */ true);
$composer_autoload = idx($composer_config, 'autoload');
if ($composer_autoload === null) {
return;
Expand Down Expand Up @@ -145,13 +145,15 @@ final class ComposerImporter implements Builder {

private static function normalizePSRRoots(
array<string, mixed> $roots,
): array<string, array<string>> {
): darray<string, varray<string>> {
$out = darray[];
foreach ($roots as $k => $v) {
if ($v is string) {
$out[$k] ??= varray[];
$out[$k][] = $v;
} else if (\is_array($v)) {
foreach ($v as $w) {
$out[$k] ??= varray[];
$out[$k][] = $w;
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/builders/FactParseScanner.hack
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,9 @@ final class FactParseScanner implements Builder {
}

public function getAutoloadMap(): AutoloadMap {
/* HH_FIXME[2049] no HHI for \HH\facts_parse */
/* HH_FIXME[4107] no HHI for \HH|facts_parse */
$facts = \HH\facts_parse(
$this->root,
$this->paths->toArray(),
$this->paths->toValuesArray(),
/* force_hh = */ false,
/* multithreaded = */ true,
);
Expand Down
2 changes: 1 addition & 1 deletion src/filters/BasePSRFilter.hack
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ abstract class BasePSRFilter implements Builder {
);

return shape(
'class' => $classes->toArray(),
'class' => darray($classes),
'function' => darray[],
'type' => darray[],
'constant' => darray[],
Expand Down

0 comments on commit 4f69e93

Please sign in to comment.