Skip to content

Commit f915185

Browse files
committed
Add types, support PHP 8+
Add GitHub actions for test
1 parent 8a570b8 commit f915185

30 files changed

Lines changed: 471 additions & 516 deletions

.github/workflows/test.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Test
2+
on: [ push ]
3+
jobs:
4+
test:
5+
runs-on: ubuntu-20.04
6+
strategy:
7+
matrix:
8+
php: [ '8.0', '8.1' ]
9+
steps:
10+
- uses: actions/checkout@v2
11+
- run: mkdir -p build/logs
12+
- name: Test PHP
13+
uses: shivammathur/setup-php@v2
14+
with:
15+
php-version: ${{matrix.php}}
16+
- run: composer install
17+
- run: php vendor/bin/phpunit

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
}
1414
],
1515
"require": {
16-
"php": "^7.1 || ^8.0",
16+
"php": "^8.0",
1717
"ext-json": "*"
1818
},
1919
"autoload": {

composer.lock

Lines changed: 139 additions & 132 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Components/Contact.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
namespace nickdnk\OpenAPI\Components;
55

6+
use JetBrains\PhpStorm\Pure;
67
use JsonSerializable;
78

89
class Contact implements JsonSerializable
910
{
1011

11-
private $name, $url, $email;
12+
private string $url, $name, $email;
1213

14+
#[Pure]
1315
public function __construct(string $name, string $url, string $email)
1416
{
1517

src/Components/Endpoint.php

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace nickdnk\OpenAPI\Components;
55

66
use InvalidArgumentException;
7+
use JetBrains\PhpStorm\Pure;
78
use JsonSerializable;
89
use nickdnk\OpenAPI\Components\Security\RequiredSecurityScheme;
910
use nickdnk\OpenAPI\OpenAPIDocument;
@@ -21,90 +22,87 @@ class Endpoint implements JsonSerializable
2122
const CONTENT_TYPE_APPLICATION_JSON = 'application/json';
2223
const CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED = 'application/x-www-form-urlencoded';
2324

24-
private $tag, $summary, $description, $responses;
2525
/**
26-
* @var array|null
26+
* @var Response[]
2727
*/
28-
private $requestBodies;
29-
/**
30-
* @var array|null
31-
*/
32-
private $parameters;
33-
/**
34-
* @var string
35-
*/
36-
private $httpMethod;
28+
private array $responses;
29+
30+
/** @var Base[]|null $requestBodies */
31+
private ?array $requestBodies;
32+
3733
/**
38-
* @var bool
34+
* @var Parameter[]|null
3935
*/
40-
private $deprecated;
36+
private ?array $parameters;
4137

4238
/**
4339
* @var string[]|null
4440
*/
45-
private $requiredSecuritySchemes;
41+
private ?array $requiredSecuritySchemes;
4642

47-
/**
48-
* Endpoint constructor.
49-
*
50-
* @param string $httpMethod
51-
* @param string $summary
52-
* @param string $description
53-
* @param Response[] $responses
54-
* @param Parameter[]|null $parameters
55-
*/
56-
private function __construct(string $httpMethod, string $summary, string $description, array $responses,
57-
?array $parameters = null
43+
private ?string $tag;
44+
private string $httpMethod, $summary, $description;
45+
private bool $deprecated;
46+
47+
#[Pure]
48+
private function __construct(string $httpMethod, string $summary, string $description
5849
)
5950
{
6051

61-
$this->tag = null; // set by section on output.
52+
$this->requiredSecuritySchemes = null;
53+
$this->requestBodies = null;
54+
$this->tag = null;
6255
$this->summary = $summary;
6356
$this->description = $description;
64-
$this->responses = $responses;
65-
$this->parameters = $parameters;
57+
$this->responses = [];
58+
$this->parameters = null;
6659
$this->httpMethod = $httpMethod;
6760
$this->deprecated = false;
6861

6962
}
7063

64+
#[Pure]
7165
final public static function get(string $andSummary, string $description): self
7266
{
7367

7468
return new self(
75-
self::GET, $andSummary, $description, []
69+
self::GET, $andSummary, $description
7670
);
7771
}
7872

73+
#[Pure]
7974
final public static function post(string $andSummary, string $description): self
8075
{
8176

8277
return new self(
83-
self::POST, $andSummary, $description, []
78+
self::POST, $andSummary, $description
8479
);
8580
}
8681

82+
#[Pure]
8783
final public static function put(string $andSummary, string $description): self
8884
{
8985

9086
return new self(
91-
self::PUT, $andSummary, $description, []
87+
self::PUT, $andSummary, $description
9288
);
9389
}
9490

91+
#[Pure]
9592
final public static function delete(string $andSummary, string $description): self
9693
{
9794

9895
return new self(
99-
self::DELETE, $andSummary, $description, []
96+
self::DELETE, $andSummary, $description
10097
);
10198
}
10299

100+
#[Pure]
103101
final public static function patch(string $andSummary, string $description): self
104102
{
105103

106104
return new self(
107-
self::PATCH, $andSummary, $description, []
105+
self::PATCH, $andSummary, $description
108106
);
109107
}
110108

@@ -145,7 +143,7 @@ public function withParameters(Parameter ...$parameter): self
145143
*
146144
* @return Endpoint
147145
*/
148-
public function withRequiredSecuritySchemes($securityScheme): self
146+
public function withRequiredSecuritySchemes(array|RequiredSecurityScheme $securityScheme): self
149147
{
150148

151149
if ($this->requiredSecuritySchemes === null) {
@@ -168,7 +166,7 @@ public function withRequestBodyFromDocument(Base $class, string $contentType = s
168166
{
169167

170168
if ($this->httpMethod === Endpoint::GET) {
171-
throw new InvalidArgumentException('Passed request body entity ' . (is_string($class) ? ($class . ' ') : '') . 'to GET endpoint.');
169+
throw new InvalidArgumentException('Passed request body entity ' . $class::class . ' to GET endpoint.');
172170
}
173171

174172
if ($this->requestBodies === null) {
@@ -198,6 +196,7 @@ public function withRequestBodyFromEntity(string $class, string $contentType = s
198196

199197
}
200198

199+
#[Pure]
201200
final public function getHttpMethod(): string
202201
{
203202

@@ -207,6 +206,7 @@ final public function getHttpMethod(): string
207206
/**
208207
* @return Response[]
209208
*/
209+
#[Pure]
210210
final public function getResponses(): array
211211
{
212212

src/Components/ExternalDocs.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33

44
namespace nickdnk\OpenAPI\Components;
55

6+
use JetBrains\PhpStorm\Pure;
67
use JsonSerializable;
78

89
class ExternalDocs implements JsonSerializable
910
{
1011

11-
private $url, $description;
12+
private ?string $description;
13+
private string $url;
1214

15+
#[Pure]
1316
public function __construct(string $url, ?string $description)
1417
{
1518

src/Components/Header.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace nickdnk\OpenAPI\Components;
55

6+
use JetBrains\PhpStorm\Pure;
67
use JsonSerializable;
78
use nickdnk\OpenAPI\Types\Base;
89

@@ -11,8 +12,10 @@ class Header implements JsonSerializable
1112

1213
const LOCATION = 'Location';
1314

14-
private $header, $value, $description;
15+
private string $description, $header;
16+
private Base $value;
1517

18+
#[Pure]
1619
public function __construct(string $header, string $description, Base $value)
1720
{
1821

@@ -21,6 +24,7 @@ public function __construct(string $header, string $description, Base $value)
2124
$this->description = $description;
2225
}
2326

27+
#[Pure]
2428
public function getHeader(): string
2529
{
2630

src/Components/Info.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33

44
namespace nickdnk\OpenAPI\Components;
55

6+
use JetBrains\PhpStorm\Pure;
67
use JsonSerializable;
78

89
class Info implements JsonSerializable
910
{
1011

11-
private $title, $description, $termsOfService, $contact, $license, $version, $logo;
12+
private ?string $logo, $description, $termsOfService;
13+
private string $version, $title;
14+
private ?License $license;
15+
private ?Contact $contact;
1216

13-
public function __construct(string $title, string $version, ?string $description = null,
17+
#[Pure]
18+
public function __construct(string $title, string $version, ?string $description = null,
1419
?string $termsOfService = null, ?Contact $contact = null, ?License $license = null, ?string $logo = null
1520
)
1621
{
@@ -24,42 +29,49 @@ public function __construct(string $title, string $version, ?string $description
2429
$this->logo = $logo;
2530
}
2631

32+
#[Pure]
2733
final public function getTitle(): string
2834
{
2935

3036
return $this->title;
3137
}
3238

39+
#[Pure]
3340
final public function getDescription(): ?string
3441
{
3542

3643
return $this->description;
3744
}
3845

46+
#[Pure]
3947
final public function getTermsOfService(): ?string
4048
{
4149

4250
return $this->termsOfService;
4351
}
4452

53+
#[Pure]
4554
final public function getContact(): ?Contact
4655
{
4756

4857
return $this->contact;
4958
}
5059

60+
#[Pure]
5161
final public function getLicense(): ?License
5262
{
5363

5464
return $this->license;
5565
}
5666

67+
#[Pure]
5768
final public function getVersion(): string
5869
{
5970

6071
return $this->version;
6172
}
6273

74+
#[Pure]
6375
final public function getLogo(): ?string
6476
{
6577

src/Components/License.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33

44
namespace nickdnk\OpenAPI\Components;
55

6+
use JetBrains\PhpStorm\Pure;
67
use JsonSerializable;
78

89
class License implements JsonSerializable
910
{
1011

11-
private $name, $url;
12+
private ?string $url;
13+
private string $name;
1214

15+
#[Pure]
1316
public function __construct(string $name, ?string $url)
1417
{
1518

0 commit comments

Comments
 (0)