From 36542caeceba4874f837d8db0a5cf218d6e53b46 Mon Sep 17 00:00:00 2001 From: Dan Hemberger Date: Sun, 3 Jul 2022 15:24:23 -0700 Subject: [PATCH] Fix spl_autoload_register PHPStan warning > 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 https://github.com/smrealms/smr/issues/1183). 2. Remove the boolean return value from `get_class_loc`. This was a "speculative" feature added in 3f6854f2, 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. --- src/bootstrap.php | 2 +- src/lib/autoload.inc.php | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/bootstrap.php b/src/bootstrap.php index d36a9d6e8..3f6260d40 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -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'); diff --git a/src/lib/autoload.inc.php b/src/lib/autoload.inc.php index 117aa360d..1a95b9dc4 100644 --- a/src/lib/autoload.inc.php +++ b/src/lib/autoload.inc.php @@ -31,7 +31,7 @@ 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/']); @@ -39,10 +39,9 @@ function get_class_loc(string $className): bool { $classFile = LIB . $dir . $className . '.php'; if (is_file($classFile)) { require($classFile); - return true; + return; } } - return false; } /**