Skip to content

Commit

Permalink
Also run Phan on multiple PHP versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Luc45 committed Dec 14, 2024
1 parent f1be510 commit 5ac8120
Show file tree
Hide file tree
Showing 7 changed files with 937 additions and 29 deletions.
46 changes: 44 additions & 2 deletions .github/workflows/phan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,52 @@ on:
pull_request:
paths:
- 'src/**.php'
workflow_dispatch:

jobs:
phan_tests:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4']

steps:
- name: Echo Bogus
run: echo "Bogus workflow to allow PR-based testing."
- name: Checkout code
uses: actions/checkout@v4

# Restore Docker image cache for qit-cli-tests:<version>
- name: Restore Docker image cache
uses: actions/cache@v3
id: cache-docker
with:
path: qit-cli-tests-${{ matrix.php }}.tar
key: ${{ runner.os }}-qit-cli-tests-${{ matrix.php }}-${{ hashFiles('_build/docker/php/**') }}

- name: Build Docker image if needed
if: steps.cache-docker.outputs.cache-hit != 'true'
run: |
docker build --build-arg CI=true --build-arg PHP_VERSION=${{ matrix.php }} -t qit-cli-tests:${{ matrix.php }} ./_build/docker/php
docker save qit-cli-tests:${{ matrix.php }} -o qit-cli-tests-${{ matrix.php }}.tar
- name: Load Docker image from cache
if: steps.cache-docker.outputs.cache-hit == 'true'
run: |
docker load -i qit-cli-tests-${{ matrix.php }}.tar
- name: Cache composer dependencies
uses: actions/cache@v3
id: cache-composer
with:
path: src/vendor
key: ${{ runner.os }}-php-${{ matrix.php }}-${{ hashFiles('src/composer.lock') }}

- name: Composer install
if: steps.cache-composer.outputs.cache-hit != 'true'
run: |
cd src
composer install --no-interaction --no-suggest --prefer-dist
- name: Run Phan
env:
PHP_VERSION: ${{ matrix.php }}
run: make phan
63 changes: 41 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@ DOCKER_USER ?= "$(shell id -u):$(shell id -g)"
endif

## Run a command inside a PHP CLI Docker image built for a specific PHP_VERSION.
## 1. Command to execute, e.g.: "./vendor/bin/phpcs"
## 1. Command to execute, e.g.: "php /app/src/vendor/bin/phpcs"
## 2. Working dir (optional)
define execPhpAlpine
@docker image inspect qit-cli-tests:$(PHP_VERSION) > /dev/null 2>&1 || \
(echo "Docker image not found. Building qit-cli-tests:$(PHP_VERSION)..." && \
docker build --build-arg CI=${CI} --build-arg PHP_VERSION=$(PHP_VERSION) -t qit-cli-tests:$(PHP_VERSION) ./_build/docker/php)

@docker run --rm \
--user $(DOCKER_USER) \
-v "${PWD}:/app" \
-v "${PWD}/_build/docker/php/ini/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini" \
--env QIT_HOME=/tmp \
--env PHP_IDE_CONFIG=serverName=qit_cli \
--workdir "$(2:=/)" \
qit-cli-tests:$(PHP_VERSION) \
bash -c "php -d xdebug.start_with_request=$(if $(filter 1,$(DEBUG)),yes,no) -d memory_limit=1G $(1)"
@docker image inspect qit-cli-tests:$(PHP_VERSION) > /dev/null 2>&1 || \
(echo "Docker image not found. Building qit-cli-tests:$(PHP_VERSION)..." && \
docker build --build-arg CI=${CI} --build-arg PHP_VERSION=$(PHP_VERSION) -t qit-cli-tests:$(PHP_VERSION) ./_build/docker/php)

@docker run --rm \
--user $(DOCKER_USER) \
-v "${PWD}:/app" \
-v "${PWD}/_build/docker/php/ini/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini" \
--env QIT_HOME=/tmp \
--env PHP_IDE_CONFIG=serverName=qit_cli \
--env TARGET_PHP_VERSION=$(PHP_VERSION) \
--env PHAN_DISABLE_XDEBUG_WARN=1 \
--env PHAN_ALLOW_XDEBUG=1 \
--workdir "$(2:=/)" \
qit-cli-tests:$(PHP_VERSION) \
bash -c "$(1)"
endef

watch:
Expand Down Expand Up @@ -66,25 +69,38 @@ tests:
$(MAKE) phpunit
$(MAKE) phan

tests-all:
$(MAKE) phpcs
$(MAKE) phpstan
$(MAKE) phpunit-all
$(MAKE) phan-all

phpcbf:
$(call execPhpAlpine,/app/src/vendor/bin/phpcbf /app/src/qit-cli.php /app/src/src -s --standard=/app/src/.phpcs.xml.dist)
$(call execPhpAlpine,php /app/src/vendor/bin/phpcbf /app/src/qit-cli.php /app/src/src -s --standard=/app/src/.phpcs.xml.dist)

phpcs:
$(MAKE) phpcbf || true
$(call execPhpAlpine,/app/src/vendor/bin/phpcs /app/src/qit-cli.php /app/src/src -s --standard=/app/src/.phpcs.xml.dist)
$(call execPhpAlpine,php /app/src/vendor/bin/phpcs /app/src/qit-cli.php /app/src/src -s --standard=/app/src/.phpcs.xml.dist)

# Added --memory-limit=1G here
phpstan:
$(call execPhpAlpine,/app/src/vendor/bin/phpstan -vvv analyse -c /app/src/phpstan.neon)
$(call execPhpAlpine,php /app/src/vendor/bin/phpstan -vvv analyse -c /app/src/phpstan.neon --memory-limit=1G)

phpunit:
$(call execPhpAlpine,/app/src/vendor/bin/phpunit -c /app/src/phpunit.xml.dist $(ARGS))
$(call execPhpAlpine,php /app/src/vendor/bin/phpunit -c /app/src/phpunit.xml.dist $(ARGS),/app/src)

phpunit-all:
@for ver in $(PHP_VERSIONS); do \
echo "Running PHPUnit on PHP $$ver..."; \
$(MAKE) phpunit PHP_VERSION=$$ver || exit 1; \
done

phan-all:
@for ver in $(PHP_VERSIONS); do \
echo "Running Phan on PHP $$ver..."; \
$(MAKE) phan PHP_VERSION=$$ver || exit 1; \
done

check-php-versions:
@for ver in $(PHP_VERSIONS); do \
echo "Checking PHP version in qit-cli-tests:$$ver..."; \
Expand All @@ -93,8 +109,11 @@ check-php-versions:
echo "-----------------------------------"; \
done

rebuild-images:
@for ver in $(PHP_VERSIONS); do \
echo "Rebuilding qit-cli-tests:$$ver..."; \
docker build --no-cache --build-arg CI=${CI} --build-arg PHP_VERSION=$$ver -t qit-cli-tests:$$ver ./_build/docker/php || exit 1; \
done

phan:
docker run --rm \
-v ${PWD}/src:/mnt/src \
-u "$$(id -u):$$(id -g)" \
phanphp/phan:latest $(ARGS)
$(call execPhpAlpine,php -d xdebug.mode=off /app/src/vendor/bin/phan $(ARGS),/app/src)
3 changes: 3 additions & 0 deletions _build/docker/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ RUN apt-get update \
&& docker-php-ext-install zip pcntl \
&& docker-php-ext-enable zip pcntl

# Install php-ast
RUN pecl install ast && docker-php-ext-enable ast

# Install Xdebug only if not in CI environment and PHP major version >= 8
RUN if [ "$CI" != "true" ] && [ "${PHP_VERSION%%.*}" -ge 8 ]; then \
pecl install xdebug && docker-php-ext-enable xdebug; \
Expand Down
2 changes: 1 addition & 1 deletion src/.phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
// Note that the **only** effect of choosing `'5.6'` is to infer that functions removed in php 7.0 exist.
// (See `backward_compatibility_checks` for additional options)
// Automatically inferred from composer.json requirement for "php" of "^7.2.5 | ^8"
'target_php_version' => '7.2',
'target_php_version' => getenv( 'TARGET_PHP_VERSION' ) ?: '7.2',

// If enabled, missing properties will be created when
// they are first seen. If false, we'll report an
Expand Down
3 changes: 2 additions & 1 deletion src/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"dealerdirect/phpcodesniffer-composer-installer": "^1",
"wp-coding-standards/wpcs": "dev-develop",
"phpcompatibility/php-compatibility": "^9",
"spatie/phpunit-snapshot-assertions": "^3.0"
"spatie/phpunit-snapshot-assertions": "^3.0",
"phan/phan": "^5.4"
}
}
Loading

0 comments on commit 5ac8120

Please sign in to comment.