Skip to content

Commit

Permalink
Fix spl_autoload_register PHPStan warning
Browse files Browse the repository at this point in the history
> Parameter #1 $callback of function spl_autoload_register expects
> (callable(string): void)|null, Closure(string): bool given.

Two changes were needed to match the parameter type expected by
PHPStan:

1. Promote the 'get_class_loc' string to first-class callable syntax
   (see smrealms#1183).

2. Remove the boolean return value from `get_class_loc`. This was a
   "speculative" feature added in 3f6854f, but it doesn't conform to
   the expected interface nor is it needed for the autoloader to throw
   an Error if the class is not found.
  • Loading branch information
hemberger committed Jul 5, 2022
1 parent af0f2ba commit 36542ca
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function array_rand_value(array $arr): mixed {
// Set up vendor and class autoloaders
require_once(ROOT . 'vendor/autoload.php');
require_once(LIB . 'autoload.inc.php');
spl_autoload_register('get_class_loc');
spl_autoload_register(get_class_loc(...));

// Load common functions
require_once(LIB . 'Default/smr.inc.php');
Expand Down
5 changes: 2 additions & 3 deletions src/lib/autoload.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,17 @@ function get_game_dir(): string {
* Includes the correct game-specific version of a class file.
* Try to avoid calling this before `$overrideGameID` is set!
*/
function get_class_loc(string $className): bool {
function get_class_loc(string $className): void {
$className = str_replace('\\', DIRECTORY_SEPARATOR, $className);
// Fallback to Default directory
$dirs = array_unique([get_game_dir(), 'Default/']);
foreach ($dirs as $dir) {
$classFile = LIB . $dir . $className . '.php';
if (is_file($classFile)) {
require($classFile);
return true;
return;
}
}
return false;
}

/**
Expand Down

0 comments on commit 36542ca

Please sign in to comment.