From b0da9b81d5de3c6648dab26952a35cb70702f22a Mon Sep 17 00:00:00 2001 From: julien Date: Fri, 6 Feb 2026 17:10:29 +0900 Subject: [PATCH] feat(php 8.5): Handle deprecated warnings --- .github/workflows/coding-standards.yml | 8 ++++++-- .github/workflows/php-sdk-development-tests.yml | 2 +- .github/workflows/unit-and-integration-test.yml | 2 +- src/Client/AbstractClient.php | 3 +++ src/Client/CapiHandler/CapiHandlerInterface.php | 1 + src/Client/CapiHandler/Curl.php | 7 ++++++- src/Client/CapiHandler/FileGetContents.php | 4 ++++ src/Configuration/Signal.php | 1 + src/Configuration/Signal/Decisions.php | 2 ++ src/Configuration/Signal/Source.php | 1 + src/Configuration/Watcher.php | 1 + src/Storage/FileStorage.php | 10 ++++++++++ src/Storage/StorageInterface.php | 2 ++ src/Watcher.php | 2 ++ tests/PHPUnitUtil.php | 5 ++++- tools/coding-standards/composer.json | 4 ++-- tools/coding-standards/psalm/psalm.xml | 2 ++ 17 files changed, 49 insertions(+), 8 deletions(-) diff --git a/.github/workflows/coding-standards.yml b/.github/workflows/coding-standards.yml index 986e49a..944cff7 100644 --- a/.github/workflows/coding-standards.yml +++ b/.github/workflows/coding-standards.yml @@ -5,6 +5,11 @@ on: - main paths-ignore: - '**.md' + pull_request: + branches: + - main + paths-ignore: + - '**.md' workflow_dispatch: inputs: coverage_report: @@ -20,7 +25,7 @@ jobs: strategy: fail-fast: false matrix: - php-version: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4'] + php-version: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5'] name: Coding standards test runs-on: ubuntu-latest @@ -77,7 +82,6 @@ jobs: run: ddev phpstan /var/www/html/${{env.EXTENSION_PATH}}/tools/coding-standards phpstan/phpstan.neon /var/www/html/${{env.EXTENSION_PATH}}/src - name: Run PSALM - if: contains(fromJson('["7.4","8.0","8.1","8.2","8.3"]'),matrix.php-version) run: ddev psalm ./${{env.EXTENSION_PATH}}/tools/coding-standards /var/www/html/${{env.EXTENSION_PATH}}/tools/coding-standards/psalm - name: Prepare integration tests diff --git a/.github/workflows/php-sdk-development-tests.yml b/.github/workflows/php-sdk-development-tests.yml index faa0a6d..abc054e 100644 --- a/.github/workflows/php-sdk-development-tests.yml +++ b/.github/workflows/php-sdk-development-tests.yml @@ -47,7 +47,7 @@ jobs: strategy: fail-fast: false matrix: - php-version: ["7.2", "7.3", "7.4", "8.0", "8.1", "8.2", "8.3", "8.4"] + php-version: ["7.2", "7.3", "7.4", "8.0", "8.1", "8.2", "8.3", "8.4", "8.5"] name: Unit and integration test runs-on: ubuntu-latest diff --git a/.github/workflows/unit-and-integration-test.yml b/.github/workflows/unit-and-integration-test.yml index 947842b..386ef42 100644 --- a/.github/workflows/unit-and-integration-test.yml +++ b/.github/workflows/unit-and-integration-test.yml @@ -35,7 +35,7 @@ jobs: strategy: fail-fast: false matrix: - php-version: ['7.2','7.3','7.4','8.0','8.1','8.2','8.3', '8.4'] + php-version: ['7.2','7.3','7.4','8.0','8.1','8.2','8.3', '8.4', '8.5'] name: Unit and integration test runs-on: ubuntu-latest diff --git a/src/Client/AbstractClient.php b/src/Client/AbstractClient.php index 7f7f656..c314e9f 100644 --- a/src/Client/AbstractClient.php +++ b/src/Client/AbstractClient.php @@ -36,6 +36,9 @@ public function __construct( parent::__construct($configs, $this->capiHandler, $logger); } + /** + * @psalm-api + */ public function getCapiHandler(): CapiHandlerInterface { return $this->capiHandler; diff --git a/src/Client/CapiHandler/CapiHandlerInterface.php b/src/Client/CapiHandler/CapiHandlerInterface.php index 93d2cc5..ff94ce6 100644 --- a/src/Client/CapiHandler/CapiHandlerInterface.php +++ b/src/Client/CapiHandler/CapiHandlerInterface.php @@ -20,6 +20,7 @@ interface CapiHandlerInterface extends RequestHandlerInterface { /** * Retrieve decisions list from a blocklist url. + * @psalm-api */ public function getListDecisions(string $url, array $headers = []): string; } diff --git a/src/Client/CapiHandler/Curl.php b/src/Client/CapiHandler/Curl.php index bb62c72..f14fe97 100644 --- a/src/Client/CapiHandler/Curl.php +++ b/src/Client/CapiHandler/Curl.php @@ -16,9 +16,12 @@ * * @copyright Copyright (c) 2022+ CrowdSec * @license MIT License + * + * @psalm-api */ class Curl extends CommonCurl implements CapiHandlerInterface { + #[\Override] public function getListDecisions(string $url, array $headers = []): string { $handle = curl_init(); @@ -30,7 +33,9 @@ public function getListDecisions(string $url, array $headers = []): string $statusCode = $this->getResponseHttpCode($handle); - curl_close($handle); + if (PHP_VERSION_ID < 80000) { + curl_close($handle); + } return 200 === $statusCode ? (string) $response : ''; } diff --git a/src/Client/CapiHandler/FileGetContents.php b/src/Client/CapiHandler/FileGetContents.php index 96f10e9..d28e803 100644 --- a/src/Client/CapiHandler/FileGetContents.php +++ b/src/Client/CapiHandler/FileGetContents.php @@ -16,9 +16,13 @@ * * @copyright Copyright (c) 2022+ CrowdSec * @license MIT License + * + * @psalm-api + * */ class FileGetContents extends CommonFileGetContents implements CapiHandlerInterface { + #[\Override] public function getListDecisions(string $url, array $headers = []): string { $config = $this->createListContextConfig($headers); diff --git a/src/Configuration/Signal.php b/src/Configuration/Signal.php index 349c9f6..3332e29 100644 --- a/src/Configuration/Signal.php +++ b/src/Configuration/Signal.php @@ -43,6 +43,7 @@ class Signal extends AbstractConfiguration * @throws \RuntimeException * @throws \InvalidArgumentException */ + #[\Override] public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('signalConfig'); diff --git a/src/Configuration/Signal/Decisions.php b/src/Configuration/Signal/Decisions.php index 6e4cd56..dec1c51 100644 --- a/src/Configuration/Signal/Decisions.php +++ b/src/Configuration/Signal/Decisions.php @@ -40,6 +40,7 @@ class Decisions extends AbstractConfiguration * Keep only necessary configs * Override because $configs is an array of array (decision) and we want to clean each decision. */ + #[\Override] public function cleanConfigs(array $configs): array { $result = []; @@ -54,6 +55,7 @@ public function cleanConfigs(array $configs): array * @throws \InvalidArgumentException * @throws \RuntimeException */ + #[\Override] public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('signalDecisionsConfig'); diff --git a/src/Configuration/Signal/Source.php b/src/Configuration/Signal/Source.php index 6484c8a..3969865 100644 --- a/src/Configuration/Signal/Source.php +++ b/src/Configuration/Signal/Source.php @@ -33,6 +33,7 @@ class Source extends AbstractConfiguration 'as_number', ]; + #[\Override] public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('signalSourceConfig'); diff --git a/src/Configuration/Watcher.php b/src/Configuration/Watcher.php index d2a1db0..923d866 100644 --- a/src/Configuration/Watcher.php +++ b/src/Configuration/Watcher.php @@ -40,6 +40,7 @@ class Watcher extends AbstractConfiguration * @throws \InvalidArgumentException * @throws \RuntimeException */ + #[\Override] public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('watcherConfig'); diff --git a/src/Storage/FileStorage.php b/src/Storage/FileStorage.php index ab376d6..9940b5c 100644 --- a/src/Storage/FileStorage.php +++ b/src/Storage/FileStorage.php @@ -15,6 +15,8 @@ * * @copyright Copyright (c) 2022+ CrowdSec * @license MIT License + * + * @psalm-api */ class FileStorage implements StorageInterface { @@ -38,6 +40,7 @@ public function __construct(string $rootDir = __DIR__, string $env = Constants:: $this->env = $env; } + #[\Override] public function retrieveMachineId(): ?string { $storageContent = $this->readFile($this->getBasePath() . self::MACHINE_ID_FILE); @@ -45,6 +48,7 @@ public function retrieveMachineId(): ?string return !empty($storageContent['machine_id']) ? $storageContent['machine_id'] : null; } + #[\Override] public function retrievePassword(): ?string { $storageContent = $this->readFile($this->getBasePath() . self::PASSWORD_FILE); @@ -52,6 +56,7 @@ public function retrievePassword(): ?string return !empty($storageContent['password']) ? $storageContent['password'] : null; } + #[\Override] public function retrieveScenarios(): ?array { $storageContent = $this->readFile($this->getBasePath() . self::SCENARIOS_FILE); @@ -59,6 +64,7 @@ public function retrieveScenarios(): ?array return !empty($storageContent['scenarios']) ? $storageContent['scenarios'] : null; } + #[\Override] public function retrieveToken(): ?string { $storageContent = $this->readFile($this->getBasePath() . self::TOKEN_FILE); @@ -66,6 +72,7 @@ public function retrieveToken(): ?string return !empty($storageContent['token']) ? $storageContent['token'] : null; } + #[\Override] public function storeMachineId(string $machineId): bool { try { @@ -78,6 +85,7 @@ public function storeMachineId(string $machineId): bool return true; } + #[\Override] public function storePassword(string $password): bool { try { @@ -90,6 +98,7 @@ public function storePassword(string $password): bool return true; } + #[\Override] public function storeScenarios(array $scenarios): bool { try { @@ -102,6 +111,7 @@ public function storeScenarios(array $scenarios): bool return true; } + #[\Override] public function storeToken(string $token): bool { try { diff --git a/src/Storage/StorageInterface.php b/src/Storage/StorageInterface.php index 91166e9..dd478bd 100644 --- a/src/Storage/StorageInterface.php +++ b/src/Storage/StorageInterface.php @@ -15,6 +15,8 @@ * * @copyright Copyright (c) 2022+ CrowdSec * @license MIT License + * + * @psalm-api */ interface StorageInterface { diff --git a/src/Watcher.php b/src/Watcher.php index 328271f..a1b799c 100644 --- a/src/Watcher.php +++ b/src/Watcher.php @@ -20,6 +20,8 @@ * * @see https://crowdsec.net CrowdSec Official Website * + * @psalm-api + * * @copyright Copyright (c) 2022+ CrowdSec * @license MIT License */ diff --git a/tests/PHPUnitUtil.php b/tests/PHPUnitUtil.php index 26a5c66..b523ab6 100644 --- a/tests/PHPUnitUtil.php +++ b/tests/PHPUnitUtil.php @@ -22,7 +22,10 @@ public static function callMethod($obj, $name, array $args) { $class = new \ReflectionClass($obj); $method = $class->getMethod($name); - $method->setAccessible(true); + // setAccessible() is only needed for PHP < 8.1 and deprecated in PHP 8.5 + if (PHP_VERSION_ID < 80100) { + $method->setAccessible(true); + } return $method->invokeArgs($obj, $args); } diff --git a/tools/coding-standards/composer.json b/tools/coding-standards/composer.json index c3bb2bd..60fc74d 100644 --- a/tools/coding-standards/composer.json +++ b/tools/coding-standards/composer.json @@ -4,8 +4,8 @@ "friendsofphp/php-cs-fixer": "^v3.8.0", "phpstan/phpstan": "^1.8.0", "phpmd/phpmd": "^2.12.0", - "squizlabs/php_codesniffer": "3.7.1", - "vimeo/psalm": "^4.24.0 || ^5.26", + "squizlabs/php_codesniffer": "3.7.1 || ^4.0.1", + "vimeo/psalm": "^4.24.0 || ^5.26 || ^6.13.1", "phpunit/phpunit": "^9.3", "phpunit/php-code-coverage": "^9.2.15", "mikey179/vfsstream": "^1.6.11", diff --git a/tools/coding-standards/psalm/psalm.xml b/tools/coding-standards/psalm/psalm.xml index 7d8eb84..2d4a1b5 100644 --- a/tools/coding-standards/psalm/psalm.xml +++ b/tools/coding-standards/psalm/psalm.xml @@ -2,6 +2,7 @@ +