From 1b48554e4c2f015710d10b474ffbd6e247105317 Mon Sep 17 00:00:00 2001 From: Mario Ugurcu Date: Mon, 18 May 2026 13:01:19 +0200 Subject: [PATCH 1/2] Add PHPStan static analysis setup Add PHPStan as a Composer dev dependency, configure phpstan.neon at level 6, add the Composer analyse script, and fix reported iterable type issues across the SDK and tests. Integration tests requiring valid tokens remain skipped. --- composer.json | 4 +++- phpstan.neon | 5 +++++ src/Cache/ArrayCache.php | 6 ++++++ src/Client.php | 14 ++++++++++++++ src/Interfaces/HttpTransportInterface.php | 5 ++++- src/OauthClient.php | 7 +++++++ src/Transports/CurlTransport.php | 2 +- tests/Transports/FakeTransport.php | 7 +++++-- 8 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 phpstan.neon diff --git a/composer.json b/composer.json index 4841037..a10fc35 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,8 @@ }, "require-dev": { "symfony/dotenv": "^5.3", - "phpunit/phpunit": "^9.5" + "phpunit/phpunit": "^9.5", + "phpstan/phpstan": "^2.1" }, "autoload": { "psr-4": { @@ -67,6 +68,7 @@ } }, "scripts": { + "analyse": "vendor/bin/phpstan analyse -c phpstan.neon", "test": "phpunit tests/", "test:unit": "phpunit tests/ --testsuite=unit", "example:token": "php examples/token_generation.php", diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..6ea7d09 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,5 @@ +parameters: + level: 6 + paths: + - src + - tests \ No newline at end of file diff --git a/src/Cache/ArrayCache.php b/src/Cache/ArrayCache.php index 8b9f6a2..2733058 100644 --- a/src/Cache/ArrayCache.php +++ b/src/Cache/ArrayCache.php @@ -4,7 +4,13 @@ class ArrayCache implements CacheInterface { + /** + * @var array + */ private array $cache = []; + /** + * @var array + */ private array $expiry = []; public function get(string $key): mixed diff --git a/src/Client.php b/src/Client.php index f92ece3..6c77b44 100644 --- a/src/Client.php +++ b/src/Client.php @@ -21,6 +21,15 @@ public function __construct(?string $token = null, HttpTransportInterface|PsrCli $this->transport = $transport ?? new CurlTransport($this->token); } + /** + * Execute HTTP request + * + * @param string $method HTTP method (GET, POST, PUT, DELETE, PATCH) + * @param string $url Target URL + * @param mixed $payload Request body (for POST/PUT/PATCH) + * @param array|null $params Query parameters (for GET) or form data (for other methods) + * @return string Response body + */ public function request( string $method, string $url, @@ -34,6 +43,11 @@ public function request( return $this->transport->request($method, $url, $payload, $params); } + /** + * Execute GET request + * + * @param array|null $params Query parameters + */ public function get(string $url, ?array $params = null): string { return $this->request('GET', $url, null, $params); diff --git a/src/Interfaces/HttpTransportInterface.php b/src/Interfaces/HttpTransportInterface.php index 8dd7ca5..436ba4b 100644 --- a/src/Interfaces/HttpTransportInterface.php +++ b/src/Interfaces/HttpTransportInterface.php @@ -4,10 +4,13 @@ interface HttpTransportInterface { + /** + * @param array|string|null $params + */ public function request( string $method, string $url, mixed $payload = null, - ?array $params = null + array|string|null $params = null ): string; } diff --git a/src/OauthClient.php b/src/OauthClient.php index 362d7a5..2bb2bb3 100644 --- a/src/OauthClient.php +++ b/src/OauthClient.php @@ -28,6 +28,9 @@ public function getScopes(bool $limit = false): string return $this->request('GET', $url); } + /** + * @param list $scopes + */ public function createToken(array $scopes, int $ttl = 3600): string { $body = [ @@ -56,6 +59,10 @@ public function getCounters(string $period, string $date): string return $this->request('GET', $this->url . '/counters/' . $period . '/' . $date); } + + /** + * @param array|null $body + */ private function request(string $method, string $url, ?array $body = null): string { $ch = curl_init(); diff --git a/src/Transports/CurlTransport.php b/src/Transports/CurlTransport.php index 5413a7f..8a662be 100644 --- a/src/Transports/CurlTransport.php +++ b/src/Transports/CurlTransport.php @@ -14,7 +14,7 @@ public function request( string $method, string $url, mixed $payload = null, - ?array $params = null + array|string|null $params = null ): string { if ($params && $method === 'GET') { $url .= '?' . http_build_query($params); diff --git a/tests/Transports/FakeTransport.php b/tests/Transports/FakeTransport.php index 81fbb01..563ee4c 100644 --- a/tests/Transports/FakeTransport.php +++ b/tests/Transports/FakeTransport.php @@ -9,14 +9,17 @@ final class FakeTransport implements HttpTransportInterface public ?string $lastMethod = null; public ?string $lastUrl = null; public mixed $lastPayload = null; - public ?array $lastParams = null; + /** + * @var array|string|null + */ + public array|string|null $lastParams = null; public int $callCount = 0; public function request( string $method, string $url, mixed $payload = null, - ?array $params = null + array|string|null $params = null ): string { $this->callCount++; $this->lastMethod = $method; From 915f530bdd80d104ae170bf4bf2324d51153c8de Mon Sep 17 00:00:00 2001 From: Mario Ugurcu Date: Mon, 18 May 2026 13:06:53 +0200 Subject: [PATCH 2/2] Update Readme.md for Analysis Feature --- README.md | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 93675c4..97dba39 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,14 @@

OpenapiĀ® client for PHP

The perfect starting point to integrate OpenapiĀ® within your PHP project

- [![Build Status](https://github.com/openapi/openapi-php-sdk/actions/workflows/php.yml/badge.svg)](https://github.com/openapi/openapi-php-sdk/actions/workflows/php.yml) - [![Packagist Version](https://img.shields.io/packagist/v/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk) - [![PHP Version](https://img.shields.io/packagist/php-v/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk) - [![License](https://img.shields.io/github/license/openapi/openapi-php-sdk?v=2)](LICENSE) - [![Downloads](https://img.shields.io/packagist/dt/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk) -
+[![Build Status](https://github.com/openapi/openapi-php-sdk/actions/workflows/php.yml/badge.svg)](https://github.com/openapi/openapi-php-sdk/actions/workflows/php.yml) +[![Packagist Version](https://img.shields.io/packagist/v/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk) +[![PHP Version](https://img.shields.io/packagist/php-v/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk) +[![License](https://img.shields.io/github/license/openapi/openapi-php-sdk?v=2)](LICENSE) +[![Downloads](https://img.shields.io/packagist/dt/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk) +
[![Linux Foundation Member](https://img.shields.io/badge/Linux%20Foundation-Silver%20Member-003778?logo=linux-foundation&logoColor=white)](https://www.linuxfoundation.org/about/members) + ## Overview @@ -27,9 +28,10 @@ Before using the Openapi PHP Client, you will need an account at [Openapi](https - **Agnostic Design**: No API-specific classes, works with any Openapi service - **Minimal Dependencies**: Only requires PHP 8.0+ and cURL -- **OAuth Support**: Built-in OAuth client for token management +- **OAuth Support**: Built-in OAuth client for token management - **HTTP Primitives**: GET, POST, PUT, DELETE, PATCH methods - **Clean Interface**: Similar to the Rust SDK design +- **Static Analysis**: PHPStan level 6 configuration available via Composer ## What you can do @@ -81,7 +83,7 @@ $client = new Client($token); $params = ['denominazione' => 'Stellantis', 'provincia' => 'TO']; $response = $client->get('https://test.company.openapi.com/IT-advanced', $params); -// POST request +// POST request $payload = ['limit' => 10, 'query' => ['country_code' => 'IT']]; $response = $client->post('https://test.postontarget.com/fields/country', $payload); @@ -134,6 +136,17 @@ composer run test composer run test:unit ``` +## Static Analysis + +This SDK includes PHPStan as a Composer development dependency to help keep the codebase type-safe and maintainable. + +PHPStan is configured in `phpstan.neon` and currently runs at level 6. + +Run static analysis with: + +```bash +composer run analyse +``` ## Contributing @@ -165,9 +178,9 @@ Meet our partners using Openapi or contributing to this SDK: ## Our Commitments -We believe in open source and we act on that belief. We became Silver Members -of the Linux Foundation because we wanted to formally support the ecosystem -we build on every day. Open standards, open collaboration, and open governance +We believe in open source and we act on that belief. We became Silver Members +of the Linux Foundation because we wanted to formally support the ecosystem +we build on every day. Open standards, open collaboration, and open governance are part of how we work and how we think about software. ## License @@ -179,4 +192,3 @@ The MIT License is a permissive open-source license that allows you to freely us In short, you are free to use this SDK in your personal, academic, or commercial projects, with minimal restrictions. The project is provided "as-is", without any warranty of any kind, either expressed or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non-infringement. For more details, see the full license text at the [MIT License page](https://choosealicense.com/licenses/mit/). -