|
| 1 | +<?php // phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols |
| 2 | + |
| 3 | + |
| 4 | +declare(strict_types=1); |
| 5 | + |
| 6 | +$callmap = []; |
| 7 | + |
| 8 | +function namedTypeName(ReflectionNamedType $refl): string |
| 9 | +{ |
| 10 | + return $refl->getName(); |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * @psalm-param ?ReflectionType $reflection_type |
| 15 | + */ |
| 16 | +function typeToString($reflection_type, string $defaultType): string |
| 17 | +{ |
| 18 | + if (!$reflection_type) { |
| 19 | + return $defaultType; |
| 20 | + } |
| 21 | + |
| 22 | + if ($reflection_type instanceof ReflectionNamedType) { |
| 23 | + $type = $reflection_type->getName(); |
| 24 | + } elseif ($reflection_type instanceof ReflectionUnionType) { |
| 25 | + $type = implode('|', array_map('namedTypeName', $reflection_type->getTypes())); |
| 26 | + } elseif ($reflection_type instanceof ReflectionType) { |
| 27 | + $type = $reflection_type->__toString(); |
| 28 | + } else { |
| 29 | + throw new LogicException('Unexpected reflection class ' . get_class($reflection_type) . ' found.'); |
| 30 | + } |
| 31 | + |
| 32 | + if ($reflection_type->allowsNull() && $type !== 'mixed') { |
| 33 | + $type .= '|null'; |
| 34 | + } |
| 35 | + |
| 36 | + return $type; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * @return array<string, array{byRef: bool, refMode: 'rw'|'w'|'r', variadic: bool, optional: bool, type: string}> |
| 41 | + */ |
| 42 | +function paramsToEntries(ReflectionFunctionAbstract $reflectionFunction, string $defaultReturnType): array |
| 43 | +{ |
| 44 | + // phpcs:disable SlevomatCodingStandard.Numbers.RequireNumericLiteralSeparator.RequiredNumericLiteralSeparator |
| 45 | + $res = PHP_VERSION_ID >= 80100 ? ( |
| 46 | + $reflectionFunction->getTentativeReturnType() ?? $reflectionFunction->getReturnType() |
| 47 | + ) : $reflectionFunction->getReturnType(); |
| 48 | + |
| 49 | + $res = [typeToString($res, $defaultReturnType)]; |
| 50 | + |
| 51 | + foreach ($reflectionFunction->getParameters() as $param) { |
| 52 | + $key = $param->getName(); |
| 53 | + if ($param->isVariadic()) { |
| 54 | + $key = "...$key"; |
| 55 | + } |
| 56 | + if ($param->isPassedByReference()) { |
| 57 | + $key = "&$key"; |
| 58 | + } |
| 59 | + if ($param->isOptional()) { |
| 60 | + $key .= '='; |
| 61 | + } |
| 62 | + |
| 63 | + $res[$key] = typeToString($param->getType(), 'mixed'); |
| 64 | + } |
| 65 | + |
| 66 | + return $res; |
| 67 | +} |
| 68 | + |
| 69 | +// TEMP: not recommended, install the extension in the Dockerfile, instead |
| 70 | +foreach ([ |
| 71 | + 'couchbase/couchbase.php', |
| 72 | + 'ibm_db2/ibm_db2.php', |
| 73 | +] as $stub) { |
| 74 | + if ($stub === 'ibm_db2/ibm_db2.php' && PHP_MAJOR_VERSION < 8) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + $stub = file_get_contents("https://github.com/JetBrains/phpstorm-stubs/raw/refs/heads/master/$stub"); |
| 78 | + if (PHP_MAJOR_VERSION === 7 && PHP_MINOR_VERSION === 0) { |
| 79 | + $stub = str_replace(['?', '<php'], ['', '<?php'], $stub); |
| 80 | + $stub = str_replace(['public const', 'protected const', 'private const'], 'const', $stub); |
| 81 | + } |
| 82 | + file_put_contents('temp.php', $stub); |
| 83 | + require 'temp.php'; |
| 84 | +} |
| 85 | + |
| 86 | +foreach (get_defined_functions() as $sub) { |
| 87 | + foreach ($sub as $name) { |
| 88 | + $name = strtolower($name); |
| 89 | + if ($name === 'paramstoentries') { |
| 90 | + continue; |
| 91 | + } |
| 92 | + if ($name === 'typetostring') { |
| 93 | + continue; |
| 94 | + } |
| 95 | + if ($name === 'namedtypename') { |
| 96 | + continue; |
| 97 | + } |
| 98 | + $func = new ReflectionFunction($name); |
| 99 | + |
| 100 | + $args = paramsToEntries($func, 'mixed'); |
| 101 | + |
| 102 | + $callmap[$name] = $args; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +foreach (get_declared_classes() as $class) { |
| 107 | + $refl = new ReflectionClass($class); |
| 108 | + |
| 109 | + foreach ($refl->getMethods() as $method) { |
| 110 | + $args = paramsToEntries($method, $method->getName() === '__construct' ? 'void' : 'mixed'); |
| 111 | + |
| 112 | + $callmap[strtolower($class.'::'.$method->getName())] = $args; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +$payload = '<?php // phpcs:ignoreFile |
| 117 | +
|
| 118 | +return '.var_export($callmap, true).';'; |
| 119 | +$f = __DIR__.'/../dictionaries/autogen/CallMap_'.PHP_MAJOR_VERSION.PHP_MINOR_VERSION.'.php'; |
| 120 | + |
| 121 | +file_put_contents($f, $payload); |
0 commit comments