Skip to content

Commit 8cfb3c1

Browse files
committed
Autogenerate base callmaps
1 parent bb092f7 commit 8cfb3c1

20 files changed

+199700
-82
lines changed

bin/Dockerfile

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
ARG VERSION
2-
FROM php:${VERSION}
2+
FROM php:${VERSION}-alpine
33

44
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
55

66
RUN chmod +x /usr/local/bin/install-php-extensions && \
7-
install-php-extensions pcntl uv-beta ffi pgsql intl gmp mbstring pdo_mysql xml dom iconv zip igbinary gd && \
8-
rm /usr/local/bin/install-php-extensions
7+
install-php-extensions amqp apcu zmq ds event ev redis mongodb imagick pcntl uv-beta ffi pgsql intl gmp mbstring pdo_mysql xml dom iconv zip igbinary gd bcmath
98

10-
ADD php.ini /usr/local/etc/php/php.ini
9+
RUN echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini

bin/gen_base_callmap.php

+55-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,58 @@
44

55
$callmap = [];
66

7-
require __DIR__ . '/gen_callmap_utils.php';
7+
function typeToString(?ReflectionType $reflection_type = null): string
8+
{
9+
if (!$reflection_type) {
10+
return 'string';
11+
}
12+
13+
if ($reflection_type instanceof ReflectionNamedType) {
14+
$type = $reflection_type->getName();
15+
} elseif ($reflection_type instanceof ReflectionUnionType) {
16+
$type = implode(
17+
'|',
18+
array_map(
19+
static fn(ReflectionNamedType $reflection): string => $reflection->getName(),
20+
$reflection_type->getTypes(),
21+
),
22+
);
23+
} else {
24+
throw new LogicException('Unexpected reflection class ' . $reflection_type::class . ' found.');
25+
}
26+
27+
if ($reflection_type->allowsNull()) {
28+
$type .= '|null';
29+
}
30+
31+
return $type;
32+
}
33+
34+
/**
35+
* @return array<string, array{byRef: bool, refMode: 'rw'|'w'|'r', variadic: bool, optional: bool, type: string}>
36+
*/
37+
function paramsToEntries(ReflectionFunctionAbstract $reflectionFunction): array
38+
{
39+
$res = [typeToString($reflectionFunction->getReturnType())];
40+
41+
foreach ($reflectionFunction->getParameters() as $param) {
42+
$key = $param->getName();
43+
if ($param->isPassedByReference()) {
44+
$key = "&$key";
45+
}
46+
if ($param->isVariadic()) {
47+
$key = "...$key";
48+
}
49+
if ($param->isOptional()) {
50+
$key .= '=';
51+
}
52+
53+
$res[$key] = typeToString($param->getType());
54+
}
55+
56+
return $res;
57+
}
58+
859

960
foreach (get_defined_functions()['internal'] as $name) {
1061
$func = new ReflectionFunction($name);
@@ -27,5 +78,6 @@
2778
}
2879
}
2980

30-
$callmap = normalizeCallMap($callmap);
31-
writeCallMap(__DIR__.'/../dictionaries/CallMap.php', $callmap);
81+
file_put_contents(__DIR__.'/../dictionaries/base/CallMap_'.PHP_MAJOR_VERSION.PHP_MINOR_VERSION.'.php', '<?php // phpcs:ignoreFile
82+
83+
return '.var_export($callmap, true).';');

bin/gen_base_callmap.sh

100644100755
+4-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
#!/bin/bash
1+
#!/bin/bash -e
22

3-
VERSIONS="7.0 7.1 7.2 7.3 7.4 8.0 8.1 8.2 8.3 8.4"
4-
5-
cd bin
3+
VERSIONS="8.0 8.1 8.2 8.3 8.4"
64

75
for f in $VERSIONS; do
8-
docker build --build-arg VERSION=$f . -t psalm_test_$f &
6+
docker build --build-arg VERSION=$f . -f bin/Dockerfile -t psalm_test_$f &
97
done
108

119
wait
1210

1311
for f in $VERSIONS; do
14-
12+
docker run --rm -it -v $PWD:/app psalm_test_$f php /app/bin/gen_base_callmap.php
1513
done

bin/gen_callmap_utils.php

+4-55
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
use Psalm\Tests\TestConfig;
1212
use Psalm\Type;
1313

14-
function internalNormalizeCallMap(array|string $callMap, string|int $key = 0): array|string {
14+
function internalNormalizeCallMap(array|string $callMap, string|int $key = 0): array|string
15+
{
1516
if (is_string($callMap)) {
1617
return Type::parseString($callMap === '' ? 'mixed' : $callMap)->getId(true);
1718
}
@@ -29,63 +30,11 @@ function internalNormalizeCallMap(array|string $callMap, string|int $key = 0): a
2930
return $new;
3031
}
3132

32-
function normalizeCallMap(array $callMap): array {
33-
return internalNormalizeCallMap($callMap);
34-
}
35-
36-
function typeToString(?ReflectionType $reflection_type = null): string
37-
{
38-
if (!$reflection_type) {
39-
return 'string';
40-
}
41-
42-
if ($reflection_type instanceof ReflectionNamedType) {
43-
$type = $reflection_type->getName();
44-
} elseif ($reflection_type instanceof ReflectionUnionType) {
45-
$type = implode(
46-
'|',
47-
array_map(
48-
static fn(ReflectionNamedType $reflection): string => $reflection->getName(),
49-
$reflection_type->getTypes(),
50-
),
51-
);
52-
} else {
53-
throw new LogicException('Unexpected reflection class ' . $reflection_type::class . ' found.');
54-
}
55-
56-
if ($reflection_type->allowsNull()) {
57-
$type .= '|null';
58-
}
59-
60-
return $type;
61-
}
62-
63-
/**
64-
* @return array<string, array{byRef: bool, refMode: 'rw'|'w'|'r', variadic: bool, optional: bool, type: string}>
65-
*/
66-
function paramsToEntries(ReflectionFunctionAbstract $reflectionFunction): array
33+
function normalizeCallMap(array $callMap): array
6734
{
68-
$res = [typeToString($reflectionFunction->getReturnType())];
69-
70-
foreach ($reflectionFunction->getParameters() as $param) {
71-
$key = $param->getName();
72-
if ($param->isPassedByReference()) {
73-
$key = "&$key";
74-
}
75-
if ($param->isVariadic()) {
76-
$key = "...$key";
77-
}
78-
if ($param->isOptional()) {
79-
$key .= '=';
80-
}
81-
82-
$res[$key] = typeToString($param->getType());
83-
}
84-
85-
return $res;
35+
return internalNormalizeCallMap($callMap);
8636
}
8737

88-
8938
/**
9039
* Returns the correct reflection type for function or method name.
9140
*/

bin/php.ini

-14
This file was deleted.

0 commit comments

Comments
 (0)