Skip to content

Commit 10ad39e

Browse files
authored
Merge pull request #197 from php-api-clients/reduce-client-properties
Reduce client properties
2 parents 1cbe153 + 674ffc2 commit 10ad39e

File tree

3 files changed

+27
-75
lines changed

3 files changed

+27
-75
lines changed

src/Generator/Client.php

Lines changed: 21 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,7 @@ public static function generate(Configuration $configuration, string $pathPrefix
5959
$factory = new BuilderFactory();
6060
$stmt = $factory->namespace(trim($configuration->namespace->source, '\\'));
6161

62-
$class = $factory->class('Client')->implement(new Node\Name('ClientInterface'))->makeFinal()->addStmt(
63-
$factory->property('authentication')->setType('\\' . AuthenticationInterface::class)->makeReadonly()->makePrivate(),
64-
)->addStmt(
65-
$factory->property('browser')->setType('\\' . Browser::class)->makeReadonly()->makePrivate(),
66-
)->addStmt(
67-
$factory->property('requestSchemaValidator')->setType('\League\OpenAPIValidation\Schema\SchemaValidator')->makeReadonly()->makePrivate(),
68-
)->addStmt(
69-
$factory->property('responseSchemaValidator')->setType('\League\OpenAPIValidation\Schema\SchemaValidator')->makeReadonly()->makePrivate(),
70-
);
62+
$class = $factory->class('Client')->implement(new Node\Name('ClientInterface'))->makeFinal();
7163

7264
if ($configuration->entryPoints->call) {
7365
$class->addStmt(
@@ -88,20 +80,10 @@ public static function generate(Configuration $configuration, string $pathPrefix
8880
}
8981

9082
$class->addStmt(
91-
$factory->property('hydrators')->setType('Hydrators')->makeReadonly()->makePrivate(),
92-
)->addStmt(
9383
$factory->property('routers')->setType('Routers')->makeReadonly()->makePrivate(),
9484
)->addStmt(
9585
$factory->method('__construct')->makePublic()->addParam(
9686
(new Param('authentication'))->setType('\\' . AuthenticationInterface::class),
97-
)->addStmt(
98-
new Node\Expr\Assign(
99-
new Node\Expr\PropertyFetch(
100-
new Node\Expr\Variable('this'),
101-
'authentication',
102-
),
103-
new Node\Expr\Variable('authentication'),
104-
),
10587
)->addParam(
10688
(new Param('browser'))->setType('\\' . Browser::class),
10789
)->addStmt((static function (Representation\Client $client): Node\Expr {
@@ -119,24 +101,21 @@ public static function generate(Configuration $configuration, string $pathPrefix
119101
);
120102
}
121103

122-
return new Node\Expr\Assign(new Node\Expr\PropertyFetch(
123-
new Node\Expr\Variable('this'),
124-
'browser',
125-
), new Node\Expr\MethodCall(
126-
$assignExpr,
127-
'withFollowRedirects',
128-
[
129-
new Arg(
130-
new Node\Expr\ConstFetch(new Node\Name('false')),
131-
),
132-
],
133-
));
104+
return new Node\Expr\Assign(
105+
new Node\Expr\Variable('browser'),
106+
new Node\Expr\MethodCall(
107+
$assignExpr,
108+
'withFollowRedirects',
109+
[
110+
new Arg(
111+
new Node\Expr\ConstFetch(new Node\Name('false')),
112+
),
113+
],
114+
),
115+
);
134116
})($client))->addStmt(
135117
new Node\Expr\Assign(
136-
new Node\Expr\PropertyFetch(
137-
new Node\Expr\Variable('this'),
138-
'requestSchemaValidator',
139-
),
118+
new Node\Expr\Variable('requestSchemaValidator'),
140119
new Node\Expr\New_(
141120
new Node\Name('\League\OpenAPIValidation\Schema\SchemaValidator'),
142121
[
@@ -149,10 +128,7 @@ public static function generate(Configuration $configuration, string $pathPrefix
149128
),
150129
)->addStmt(
151130
new Node\Expr\Assign(
152-
new Node\Expr\PropertyFetch(
153-
new Node\Expr\Variable('this'),
154-
'responseSchemaValidator',
155-
),
131+
new Node\Expr\Variable('responseSchemaValidator'),
156132
new Node\Expr\New_(
157133
new Node\Name('\League\OpenAPIValidation\Schema\SchemaValidator'),
158134
[
@@ -165,10 +141,7 @@ public static function generate(Configuration $configuration, string $pathPrefix
165141
),
166142
)->addStmt(
167143
new Node\Expr\Assign(
168-
new Node\Expr\PropertyFetch(
169-
new Node\Expr\Variable('this'),
170-
'hydrators',
171-
),
144+
new Node\Expr\Variable('hydrators'),
172145
new Node\Expr\New_(
173146
new Node\Name('Hydrators'),
174147
[],
@@ -185,50 +158,35 @@ public static function generate(Configuration $configuration, string $pathPrefix
185158
new Node\Name('Operations'),
186159
[
187160
new Arg(
188-
new Node\Expr\PropertyFetch(
189-
new Node\Expr\Variable('this'),
190-
'browser',
191-
),
161+
new Node\Expr\Variable('browser'),
192162
false,
193163
false,
194164
[],
195165
new Node\Identifier('browser'),
196166
),
197167
new Arg(
198-
new Node\Expr\PropertyFetch(
199-
new Node\Expr\Variable('this'),
200-
'authentication',
201-
),
168+
new Node\Expr\Variable('authentication'),
202169
false,
203170
false,
204171
[],
205172
new Node\Identifier('authentication'),
206173
),
207174
new Arg(
208-
new Node\Expr\PropertyFetch(
209-
new Node\Expr\Variable('this'),
210-
'requestSchemaValidator',
211-
),
175+
new Node\Expr\Variable('requestSchemaValidator'),
212176
false,
213177
false,
214178
[],
215179
new Node\Identifier('requestSchemaValidator'),
216180
),
217181
new Arg(
218-
new Node\Expr\PropertyFetch(
219-
new Node\Expr\Variable('this'),
220-
'responseSchemaValidator',
221-
),
182+
new Node\Expr\Variable('responseSchemaValidator'),
222183
false,
223184
false,
224185
[],
225186
new Node\Identifier('responseSchemaValidator'),
226187
),
227188
new Arg(
228-
new Node\Expr\PropertyFetch(
229-
new Node\Expr\Variable('this'),
230-
'hydrators',
231-
),
189+
new Node\Expr\Variable('hydrators'),
232190
false,
233191
false,
234192
[],

tests/app/etc/openapi-client-generator.state

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@
204204
},
205205
{
206206
"name": ".\/tests\/app\/src\/\/Client.php",
207-
"hash": "6ff122e2242b2a6e4485e102494cb16e"
207+
"hash": "ee4a2025a9354a33e9bb90bf8bc01f4b"
208208
},
209209
{
210210
"name": ".\/tests\/app\/src\/\/Router\/Get\/Pets.php",

tests/app/src/Client.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,16 @@
1515
use ApiClients\Contracts;
1616
final class Client implements ClientInterface
1717
{
18-
private readonly \ApiClients\Contracts\HTTP\Headers\AuthenticationInterface $authentication;
19-
private readonly \React\Http\Browser $browser;
20-
private readonly \League\OpenAPIValidation\Schema\SchemaValidator $requestSchemaValidator;
21-
private readonly \League\OpenAPIValidation\Schema\SchemaValidator $responseSchemaValidator;
2218
private array $router = array();
2319
private readonly OperationsInterface $operations;
24-
private readonly Hydrators $hydrators;
2520
private readonly Routers $routers;
2621
public function __construct(\ApiClients\Contracts\HTTP\Headers\AuthenticationInterface $authentication, \React\Http\Browser $browser)
2722
{
28-
$this->authentication = $authentication;
29-
$this->browser = $browser->withBase('http://petstore.swagger.io/v1')->withFollowRedirects(false);
30-
$this->requestSchemaValidator = new \League\OpenAPIValidation\Schema\SchemaValidator(\League\OpenAPIValidation\Schema\SchemaValidator::VALIDATE_AS_REQUEST);
31-
$this->responseSchemaValidator = new \League\OpenAPIValidation\Schema\SchemaValidator(\League\OpenAPIValidation\Schema\SchemaValidator::VALIDATE_AS_RESPONSE);
32-
$this->hydrators = new Hydrators();
33-
$this->operations = new Operations(browser: $this->browser, authentication: $this->authentication, requestSchemaValidator: $this->requestSchemaValidator, responseSchemaValidator: $this->responseSchemaValidator, hydrators: $this->hydrators);
23+
$browser = $browser->withBase('http://petstore.swagger.io/v1')->withFollowRedirects(false);
24+
$requestSchemaValidator = new \League\OpenAPIValidation\Schema\SchemaValidator(\League\OpenAPIValidation\Schema\SchemaValidator::VALIDATE_AS_REQUEST);
25+
$responseSchemaValidator = new \League\OpenAPIValidation\Schema\SchemaValidator(\League\OpenAPIValidation\Schema\SchemaValidator::VALIDATE_AS_RESPONSE);
26+
$hydrators = new Hydrators();
27+
$this->operations = new Operations(browser: $browser, authentication: $authentication, requestSchemaValidator: $requestSchemaValidator, responseSchemaValidator: $responseSchemaValidator, hydrators: $hydrators);
3428
$this->routers = new Routers(browser: $this->browser, authentication: $this->authentication, requestSchemaValidator: $this->requestSchemaValidator, responseSchemaValidator: $this->responseSchemaValidator, hydrators: $this->hydrators);
3529
}
3630
// phpcs:disable

0 commit comments

Comments
 (0)