Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/stubs/Dockerfile_7.0
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ RUN chmod +x /usr/local/bin/install-php-extensions
RUN install-php-extensions memcached grpc soap swoole zookeeper
RUN install-php-extensions amqp apcu zmq ds event ev redis mongodb imagick pcntl pgsql intl gmp mbstring pdo_mysql xml dom iconv zip igbinary gd bcmath

RUN echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
RUN php -m | grep -q opcache || echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
2 changes: 1 addition & 1 deletion bin/stubs/Dockerfile_7.1
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ RUN chmod +x /usr/local/bin/install-php-extensions
RUN install-php-extensions memcached grpc soap swoole zookeeper
RUN install-php-extensions amqp apcu zmq ds event ev redis mongodb imagick pcntl pgsql intl gmp mbstring pdo_mysql xml dom iconv zip igbinary gd bcmath

RUN echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
RUN php -m | grep -q opcache || echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
2 changes: 1 addition & 1 deletion bin/stubs/Dockerfile_7.2
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ RUN chmod +x /usr/local/bin/install-php-extensions
RUN install-php-extensions memcached grpc soap swoole zookeeper
RUN install-php-extensions amqp apcu zmq ds event ev redis mongodb imagick pcntl pgsql intl gmp mbstring pdo_mysql xml dom iconv zip igbinary gd bcmath

RUN echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
RUN php -m | grep -q opcache || echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
2 changes: 1 addition & 1 deletion bin/stubs/Dockerfile_7.3
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ RUN chmod +x /usr/local/bin/install-php-extensions
RUN install-php-extensions memcached grpc soap swoole zookeeper
RUN install-php-extensions amqp apcu zmq ds event ev redis mongodb imagick pcntl pgsql intl gmp mbstring pdo_mysql xml dom iconv zip igbinary gd bcmath

RUN echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
RUN php -m | grep -q opcache || echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
2 changes: 1 addition & 1 deletion bin/stubs/Dockerfile_7.4
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ RUN chmod +x /usr/local/bin/install-php-extensions
RUN install-php-extensions memcached grpc soap swoole zookeeper
RUN install-php-extensions amqp apcu zmq ds event ev redis mongodb imagick pcntl ffi pgsql intl gmp mbstring pdo_mysql xml dom iconv zip igbinary gd bcmath

RUN echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
RUN php -m | grep -q opcache || echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
2 changes: 1 addition & 1 deletion bin/stubs/Dockerfile_8.0
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ RUN chmod +x /usr/local/bin/install-php-extensions
RUN install-php-extensions memcached grpc soap swoole zookeeper
RUN 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

RUN echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
RUN php -m | grep -q opcache || echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
2 changes: 1 addition & 1 deletion bin/stubs/Dockerfile_8.1
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ RUN chmod +x /usr/local/bin/install-php-extensions
RUN install-php-extensions memcached grpc soap swoole zookeeper
RUN 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

RUN echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
RUN php -m | grep -q opcache || echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
2 changes: 1 addition & 1 deletion bin/stubs/Dockerfile_8.2
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ RUN chmod +x /usr/local/bin/install-php-extensions
RUN install-php-extensions memcached grpc soap swoole zookeeper
RUN 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

RUN echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
RUN php -m | grep -q opcache || echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
2 changes: 1 addition & 1 deletion bin/stubs/Dockerfile_8.3
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ RUN chmod +x /usr/local/bin/install-php-extensions
RUN install-php-extensions memcached grpc soap swoole zookeeper
RUN 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

RUN echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
RUN php -m | grep -q opcache || echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
3 changes: 1 addition & 2 deletions bin/stubs/Dockerfile_8.4
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ FROM php:8.4-alpine
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/

RUN chmod +x /usr/local/bin/install-php-extensions

RUN install-php-extensions memcached grpc soap swoole zookeeper
RUN 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

RUN echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
RUN php -m | grep -q opcache || echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
10 changes: 6 additions & 4 deletions bin/stubs/Dockerfile_8.5
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ FROM php:8.5-alpine
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/

RUN chmod +x /usr/local/bin/install-php-extensions
#RUN install-php-extensions memcached grpc soap swoole zookeeper
# swoole doesn't support PHP 8.5 at the moment
RUN install-php-extensions memcached grpc soap zookeeper
# zmq isn't compatible with PHP 8.5
RUN install-php-extensions amqp apcu ds event ev redis mongodb imagick pcntl uv-beta ffi pgsql intl gmp mbstring pdo_mysql xml dom iconv zip igbinary gd bcmath

RUN install-php-extensions memcached grpc soap swoole zookeeper
RUN 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

RUN echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
RUN php -m | grep -q opcache || echo 'zend_extension=opcache' > /usr/local/etc/php/php.ini
45 changes: 39 additions & 6 deletions bin/stubs/build_docker.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,45 @@
#!/bin/bash -e
# $1: optional: default "local". Github user name, if container image should be published to container registry
# $2: optional: PHP version to build, true to rebuild all, false to rebuild missing only

VERSIONS="7.0 7.1 7.2 7.3 7.4 8.0 8.1 8.2 8.3 8.4 8.5"
ACTOR="${1:-local}"
rebuild="${2:-false}"

for f in $VERSIONS; do
{
docker buildx build --push --build-arg VERSION=$f . -f bin/stubs/Dockerfile_$f -t ghcr.io/$ACTOR/psalm:internal_stubs_$f
} &
if [ "$f" == "7.1" ] || [ "$f" == "7.3" ] || [ "$f" == "8.0" ] || [ "$f" == "8.2" ] || [ "$f" == "8.4" ]; then wait; fi
is_first=true
for file in bin/stubs/Dockerfile_*; do
# if none found - use shopt -s nullglob alternatively
if [[ ! -e "$file" ]]; then
break
fi

f="${file#bin/stubs/Dockerfile_}"
if [[ "$rebuild" != true ]] && [[ "$rebuild" != false ]] && [[ "$rebuild" != "$f" ]]; then
continue
fi

if [[ "$ACTOR" == "local" ]]
then
if [[ "$rebuild" == false ]] && docker image inspect "psalm:internal_stubs_${f}" > /dev/null 2>&1; then
continue
fi

if [[ "$rebuild" == false ]] && docker manifest inspect "ghcr.io/danog/psalm:internal_stubs_${f}" > /dev/null 2>&1; then
continue
fi

docker buildx build --load --build-arg VERSION="$f" . -f "bin/stubs/Dockerfile_${f}" -t "psalm:internal_stubs_${f}" &
elif [[ "$rebuild" == false ]] && docker manifest inspect "ghcr.io/${ACTOR}/psalm:internal_stubs_${f}" > /dev/null 2>&1; then
continue
else
docker buildx build --push --build-arg VERSION="$f" . -f "bin/stubs/Dockerfile_${f}" -t "ghcr.io/${ACTOR}/psalm:internal_stubs_${f}" &
fi

# run 2 at once at max
if [[ "$is_first" == true ]]; then
is_first=false
else
wait -n
fi
done

wait
61 changes: 43 additions & 18 deletions bin/stubs/gen_base_callmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@

declare(strict_types=1);

if (!isset($argv[1])) {
throw new Exception('Pass the expected PHP version, like 8.5, as 1st argument to this script');
exit(1);
}

if ($argv[1] !== PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION) {
$err = 'The script was called with PHP ' . PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . ', expected ' . $argv[1];
throw new Exception($err);
exit(1);
}

$callmap = [];

function namedTypeName(ReflectionNamedType $refl): string
Expand All @@ -19,17 +30,20 @@ function typeToString($reflection_type, string $defaultType): string
return $defaultType;
}

$maybe_add_null = false;
if ($reflection_type instanceof ReflectionNamedType) {
$type = $reflection_type->getName();
$maybe_add_null = true;
} elseif ($reflection_type instanceof ReflectionUnionType) {
$type = implode('|', array_map('namedTypeName', $reflection_type->getTypes()));
} elseif ($reflection_type instanceof ReflectionType) {
$type = $reflection_type->__toString();
$maybe_add_null = true;
} else {
throw new LogicException('Unexpected reflection class ' . get_class($reflection_type) . ' found.');
}

if ($reflection_type->allowsNull() && $type !== 'mixed') {
if ($maybe_add_null && $reflection_type->allowsNull() && $type !== 'mixed') {
$type .= '|null';
}

Expand Down Expand Up @@ -67,48 +81,57 @@ function paramsToEntries(ReflectionFunctionAbstract $reflectionFunction, string
}

// TEMP: not recommended, install the extension in the Dockerfile, instead
// ignore E_DEPRECATED errors for these
$previous = error_reporting();
error_reporting($previous & ~E_DEPRECATED & ~E_USER_DEPRECATED);
$temp_files = [];
foreach ([
'couchbase/couchbase.php',
'ibm_db2/ibm_db2.php',
] as $stub) {
if ($stub === 'ibm_db2/ibm_db2.php' && PHP_MAJOR_VERSION < 8) {
] as $stub_path) {
if ($stub_path === 'ibm_db2/ibm_db2.php' && PHP_MAJOR_VERSION < 8) {
continue;
}
$stub = file_get_contents("https://github.com/JetBrains/phpstorm-stubs/raw/refs/heads/master/$stub");
$stub = file_get_contents("https://github.com/JetBrains/phpstorm-stubs/raw/refs/heads/master/$stub_path");
if (PHP_MAJOR_VERSION === 7 && PHP_MINOR_VERSION === 0) {
$stub = str_replace(['?', '<php'], ['', '<?php'], $stub);
$stub = str_replace(['public const', 'protected const', 'private const'], 'const', $stub);
}
file_put_contents('temp.php', $stub);
require 'temp.php';
$temp_path = __DIR__ . '/temp-' . crc32($stub_path) . '-' . PHP_MAJOR_VERSION . '-' . PHP_MINOR_VERSION . '.php';
$temp_files[] = $temp_path;
file_put_contents($temp_path, $stub);
require $temp_path;
}
error_reporting($previous);

foreach (get_defined_functions() as $sub) {
foreach ($sub as $name) {
$name = strtolower($name);
if ($name === 'paramstoentries') {
continue;
}
if ($name === 'typetostring') {
continue;
}
if ($name === 'namedtypename') {
$func = new ReflectionFunction($name);

// skip any polyfills that may have been loaded
if (!$func->isInternal()) {
continue;
}
$func = new ReflectionFunction($name);

$args = paramsToEntries($func, 'mixed');

$callmap[$name] = $args;
$callmap[strtolower($name)] = $args;
}
}

foreach (get_declared_classes() as $class) {
foreach (array_merge(get_declared_interfaces(), get_declared_traits(), get_declared_classes()) as $class) {
$refl = new ReflectionClass($class);
if (!$refl->isInternal()) {
continue;
}

foreach ($refl->getMethods() as $method) {
if (!$method->isInternal()) {
continue;
}

$args = paramsToEntries($method, $method->getName() === '__construct' ? 'void' : 'mixed');

$callmap[strtolower($class.'::'.$method->getName())] = $args;
}
}
Expand All @@ -119,3 +142,5 @@ function paramsToEntries(ReflectionFunctionAbstract $reflectionFunction, string
$f = __DIR__.'/../../dictionaries/autogen/CallMap_'.PHP_MAJOR_VERSION.PHP_MINOR_VERSION.'.php';

file_put_contents($f, $payload);

array_map('unlink', $temp_files);
4 changes: 3 additions & 1 deletion bin/stubs/gen_callmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Psalm\Tests\TestConfig;
use Webmozart\Assert\Assert;

require 'vendor/autoload.php';
ini_set('memory_limit', '1024M');

require __DIR__ . '/../../vendor/autoload.php';
require __DIR__ . '/gen_callmap_utils.php';

BypassFinals::enable();
Expand Down
33 changes: 28 additions & 5 deletions bin/stubs/gen_callmap.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
#!/bin/bash -e

VERSIONS="7.0 7.1 7.2 7.3 7.4 8.0 8.1 8.2 8.3 8.4 8.5"
ACTOR="${1:-danog}"

for file in bin/stubs/Dockerfile_*; do
# if none found - use shopt -s nullglob alternatively
if [[ ! -e "$file" ]]; then
break
fi

for f in $VERSIONS; do
docker run --pull always --platform linux/amd64 --rm -v $PWD:/app ghcr.io/danog/psalm:internal_stubs_$f php /app/bin/stubs/gen_base_callmap.php &
f="${file#bin/stubs/Dockerfile_}"
echo "Starting ${f}"

pull="pull"
image="ghcr.io/${ACTOR}/psalm:internal_stubs_${f}"
if [[ "$ACTOR" == local ]] || ! docker manifest inspect "$image" > /dev/null 2>&1; then
# check locally
pull=""
image="psalm:internal_stubs_${f}"
if ! docker image inspect "$image" > /dev/null 2>&1; then
bash bin/stubs/build_docker.sh local "$f"
fi
fi

MSYS_NO_PATHCONV=1 docker run ${pull:+"--pull" always} --quiet --platform linux/amd64 --rm -v "${PWD}:/app" "$image" php /app/bin/stubs/gen_base_callmap.php "$f" &
done

wait

php bin/stubs/gen_callmap.php
php bin/stubs/gen_callmap.php
if [[ ! -d vendor ]]
then
MSYS_NO_PATHCONV=1 docker run --pull always --quiet --platform linux/amd64 --rm -v "${PWD}:/app" -w /app composer:latest composer install --no-progress
fi

# don't --pull but reuse the image we just downloaded
MSYS_NO_PATHCONV=1 docker run --quiet --platform linux/amd64 --rm -v "${PWD}:/app" -w /app "$image" php /app/bin/stubs/gen_callmap.php
Loading
Loading