|
3 | 3 |
|
4 | 4 | namespace TheCodingMachine\Safe\PHPStan\Utils; |
5 | 5 |
|
6 | | -use PHPStan\Analyser\Scope; |
7 | | -use PHPStan\Reflection\FunctionReflection; |
8 | | -use PHPStan\Reflection\MethodReflection; |
9 | | - |
10 | 6 | class FunctionListLoader |
11 | 7 | { |
12 | 8 | /** |
13 | | - * @var string[] |
| 9 | + * @var array<string, string> |
14 | 10 | */ |
15 | | - private static $functions; |
| 11 | + private static array $functions; |
16 | 12 |
|
17 | 13 | /** |
18 | | - * @return string[] |
| 14 | + * @return array<string, string> |
19 | 15 | */ |
20 | 16 | public static function getFunctionList(): array |
21 | 17 | { |
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)); |
29 | 43 | } |
| 44 | + |
30 | 45 | // Let's index these functions by their name |
31 | | - self::$functions = \Safe\array_combine($functions, $functions); |
| 46 | + $indexedFunctions[$function] = $function; |
32 | 47 | } |
33 | | - return self::$functions; |
| 48 | + |
| 49 | + return $indexedFunctions; |
34 | 50 | } |
35 | 51 | } |
0 commit comments