Skip to content

Commit 91f4792

Browse files
Merge pull request #35 from Seraphim200001/main
Add PHPStan static analysis setup
2 parents fc3c231 + 915f530 commit 91f4792

9 files changed

Lines changed: 69 additions & 17 deletions

File tree

README.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
<h1>Openapi® client for PHP</h1>
77
<h4>The perfect starting point to integrate <a href="https://openapi.com/">Openapi®</a> within your PHP project</h4>
88

9-
[![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)
10-
[![Packagist Version](https://img.shields.io/packagist/v/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk)
11-
[![PHP Version](https://img.shields.io/packagist/php-v/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk)
12-
[![License](https://img.shields.io/github/license/openapi/openapi-php-sdk?v=2)](LICENSE)
13-
[![Downloads](https://img.shields.io/packagist/dt/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk)
14-
<br>
9+
[![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)
10+
[![Packagist Version](https://img.shields.io/packagist/v/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk)
11+
[![PHP Version](https://img.shields.io/packagist/php-v/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk)
12+
[![License](https://img.shields.io/github/license/openapi/openapi-php-sdk?v=2)](LICENSE)
13+
[![Downloads](https://img.shields.io/packagist/dt/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk)
14+
<br>
1515
[![Linux Foundation Member](https://img.shields.io/badge/Linux%20Foundation-Silver%20Member-003778?logo=linux-foundation&logoColor=white)](https://www.linuxfoundation.org/about/members)
16+
1617
</div>
1718

1819
## Overview
@@ -27,9 +28,10 @@ Before using the Openapi PHP Client, you will need an account at [Openapi](https
2728

2829
- **Agnostic Design**: No API-specific classes, works with any Openapi service
2930
- **Minimal Dependencies**: Only requires PHP 8.0+ and cURL
30-
- **OAuth Support**: Built-in OAuth client for token management
31+
- **OAuth Support**: Built-in OAuth client for token management
3132
- **HTTP Primitives**: GET, POST, PUT, DELETE, PATCH methods
3233
- **Clean Interface**: Similar to the Rust SDK design
34+
- **Static Analysis**: PHPStan level 6 configuration available via Composer
3335

3436
## What you can do
3537

@@ -81,7 +83,7 @@ $client = new Client($token);
8183
$params = ['denominazione' => 'Stellantis', 'provincia' => 'TO'];
8284
$response = $client->get('https://test.company.openapi.com/IT-advanced', $params);
8385

84-
// POST request
86+
// POST request
8587
$payload = ['limit' => 10, 'query' => ['country_code' => 'IT']];
8688
$response = $client->post('https://test.postontarget.com/fields/country', $payload);
8789

@@ -134,6 +136,17 @@ composer run test
134136
composer run test:unit
135137
```
136138

139+
## Static Analysis
140+
141+
This SDK includes PHPStan as a Composer development dependency to help keep the codebase type-safe and maintainable.
142+
143+
PHPStan is configured in `phpstan.neon` and currently runs at level 6.
144+
145+
Run static analysis with:
146+
147+
```bash
148+
composer run analyse
149+
```
137150

138151
## Contributing
139152

@@ -165,9 +178,9 @@ Meet our partners using Openapi or contributing to this SDK:
165178

166179
## Our Commitments
167180

168-
We believe in open source and we act on that belief. We became Silver Members
169-
of the Linux Foundation because we wanted to formally support the ecosystem
170-
we build on every day. Open standards, open collaboration, and open governance
181+
We believe in open source and we act on that belief. We became Silver Members
182+
of the Linux Foundation because we wanted to formally support the ecosystem
183+
we build on every day. Open standards, open collaboration, and open governance
171184
are part of how we work and how we think about software.
172185

173186
## License
@@ -179,4 +192,3 @@ The MIT License is a permissive open-source license that allows you to freely us
179192
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.
180193

181194
For more details, see the full license text at the [MIT License page](https://choosealicense.com/licenses/mit/).
182-

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
},
5252
"require-dev": {
5353
"symfony/dotenv": "^5.3",
54-
"phpunit/phpunit": "^9.5"
54+
"phpunit/phpunit": "^9.5",
55+
"phpstan/phpstan": "^2.1"
5556
},
5657
"autoload": {
5758
"psr-4": {
@@ -67,6 +68,7 @@
6768
}
6869
},
6970
"scripts": {
71+
"analyse": "vendor/bin/phpstan analyse -c phpstan.neon",
7072
"test": "phpunit tests/",
7173
"test:unit": "phpunit tests/ --testsuite=unit",
7274
"example:token": "php examples/token_generation.php",

phpstan.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
level: 6
3+
paths:
4+
- src
5+
- tests

src/Cache/ArrayCache.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44

55
class ArrayCache implements CacheInterface
66
{
7+
/**
8+
* @var array<string, mixed>
9+
*/
710
private array $cache = [];
11+
/**
12+
* @var array<string, int>
13+
*/
814
private array $expiry = [];
915

1016
public function get(string $key): mixed

src/Client.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ public function __construct(?string $token = null, HttpTransportInterface|PsrCli
2121
$this->transport = $transport ?? new CurlTransport($this->token);
2222
}
2323

24+
/**
25+
* Execute HTTP request
26+
*
27+
* @param string $method HTTP method (GET, POST, PUT, DELETE, PATCH)
28+
* @param string $url Target URL
29+
* @param mixed $payload Request body (for POST/PUT/PATCH)
30+
* @param array<string, scalar|null>|null $params Query parameters (for GET) or form data (for other methods)
31+
* @return string Response body
32+
*/
2433
public function request(
2534
string $method,
2635
string $url,
@@ -34,6 +43,11 @@ public function request(
3443
return $this->transport->request($method, $url, $payload, $params);
3544
}
3645

46+
/**
47+
* Execute GET request
48+
*
49+
* @param array<string, scalar|null>|null $params Query parameters
50+
*/
3751
public function get(string $url, ?array $params = null): string
3852
{
3953
return $this->request('GET', $url, null, $params);

src/Interfaces/HttpTransportInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
interface HttpTransportInterface
66
{
7+
/**
8+
* @param array<string, mixed>|string|null $params
9+
*/
710
public function request(
811
string $method,
912
string $url,
1013
mixed $payload = null,
11-
?array $params = null
14+
array|string|null $params = null
1215
): string;
1316
}

src/OauthClient.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public function getScopes(bool $limit = false): string
2828
return $this->request('GET', $url);
2929
}
3030

31+
/**
32+
* @param list<string> $scopes
33+
*/
3134
public function createToken(array $scopes, int $ttl = 3600): string
3235
{
3336
$body = [
@@ -56,6 +59,10 @@ public function getCounters(string $period, string $date): string
5659
return $this->request('GET', $this->url . '/counters/' . $period . '/' . $date);
5760
}
5861

62+
63+
/**
64+
* @param array<string, mixed>|null $body
65+
*/
5966
private function request(string $method, string $url, ?array $body = null): string
6067
{
6168
$ch = curl_init();

src/Transports/CurlTransport.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function request(
1414
string $method,
1515
string $url,
1616
mixed $payload = null,
17-
?array $params = null
17+
array|string|null $params = null
1818
): string {
1919
if ($params && $method === 'GET') {
2020
$url .= '?' . http_build_query($params);

tests/Transports/FakeTransport.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ final class FakeTransport implements HttpTransportInterface
99
public ?string $lastMethod = null;
1010
public ?string $lastUrl = null;
1111
public mixed $lastPayload = null;
12-
public ?array $lastParams = null;
12+
/**
13+
* @var array<string, mixed>|string|null
14+
*/
15+
public array|string|null $lastParams = null;
1316
public int $callCount = 0;
1417

1518
public function request(
1619
string $method,
1720
string $url,
1821
mixed $payload = null,
19-
?array $params = null
22+
array|string|null $params = null
2023
): string {
2124
$this->callCount++;
2225
$this->lastMethod = $method;

0 commit comments

Comments
 (0)