Skip to content

Commit

Permalink
Merge pull request #230 from woocommerce/24-12/php-84-deprecation
Browse files Browse the repository at this point in the history
Fix PHP 8.4 deprecation
  • Loading branch information
Luc45 authored Dec 16, 2024
2 parents cb7e315 + ff2f0bc commit cc7903c
Show file tree
Hide file tree
Showing 18 changed files with 1,225 additions and 221 deletions.
31 changes: 31 additions & 0 deletions .github/actions/restore-docker-cache/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Restore Docker Image Cache
description: Restores or builds docker image cache for a given PHP version
inputs:
php-version:
description: "PHP version to build/load the docker image for"
required: true
runs:
using: composite
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Restore Docker image cache
uses: actions/cache@v3
id: cache-docker
with:
path: qit-cli-tests-${{ inputs.php-version }}.tar
key: ${{ runner.os }}-qit-cli-tests-${{ inputs.php-version }}-${{ 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=${{ inputs.php-version }} -t qit-cli-tests:${{ inputs.php-version }} ./_build/docker/php
docker save qit-cli-tests:${{ inputs.php-version }} -o qit-cli-tests-${{ inputs.php-version }}.tar
shell: bash

- name: Load Docker image from cache
if: steps.cache-docker.outputs.cache-hit == 'true'
run: |
docker load -i qit-cli-tests-${{ inputs.php-version }}.tar
shell: bash
25 changes: 19 additions & 6 deletions .github/workflows/code-tests.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
name: QIT CLI Code tests
on:
workflow_call:
push:
pull_request:
paths:
- 'src/**.php'
workflow_dispatch:

concurrency:
group: code-tests-${{ github.ref }}
cancel-in-progress: true

jobs:
code_tests:
name: Code tests
runs-on: ubuntu-20.04
steps:
- name: Checkout code
uses: actions/checkout@v4
- uses: actions/cache@v3

- name: Restore Docker Cache (default PHP 8.3)
uses: ./.github/actions/restore-docker-cache
with:
php-version: "8.3"

- name: Cache composer dependencies
uses: actions/cache@v3
id: cache-composer
with:
path: src/vendor
key: ${{ runner.os }}-${{ hashFiles('src/composer.lock') }}

- name: Composer install
if: steps.cache-composer.outputs.cache-hit != 'true'
working-directory: src
run: composer install

- name: Run PHPCS
run: make phpcs

- name: Run PHPStan
run: make phpstan

- name: Run Phan
run: make phan
- name: Run PHPUnit
run: make phpunit
36 changes: 34 additions & 2 deletions .github/workflows/phan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,42 @@ on:
pull_request:
paths:
- 'src/**.php'
workflow_dispatch:

concurrency:
group: phan-tests-${{ github.ref }}
cancel-in-progress: true

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

- name: Restore Docker Cache
uses: ./.github/actions/restore-docker-cache
with:
php-version: ${{ matrix.php }}

- 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
36 changes: 34 additions & 2 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,42 @@ on:
pull_request:
paths:
- 'src/**.php'
workflow_dispatch:

concurrency:
group: phpunit-tests-${{ github.ref }}
cancel-in-progress: true

jobs:
phpunit_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

- name: Restore Docker Cache
uses: ./.github/actions/restore-docker-cache
with:
php-version: ${{ matrix.php }}

- 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 PHPUnit tests
env:
PHP_VERSION: ${{ matrix.php }}
run: make phpunit
32 changes: 32 additions & 0 deletions .github/workflows/restore-docker-cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Restore Docker Image Cache
on:
workflow_call:
inputs:
php-version:
required: true
type: string

jobs:
restore-docker-cache:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Restore Docker image cache
uses: actions/cache@v3
id: cache-docker
with:
path: qit-cli-tests-${{ inputs.php-version }}.tar
key: ${{ runner.os }}-qit-cli-tests-${{ inputs.php-version }}-${{ 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=${{ inputs.php-version }} -t qit-cli-tests:${{ inputs.php-version }} ./_build/docker/php
docker save qit-cli-tests:${{ inputs.php-version }} -o qit-cli-tests-${{ inputs.php-version }}.tar
- name: Load Docker image from cache
if: steps.cache-docker.outputs.cache-hit == 'true'
run: |
docker load -i qit-cli-tests-${{ inputs.php-version }}.tar
87 changes: 62 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,37 @@ DEBUG ?= 0
ARGS ?=
VERSION ?= qit_dev_build

# List all PHP versions you want to test/build images for:
PHP_VERSIONS = 7.2 7.3 7.4 8.0 8.1 8.2 8.3 8.4
# Default PHP version used if none specified
PHP_VERSION ?= 8.3

ifeq (1, $(ROOT))
DOCKER_USER ?= "0:0"
else
DOCKER_USER ?= "$(shell id -u):$(shell id -g)"
endif

## Run a command inside an alpine PHP 8 CLI image.
## 1. Command to execute, eg: "./vendor/bin/phpcs" 2. Working dir (optional)
## Run a command inside a PHP CLI Docker image built for a specific PHP_VERSION.
## 1. Command to execute, e.g.: "php /app/src/vendor/bin/phpcs"
## 2. Working dir (optional)
define execPhpAlpine
@docker image inspect qit-cli-php-xdebug-pcntl > /dev/null 2>&1 || docker build --build-arg CI=${CI} -t qit-cli-php-xdebug-pcntl ./_build/docker/php83
docker run --rm \
--user $(DOCKER_USER) \
-v "${PWD}:/app" \
-v "${PWD}/_build/docker/php83/ini/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini" \
--env QIT_HOME=/tmp \
--env PHP_IDE_CONFIG=serverName=qit_cli \
--workdir "$(2:=/)" \
--add-host host.docker.internal:host-gateway \
qit-cli-php-xdebug-pcntl \
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 All @@ -44,14 +55,11 @@ build:
composer \
install --no-dev --quiet --optimize-autoloader --ignore-platform-reqs

# Create a temporary configuration file with the specified VERSION
@sed "s/QIT_VERSION_REPLACE/$(VERSION)/g" ./_build/box.json.dist > ./_build/box.json

# Ensure the Docker image is built and run Box with the temporary configuration file
@docker images -q | grep qit-cli-box || docker build -t qit-cli-box ./_build/docker/box
@docker run --rm -v ${PWD}:${PWD} -w ${PWD} -u "$(shell id -u):$(shell id -g)" qit-cli-box ./_build/box.phar compile -c ./_build/box.json --no-parallel || rm -rf src-tmp

# Clean up the temporary directory and configuration file
@rm -rf src-tmp
@rm -f ./_build/box.json

Expand All @@ -61,22 +69,51 @@ 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..."; \
docker image inspect qit-cli-tests:$$ver >/dev/null 2>&1 || (echo "Image not found for $$ver, building..." && docker build --build-arg CI=${CI} --build-arg PHP_VERSION=$$ver -t qit-cli-tests:$$ver ./_build/docker/php); \
docker run --rm qit-cli-tests:$$ver php -v; \
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)
# PS: To update Phan, run: docker image pull phanphp/phan:latest
$(call execPhpAlpine,php -d xdebug.mode=off /app/src/vendor/bin/phan $(ARGS),/app/src)
17 changes: 17 additions & 0 deletions _build/docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Use an ARG for the PHP version. Default to 8.3 if not provided.
ARG PHP_VERSION=8.3
FROM php:${PHP_VERSION}-cli

ARG CI
RUN apt-get update \
&& apt-get install -y libzip-dev \
&& 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; \
fi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
xdebug.client_host = host.docker.internal
xdebug.client_port = 9003
xdebug.output_dir = /app/_build/docker/php83/xdebug/output
xdebug.log = /app/_build/docker/php83/xdebug/xdebug.log
xdebug.output_dir = /app/_build/docker/php/xdebug/output
xdebug.log = /app/_build/docker/php/xdebug/xdebug.log
# Enabled at runtime with DEBUG=1
xdebug.start_with_request = no

Expand Down
File renamed without changes.
File renamed without changes.
Empty file.
15 changes: 0 additions & 15 deletions _build/docker/php83/Dockerfile

This file was deleted.

Binary file modified qit
Binary file not shown.
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
Loading

0 comments on commit cc7903c

Please sign in to comment.