Skip to content

Commit 69f7d41

Browse files
committed
Refactor FunctionListLoader to avoid PHPStan errors
1 parent 23fe3a8 commit 69f7d41

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

src/Utils/FunctionListLoader.php

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,49 @@
33

44
namespace TheCodingMachine\Safe\PHPStan\Utils;
55

6-
use PHPStan\Analyser\Scope;
7-
use PHPStan\Reflection\FunctionReflection;
8-
use PHPStan\Reflection\MethodReflection;
9-
106
class FunctionListLoader
117
{
128
/**
13-
* @var string[]
9+
* @var array<string, string>
1410
*/
15-
private static $functions;
11+
private static array $functions;
1612

1713
/**
18-
* @return string[]
14+
* @return array<string, string>
1915
*/
2016
public static function getFunctionList(): array
2117
{
22-
if (self::$functions === null) {
23-
if (\file_exists(__DIR__.'/../../../safe/generated/functionsList.php')) {
24-
$functions = require __DIR__.'/../../../safe/generated/functionsList.php';
25-
} elseif (\file_exists(__DIR__.'/../../vendor/thecodingmachine/safe/generated/functionsList.php')) {
26-
$functions = require __DIR__.'/../../vendor/thecodingmachine/safe/generated/functionsList.php';
27-
} else {
28-
throw new \RuntimeException('Could not find thecodingmachine/safe\'s functionsList.php file.');
18+
return self::$functions ??= self::fetchIndexedFunctions();
19+
}
20+
21+
/**
22+
* @return array<string, string>
23+
*/
24+
private static function fetchIndexedFunctions(): array
25+
{
26+
if (\file_exists(__DIR__ . '/../../../safe/generated/functionsList.php')) {
27+
$functions = require __DIR__ . '/../../../safe/generated/functionsList.php';
28+
} elseif (\file_exists(__DIR__ . '/../../vendor/thecodingmachine/safe/generated/functionsList.php')) {
29+
$functions = require __DIR__ . '/../../vendor/thecodingmachine/safe/generated/functionsList.php';
30+
} else {
31+
throw new \RuntimeException('Could not find thecodingmachine/safe\'s functionsList.php file.');
32+
}
33+
34+
if (!is_array($functions)) {
35+
throw new \RuntimeException('The functions list should be an array.');
36+
}
37+
38+
$indexedFunctions = [];
39+
40+
foreach ($functions as $function) {
41+
if (!is_string($function)) {
42+
throw new \RuntimeException('The functions list should contain only strings, got ' . get_debug_type($function));
2943
}
44+
3045
// Let's index these functions by their name
31-
self::$functions = \Safe\array_combine($functions, $functions);
46+
$indexedFunctions[$function] = $function;
3247
}
33-
return self::$functions;
48+
49+
return $indexedFunctions;
3450
}
3551
}

0 commit comments

Comments
 (0)