Skip to content

Commit 0bb32d6

Browse files
authored
Merge pull request #89 from php-api-clients/use-useuse-instead-of-using-FQCN
Use UseUse instead of using FQCN
2 parents 9f9b29b + 4681136 commit 0bb32d6

File tree

8 files changed

+73
-41
lines changed

8 files changed

+73
-41
lines changed

src/Generator.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,39 @@ public function generate(string $namespace, string $destinationPath)
3636

3737
foreach ($this->all($namespace, $destinationPath . DIRECTORY_SEPARATOR) as $file) {
3838
$fileName = $destinationPath . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, substr($file->fqcn, strlen($namespace))) . '.php';
39-
$fileContents = ($file->contents instanceof Node ? $codePrinter->prettyPrintFile([
39+
if ($file->contents instanceof Node\Stmt\Namespace_) {
40+
array_unshift($file->contents->stmts, ...[
41+
new Node\Stmt\Use_([
42+
new Node\Stmt\UseUse(
43+
new Node\Name(
44+
$namespace . 'Hydrator',
45+
)
46+
)
47+
]),
48+
new Node\Stmt\Use_([
49+
new Node\Stmt\UseUse(
50+
new Node\Name(
51+
$namespace . 'Operation',
52+
)
53+
)
54+
]),
55+
new Node\Stmt\Use_([
56+
new Node\Stmt\UseUse(
57+
new Node\Name(
58+
$namespace . 'Schema',
59+
)
60+
)
61+
]),
62+
new Node\Stmt\Use_([
63+
new Node\Stmt\UseUse(
64+
new Node\Name(
65+
$namespace . 'WebHook',
66+
)
67+
)
68+
]),
69+
]);
70+
}
71+
$fileContents = ($file->contents instanceof Node\Stmt\Namespace_ ? $codePrinter->prettyPrintFile([
4072
new Node\Stmt\Declare_([
4173
new Node\Stmt\DeclareDeclare('strict_types', new Node\Scalar\LNumber(1)),
4274
]),

src/Generator/Client.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,22 +162,22 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
162162
$factory->method('call')->makePublic()->setDocComment(
163163
new Doc(implode(PHP_EOL, [
164164
'/**',
165-
' * @return ' . (function (string $namespace, array $operations): string {
165+
' * @return ' . (function (array $operations): string {
166166
$count = count($operations);
167167
$lastItem = $count - 1;
168168
$left = '';
169169
$right = '';
170170
for ($i = 0; $i < $count; $i++) {
171-
$returnType = implode('|', array_map(static fn (string $className): string => strpos($className, '\\') === 0 ? $className : $namespace . 'Schema\\' . $className, array_unique($operations[$i]->returnType)));
171+
$returnType = implode('|', array_map(static fn (string $className): string => strpos($className, '\\') === 0 ? $className : 'Schema\\' . $className, array_unique($operations[$i]->returnType)));
172172
if ($i !== $lastItem) {
173-
$left .= '($call is ' . $namespace . 'Operation\\' . $operations[$i]->classNameSanitized . '::OPERATION_MATCH ? ' . $returnType . ' : ';
173+
$left .= '($call is ' . 'Operation\\' . $operations[$i]->classNameSanitized . '::OPERATION_MATCH ? ' . $returnType . ' : ';
174174
} else {
175175
$left .= $returnType;
176176
}
177177
$right .= ')';
178178
}
179179
return $left . $right;
180-
})($namespace, $operations),
180+
})($operations),
181181
' */',
182182
]))
183183
)->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))->addStmt(new Node\Stmt\Return_(
@@ -203,30 +203,30 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
203203
$factory->method('callAsync')->makePublic()->setDocComment(
204204
new Doc(implode(PHP_EOL, [
205205
'/**',
206-
' * @return ' . (function (string $namespace, array $operations): string {
206+
' * @return ' . (function (array $operations): string {
207207
$count = count($operations);
208208
$lastItem = $count - 1;
209209
$left = '';
210210
$right = '';
211211
for ($i = 0; $i < $count; $i++) {
212-
$returnType = implode('|', array_map(static fn (string $className): string => strpos($className, '\\') === 0 ? $className : $namespace . 'Schema\\' . $className, array_unique($operations[$i]->returnType)));
212+
$returnType = implode('|', array_map(static fn (string $className): string => strpos($className, '\\') === 0 ? $className : 'Schema\\' . $className, array_unique($operations[$i]->returnType)));
213213
if ($i !== $lastItem) {
214-
$left .= '($call is ' . $namespace . 'Operation\\' . $operations[$i]->classNameSanitized . '::OPERATION_MATCH ? ' . '\\' . PromiseInterface::class . '<' . $returnType . '>' . ' : ';
214+
$left .= '($call is ' . 'Operation\\' . $operations[$i]->classNameSanitized . '::OPERATION_MATCH ? ' . '\\' . PromiseInterface::class . '<' . $returnType . '>' . ' : ';
215215
} else {
216216
$left .= '\\' . PromiseInterface::class . '<' . $returnType . '>';
217217
}
218218
$right .= ')';
219219
}
220220
return $left . $right;
221-
})($namespace, $operations),
221+
})($operations),
222222
' */',
223223
]))
224224
)->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))->addStmt(new Node\Stmt\Switch_(
225225
new Node\Expr\Variable('call'),
226-
iterator_to_array((function (string $namespace, array $paths) use ($factory): iterable {
226+
iterator_to_array((function (array $paths) use ($factory): iterable {
227227
foreach ($paths as $path) {
228228
foreach ($path->operations as $operation) {
229-
$operationClassname = $namespace . 'Operation\\' . Utils::className(str_replace('/', '\\', $operation->className));
229+
$operationClassname = 'Operation\\' . Utils::className(str_replace('/', '\\', $operation->className));
230230
yield new Node\Stmt\Case_(
231231
new Node\Expr\ClassConstFetch(new Node\Name($operationClassname), 'OPERATION_MATCH'),
232232
[
@@ -280,7 +280,7 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
280280
new Node\Name('\array_key_exists'),
281281
[
282282
new Arg(new Node\Expr\ClassConstFetch(
283-
new Node\Name($namespace . 'Hydrator\\' . $path->hydrator->className),
283+
new Node\Name('Hydrator\\' . $path->hydrator->className),
284284
new Node\Name('class'),
285285
)),
286286
new Arg(new Node\Expr\PropertyFetch(
@@ -299,7 +299,7 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
299299
new Node\Expr\Variable('this'),
300300
'hydrator'
301301
), new Node\Expr\ClassConstFetch(
302-
new Node\Name($namespace . 'Hydrator\\' . $path->hydrator->className),
302+
new Node\Name('Hydrator\\' . $path->hydrator->className),
303303
new Node\Name('class'),
304304
)),
305305
new Node\Expr\MethodCall(
@@ -334,7 +334,7 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
334334
new Node\Expr\Variable('this'),
335335
'hydrator'
336336
), new Node\Expr\ClassConstFetch(
337-
new Node\Name($namespace . 'Hydrator\\' . $path->hydrator->className),
337+
new Node\Name('Hydrator\\' . $path->hydrator->className),
338338
new Node\Name('class'),
339339
))),
340340
]),
@@ -397,7 +397,7 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
397397
'uses' => [
398398
new Node\Expr\Variable('operation'),
399399
],
400-
'returnType' => count($operation->returnType) > 0 ? new Node\UnionType(array_map(static fn(string $object): Node\Name => new Node\Name(strpos($object, '\\') === 0 ? $object : $namespace . 'Schema\\' . $object), array_unique($operation->returnType))) : null,
400+
'returnType' => count($operation->returnType) > 0 ? new Node\UnionType(array_map(static fn(string $object): Node\Name => new Node\Name(strpos($object, '\\') === 0 ? $object : 'Schema\\' . $object), array_unique($operation->returnType))) : null,
401401
]))
402402
]
403403
)),
@@ -407,7 +407,7 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
407407
// yield new Node\Stmt\Echo_([new Node\Scalar\String_('/**' . @var_export($operationCall, true) . '*/')]);
408408
}
409409
}
410-
})($namespace, $client->paths))
410+
})($client->paths))
411411
))->addStmt(
412412
new Node\Stmt\Throw_(
413413
new Node\Expr\New_(

src/Generator/ClientInterface.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,22 @@ public static function generate(string $namespace, array $operations): iterable
7979
$factory->method('call')->makePublic()->setDocComment(
8080
new Doc(implode(PHP_EOL, [
8181
'/**',
82-
' * @return ' . (function (string $namespace, array $operations): string {
82+
' * @return ' . (function (array $operations): string {
8383
$count = count($operations);
8484
$lastItem = $count - 1;
8585
$left = '';
8686
$right = '';
8787
for ($i = 0; $i < $count; $i++) {
88-
$returnType = implode('|', array_map(static fn (string $className): string => strpos($className, '\\') === 0 ? $className : $namespace . 'Schema\\' . $className, array_unique($operations[$i]->returnType)));
88+
$returnType = implode('|', array_map(static fn (string $className): string => strpos($className, '\\') === 0 ? $className : 'Schema\\' . $className, array_unique($operations[$i]->returnType)));
8989
if ($i !== $lastItem) {
90-
$left .= '($call is ' . $namespace . 'Operation\\' . $operations[$i]->classNameSanitized . '::OPERATION_MATCH ? ' . $returnType . ' : ';
90+
$left .= '($call is ' . 'Operation\\' . $operations[$i]->classNameSanitized . '::OPERATION_MATCH ? ' . $returnType . ' : ';
9191
} else {
9292
$left .= $returnType;
9393
}
9494
$right .= ')';
9595
}
9696
return $left . $right;
97-
})($namespace, $operations),
97+
})($operations),
9898
' */',
9999
]))
100100
)->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))
@@ -104,22 +104,22 @@ public static function generate(string $namespace, array $operations): iterable
104104
$factory->method('callAsync')->makePublic()->setDocComment(
105105
new Doc(implode(PHP_EOL, [
106106
'/**',
107-
' * @return ' . (function (string $namespace,array $operations): string {
107+
' * @return ' . (function (array $operations): string {
108108
$count = count($operations);
109109
$lastItem = $count - 1;
110110
$left = '';
111111
$right = '';
112112
for ($i = 0; $i < $count; $i++) {
113-
$returnType = implode('|', array_map(static fn (string $className): string => strpos($className, '\\') === 0 ? $className : $namespace . 'Schema\\' . $className, array_unique($operations[$i]->returnType)));
113+
$returnType = implode('|', array_map(static fn (string $className): string => strpos($className, '\\') === 0 ? $className : 'Schema\\' . $className, array_unique($operations[$i]->returnType)));
114114
if ($i !== $lastItem) {
115-
$left .= '($call is ' . $namespace . 'Operation\\' . $operations[$i]->classNameSanitized . '::OPERATION_MATCH ? ' . '\\' . PromiseInterface::class . '<' . $returnType . '>' . ' : ';
115+
$left .= '($call is ' . 'Operation\\' . $operations[$i]->classNameSanitized . '::OPERATION_MATCH ? ' . '\\' . PromiseInterface::class . '<' . $returnType . '>' . ' : ';
116116
} else {
117117
$left .= '\\' . PromiseInterface::class . '<' . $returnType . '>';
118118
}
119119
$right .= ')';
120120
}
121121
return $left . $right;
122-
})($namespace, $operations),
122+
})($operations),
123123
' */',
124124
]))
125125
)->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))

src/Generator/Hydrators.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
3535
$hydrators = array_filter($hydrators, static fn (\ApiClients\Tools\OpenApiClientGenerator\Representation\Hydrator $hydrator): bool => count($usefullHydrators[$hydrator->className]) > 0);
3636

3737
foreach ($hydrators as $hydrator) {
38-
$class->addStmt($factory->property($hydrator->methodName)->setType('?' . $namespace . 'Hydrator\\' . str_replace('/', '\\', $hydrator->className))->setDefault(null)->makePrivate());
38+
$class->addStmt($factory->property($hydrator->methodName)->setType('?' . 'Hydrator\\' . str_replace('/', '\\', $hydrator->className))->setDefault(null)->makePrivate());
3939
}
4040

4141
$class->addStmt(
@@ -223,15 +223,15 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
223223

224224
foreach ($hydrators as $hydrator) {
225225
$class->addStmt(
226-
$factory->method('getObjectMapper' . ucfirst($hydrator->methodName))->makePublic()->setReturnType($namespace . 'Hydrator\\' . str_replace('/', '\\', $hydrator->className))->addStmts([
226+
$factory->method('getObjectMapper' . ucfirst($hydrator->methodName))->makePublic()->setReturnType('Hydrator\\' . str_replace('/', '\\', $hydrator->className))->addStmts([
227227
new Node\Stmt\If_(
228228
new Node\Expr\BinaryOp\Identical(
229229
new Node\Expr\Instanceof_(
230230
new Node\Expr\PropertyFetch(
231231
new Node\Expr\Variable('this'),
232232
$hydrator->methodName
233233
),
234-
new Node\Expr\ConstFetch(new Node\Name($namespace . 'Hydrator\\' . str_replace('/', '\\', $hydrator->className))),
234+
new Node\Expr\ConstFetch(new Node\Name('Hydrator\\' . str_replace('/', '\\', $hydrator->className))),
235235
),
236236
new Node\Expr\ConstFetch(new Node\Name('false')),
237237
),
@@ -244,7 +244,7 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
244244
$hydrator->methodName
245245
),
246246
new Node\Expr\New_(
247-
new Node\Name($namespace . 'Hydrator\\' . str_replace('/', '\\', $hydrator->className))
247+
new Node\Name('Hydrator\\' . str_replace('/', '\\', $hydrator->className))
248248
),
249249
),
250250
),

0 commit comments

Comments
 (0)