Skip to content

Commit 61ca194

Browse files
committed
Update to ensure still works on php7.4 and simplify use of psr7
1 parent f588b9c commit 61ca194

File tree

10 files changed

+236
-999
lines changed

10 files changed

+236
-999
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
}
99
],
1010
"require": {
11-
"php": ">=7.2 <8.3",
11+
"php": ">=7.4 <8.3",
1212
"nette/php-generator": "3.6.9",
1313
"symfony/yaml": "5.4.35",
1414
"symfony/serializer": "5.4.36",
15-
"psr/http-message": "^1.0"
15+
"psr/http-message": "^1.1 || ^2.0"
1616
},
1717
"require-dev": {
1818
"roave/security-advisories": "dev-latest"

composer.lock

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

example.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
"dir",
1111
"more-specificity",
1212
];
13-
$options = getopt("", array_map(function($option){
14-
return "{$option}:";
15-
}, $opt_fields));
13+
$options = getopt("", array_map(static fn($option) => "{$option}:", $opt_fields));
1614

1715
$opt_errors = [];
1816
foreach ($opt_fields as $opt_field){

src/ClassGenerator.php

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,15 @@ abstract class ClassGenerator {
2424
public const NAMESPACE_MODEL = 'Models';
2525
public const NAMESPACE_REQUEST = 'Requests';
2626

27-
/**
28-
* @var string
29-
*/
30-
private $namespace_name;
27+
private string $namespace_name;
3128

3229
/**
3330
* @var ClassType[]
3431
*/
35-
public $classes = [];
32+
public array $classes = [];
3633

3734
/**
3835
* Creates a generator for a specific namespace
39-
*
40-
* @param string $namespace_name
4136
*/
4237
public function __construct(string $namespace_name){
4338
$this->namespace_name = $this->stringNotEndWith($namespace_name, '\\');
@@ -55,11 +50,6 @@ abstract public function saveClasses(string $dir);
5550

5651
abstract public function generate(string $file_path);
5752

58-
/**
59-
* @param string $yaml_path
60-
* @param string $dir
61-
* @return int
62-
*/
6353
public function runFull(string $yaml_path, string $dir): int{
6454
$this->generate($yaml_path);
6555

@@ -69,13 +59,7 @@ public function runFull(string $yaml_path, string $dir): int{
6959
return count($this->classes);
7060
}
7161

72-
/**
73-
* @param string $yaml_path
74-
* @param string $dir
75-
*
76-
* @return int
77-
*/
78-
public function runFullWithMoreSpecificity(string $yaml_path, string $dir){
62+
public function runFullWithMoreSpecificity(string $yaml_path, string $dir): int{
7963
$this->generate($yaml_path, true);
8064

8165
$this->saveClasses($dir);
@@ -87,9 +71,6 @@ public function runFullWithMoreSpecificity(string $yaml_path, string $dir){
8771
/**
8872
* Saves generated classes down as PHP files
8973
*
90-
* @param string $dir
91-
* @param string $namespace_name
92-
* @param string $use
9374
* @throws RuntimeException
9475
* @throws FileNotFoundException
9576
*/
@@ -110,10 +91,6 @@ protected function saveClassesInternal(string $dir, string $namespace_name, stri
11091
abstract public function dumpParentClass(string $dir);
11192

11293
/**
113-
* @param string $dir
114-
* @param string $file
115-
* @param string $namespace
116-
* @param string $namespace_use
11794
* @throws FileNotFoundException
11895
*/
11996
protected function dumpParentInternal(string $dir, string $file, string $namespace, string $namespace_use = ''): void{
@@ -128,21 +105,10 @@ protected function dumpParentInternal(string $dir, string $file, string $namespa
128105
file_put_contents("$dir/$file_name", $content);
129106
}
130107

131-
/**
132-
* Utility function
133-
*
134-
* @param string $string
135-
* @param string $char
136-
* @return string
137-
*/
138108
protected function stringNotEndWith(string $string, string $char): string{
139109
return $string[strlen($string)-1]===$char ? substr($string, 0, -1) : $string;
140110
}
141111

142-
/**
143-
* @param string $string
144-
* @return string
145-
*/
146112
protected function unPlural(string $string): string{
147113
if (substr($string, -3)==='ies'){
148114
return substr($string, 0, -3).'y';
@@ -156,9 +122,6 @@ protected function unPlural(string $string): string{
156122

157123
/**
158124
* Changes a Swagger definition into a type
159-
*
160-
* @param array $property
161-
* @return string
162125
*/
163126
protected function typeFromRef(array $property): string{
164127
if (!isset($property['$ref'])){
@@ -168,20 +131,13 @@ protected function typeFromRef(array $property): string{
168131
return str_replace('#/definitions/', '', $property['$ref']);
169132
}
170133

171-
/**
172-
* @param string $dir
173-
* @param string $namespace
174-
* @return string
175-
*/
176134
protected function dirNamespace(string $dir, string $namespace): string{
177135
$dir = $this->stringNotEndWith($dir, '/');
178136

179137
return "$dir/$namespace";
180138
}
181139

182140
/**
183-
* @param string $dir
184-
* @return string
185141
* @throws FileNotFoundException
186142
*/
187143
private function checkDir(string $dir): string{
@@ -195,10 +151,6 @@ private function checkDir(string $dir): string{
195151
return $dir;
196152
}
197153

198-
/**
199-
* @param string $type
200-
* @return bool
201-
*/
202154
protected function notScalarType(string $type): bool{
203155
return !in_array($type, ['integer', 'string', 'boolean', 'number', 'null']);
204156
}

src/GenerateAll.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@
33
namespace SwaggerGen;
44

55
class GenerateAll {
6-
public $saved_models = 0;
7-
public $saved_requests = 0;
6+
public int $saved_models = 0;
7+
public int $saved_requests = 0;
88

99
/**
1010
* Generates all required files into the specified directory
11-
*
12-
* @param string $namespace
13-
* @param string $yaml_path
14-
* @param string $dir
15-
* @param bool $more_specificity
1611
*/
1712
public function __construct(string $namespace, string $yaml_path, string $dir, bool $more_specificity = false){
1813
$generate_models = new GenerateModels($namespace);

src/GenerateModels.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
namespace SwaggerGen;
33

44
use Nette\PhpGenerator\ClassType;
5-
use Nette\PhpGenerator\Method;
65
use Nette\PhpGenerator\PhpNamespace;
76
use Nette\PhpGenerator\Property;
87
use RuntimeException;
@@ -16,8 +15,6 @@ class GenerateModels extends ClassGenerator {
1615

1716
/**
1817
* Generates classes in the "classes" field
19-
*
20-
* @param string $file_path
2118
* @throws RuntimeException
2219
*/
2320
public function generate(string $file_path) :void{
@@ -55,9 +52,6 @@ public function generate(string $file_path) :void{
5552
}
5653

5754
/**
58-
* @param array $properties
59-
* @param ClassType $class
60-
* @param array $required
6155
* @throws RuntimeException
6256
*/
6357
private function classProperties(array $properties, ClassType $class, ?array $required): void{
@@ -83,9 +77,6 @@ private function classProperties(array $properties, ClassType $class, ?array $re
8377
$typehint = $type;
8478
}
8579

86-
/**
87-
* @var Property $property
88-
*/
8980
$property = $class->addProperty($property_name)->setVisibility('protected');
9081
$property->addComment($property_details['description'] ?? "\n");
9182

@@ -136,9 +127,6 @@ private function classProperties(array $properties, ClassType $class, ?array $re
136127
$class->addMethod('get'.$capital_case)
137128
->setBody("return \$this->$property_name;")
138129
->addComment("@return $comment_type");
139-
/**
140-
* @var Method $setter
141-
*/
142130
$setter = $class->addMethod('set'.$capital_case)
143131
->setBody("\$this->$property_name = \$$property_name;\n\nreturn \$this;")
144132
->addComment("@param $comment_type \$$property_name")
@@ -153,9 +141,6 @@ private function classProperties(array $properties, ClassType $class, ?array $re
153141
if ($sub_type){
154142
$property_name_singular = $this->unPlural($property_name);
155143
$capital_case_singular = $this->unPlural($capital_case);
156-
/**
157-
* @var Method $add_to
158-
*/
159144
$add_to = $class->addMethod('add'.$capital_case_singular)
160145
->setBody("\$this->{$property_name}[] = \$$property_name_singular;\n\nreturn \$this;");
161146

@@ -174,25 +159,17 @@ private function classProperties(array $properties, ClassType $class, ?array $re
174159
}
175160
}
176161

177-
/**
178-
* @param string $dir
179-
*/
180162
public function saveClasses(string $dir) :void{
181163
$dir = $this->dirNamespace($dir, self::NAMESPACE_MODEL);
182164
$this->saveClassesInternal($dir, $this->namespaceModel());
183165
}
184166

185-
/**
186-
* @param string $dir
187-
*/
188167
public function dumpParentClass(string $dir) :void{
189168
$dir = $this->dirNamespace($dir, self::NAMESPACE_MODEL);
190169
$this->dumpParentInternal($dir, __DIR__.'/SwaggerModel.php', $this->namespaceModel());
191170
}
192171

193172
/**
194-
* @param Property $property
195-
* @param string $type
196173
* @throws RuntimeException
197174
*/
198175
private function blankValue(Property $property, string $type): void{

src/GenerateRequests.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ protected function handleResponse(array $method_details, ClassType $class): void
304304
$type = "''";
305305
}
306306

307-
$response_models[] = "'$code_string' => '$type'";
307+
$response_models[] = "'$code_string' => $type";
308308

309309
if ((int)$code_string>0 && (int)$code_string<400){
310310
$has_2xx = true;

src/SwaggerClient.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
<?php
22
namespace SwaggerGen;
33

4+
use Psr\Http\Message\RequestInterface;
45
use SwaggerGen\SwaggerModel;
56

67
interface SwaggerClient {
78

89
/**
9-
* @param SwaggerRequest $request
10-
* @param string[] $response_models
11-
* @return SwaggerModel|SwaggerModel[]
10+
* @template T of SwaggerModel
11+
*
12+
* @param array<array-key, class-string<T>> $response_models
13+
* @return T|list<T>
1214
*/
1315
public function make(SwaggerRequest $request, array $response_models);
16+
17+
public function messageFromRequest(SwaggerRequest $swagger):RequestInterface;
1418
}

src/SwaggerModel.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
class SwaggerModel implements JsonSerializable {
88
public const DATE_FORMAT = 'Y-m-d H:i:s';
99

10-
private $_is_error = false;
10+
private bool $_is_error = false;
1111

12-
/**
13-
* @return array
14-
*/
1512
public function toArray(): array{
1613
$this->preOutput();
1714

@@ -24,9 +21,7 @@ public function toArray(): array{
2421
$data[$key] = $val;
2522
}
2623

27-
$data = $this->toArrayData($data);
28-
29-
return $data;
24+
return $this->toArrayData($data);
3025
}
3126

3227
/**
@@ -38,15 +33,11 @@ protected function preOutput(): void{
3833
/**
3934
* @return array
4035
*/
36+
#[\ReturnTypeWillChange]
4137
public function jsonSerialize(){
4238
return $this->toArray();
4339
}
4440

45-
/**
46-
* @param array $data
47-
*
48-
* @return array
49-
*/
5041
private function toArrayData(array $data): array{
5142
foreach ($data as &$val){
5243

@@ -70,9 +61,6 @@ private function toArrayData(array $data): array{
7061
return $data;
7162
}
7263

73-
/**
74-
* @return bool
75-
*/
7664
public function isError(): bool{
7765
return $this->_is_error;
7866
}

0 commit comments

Comments
 (0)