Skip to content

Use UseUse instead of using FQCN #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,39 @@ public function generate(string $namespace, string $destinationPath)

foreach ($this->all($namespace, $destinationPath . DIRECTORY_SEPARATOR) as $file) {
$fileName = $destinationPath . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, substr($file->fqcn, strlen($namespace))) . '.php';
$fileContents = ($file->contents instanceof Node ? $codePrinter->prettyPrintFile([
if ($file->contents instanceof Node\Stmt\Namespace_) {
array_unshift($file->contents->stmts, ...[
new Node\Stmt\Use_([
new Node\Stmt\UseUse(
new Node\Name(
$namespace . 'Hydrator',
)
)
]),
new Node\Stmt\Use_([
new Node\Stmt\UseUse(
new Node\Name(
$namespace . 'Operation',
)
)
]),
new Node\Stmt\Use_([
new Node\Stmt\UseUse(
new Node\Name(
$namespace . 'Schema',
)
)
]),
new Node\Stmt\Use_([
new Node\Stmt\UseUse(
new Node\Name(
$namespace . 'WebHook',
)
)
]),
]);
}
$fileContents = ($file->contents instanceof Node\Stmt\Namespace_ ? $codePrinter->prettyPrintFile([
new Node\Stmt\Declare_([
new Node\Stmt\DeclareDeclare('strict_types', new Node\Scalar\LNumber(1)),
]),
Expand Down
30 changes: 15 additions & 15 deletions src/Generator/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,22 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
$factory->method('call')->makePublic()->setDocComment(
new Doc(implode(PHP_EOL, [
'/**',
' * @return ' . (function (string $namespace, array $operations): string {
' * @return ' . (function (array $operations): string {
$count = count($operations);
$lastItem = $count - 1;
$left = '';
$right = '';
for ($i = 0; $i < $count; $i++) {
$returnType = implode('|', array_map(static fn (string $className): string => strpos($className, '\\') === 0 ? $className : $namespace . 'Schema\\' . $className, array_unique($operations[$i]->returnType)));
$returnType = implode('|', array_map(static fn (string $className): string => strpos($className, '\\') === 0 ? $className : 'Schema\\' . $className, array_unique($operations[$i]->returnType)));
if ($i !== $lastItem) {
$left .= '($call is ' . $namespace . 'Operation\\' . $operations[$i]->classNameSanitized . '::OPERATION_MATCH ? ' . $returnType . ' : ';
$left .= '($call is ' . 'Operation\\' . $operations[$i]->classNameSanitized . '::OPERATION_MATCH ? ' . $returnType . ' : ';
} else {
$left .= $returnType;
}
$right .= ')';
}
return $left . $right;
})($namespace, $operations),
})($operations),
' */',
]))
)->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))->addStmt(new Node\Stmt\Return_(
Expand All @@ -203,30 +203,30 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
$factory->method('callAsync')->makePublic()->setDocComment(
new Doc(implode(PHP_EOL, [
'/**',
' * @return ' . (function (string $namespace, array $operations): string {
' * @return ' . (function (array $operations): string {
$count = count($operations);
$lastItem = $count - 1;
$left = '';
$right = '';
for ($i = 0; $i < $count; $i++) {
$returnType = implode('|', array_map(static fn (string $className): string => strpos($className, '\\') === 0 ? $className : $namespace . 'Schema\\' . $className, array_unique($operations[$i]->returnType)));
$returnType = implode('|', array_map(static fn (string $className): string => strpos($className, '\\') === 0 ? $className : 'Schema\\' . $className, array_unique($operations[$i]->returnType)));
if ($i !== $lastItem) {
$left .= '($call is ' . $namespace . 'Operation\\' . $operations[$i]->classNameSanitized . '::OPERATION_MATCH ? ' . '\\' . PromiseInterface::class . '<' . $returnType . '>' . ' : ';
$left .= '($call is ' . 'Operation\\' . $operations[$i]->classNameSanitized . '::OPERATION_MATCH ? ' . '\\' . PromiseInterface::class . '<' . $returnType . '>' . ' : ';
} else {
$left .= '\\' . PromiseInterface::class . '<' . $returnType . '>';
}
$right .= ')';
}
return $left . $right;
})($namespace, $operations),
})($operations),
' */',
]))
)->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))->addStmt(new Node\Stmt\Switch_(
new Node\Expr\Variable('call'),
iterator_to_array((function (string $namespace, array $paths) use ($factory): iterable {
iterator_to_array((function (array $paths) use ($factory): iterable {
foreach ($paths as $path) {
foreach ($path->operations as $operation) {
$operationClassname = $namespace . 'Operation\\' . Utils::className(str_replace('/', '\\', $operation->className));
$operationClassname = 'Operation\\' . Utils::className(str_replace('/', '\\', $operation->className));
yield new Node\Stmt\Case_(
new Node\Expr\ClassConstFetch(new Node\Name($operationClassname), 'OPERATION_MATCH'),
[
Expand Down Expand Up @@ -280,7 +280,7 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
new Node\Name('\array_key_exists'),
[
new Arg(new Node\Expr\ClassConstFetch(
new Node\Name($namespace . 'Hydrator\\' . $path->hydrator->className),
new Node\Name('Hydrator\\' . $path->hydrator->className),
new Node\Name('class'),
)),
new Arg(new Node\Expr\PropertyFetch(
Expand All @@ -299,7 +299,7 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
new Node\Expr\Variable('this'),
'hydrator'
), new Node\Expr\ClassConstFetch(
new Node\Name($namespace . 'Hydrator\\' . $path->hydrator->className),
new Node\Name('Hydrator\\' . $path->hydrator->className),
new Node\Name('class'),
)),
new Node\Expr\MethodCall(
Expand Down Expand Up @@ -334,7 +334,7 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
new Node\Expr\Variable('this'),
'hydrator'
), new Node\Expr\ClassConstFetch(
new Node\Name($namespace . 'Hydrator\\' . $path->hydrator->className),
new Node\Name('Hydrator\\' . $path->hydrator->className),
new Node\Name('class'),
))),
]),
Expand Down Expand Up @@ -397,7 +397,7 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
'uses' => [
new Node\Expr\Variable('operation'),
],
'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,
'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,
]))
]
)),
Expand All @@ -407,7 +407,7 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
// yield new Node\Stmt\Echo_([new Node\Scalar\String_('/**' . @var_export($operationCall, true) . '*/')]);
}
}
})($namespace, $client->paths))
})($client->paths))
))->addStmt(
new Node\Stmt\Throw_(
new Node\Expr\New_(
Expand Down
16 changes: 8 additions & 8 deletions src/Generator/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,22 @@ public static function generate(string $namespace, array $operations): iterable
$factory->method('call')->makePublic()->setDocComment(
new Doc(implode(PHP_EOL, [
'/**',
' * @return ' . (function (string $namespace, array $operations): string {
' * @return ' . (function (array $operations): string {
$count = count($operations);
$lastItem = $count - 1;
$left = '';
$right = '';
for ($i = 0; $i < $count; $i++) {
$returnType = implode('|', array_map(static fn (string $className): string => strpos($className, '\\') === 0 ? $className : $namespace . 'Schema\\' . $className, array_unique($operations[$i]->returnType)));
$returnType = implode('|', array_map(static fn (string $className): string => strpos($className, '\\') === 0 ? $className : 'Schema\\' . $className, array_unique($operations[$i]->returnType)));
if ($i !== $lastItem) {
$left .= '($call is ' . $namespace . 'Operation\\' . $operations[$i]->classNameSanitized . '::OPERATION_MATCH ? ' . $returnType . ' : ';
$left .= '($call is ' . 'Operation\\' . $operations[$i]->classNameSanitized . '::OPERATION_MATCH ? ' . $returnType . ' : ';
} else {
$left .= $returnType;
}
$right .= ')';
}
return $left . $right;
})($namespace, $operations),
})($operations),
' */',
]))
)->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))
Expand All @@ -104,22 +104,22 @@ public static function generate(string $namespace, array $operations): iterable
$factory->method('callAsync')->makePublic()->setDocComment(
new Doc(implode(PHP_EOL, [
'/**',
' * @return ' . (function (string $namespace,array $operations): string {
' * @return ' . (function (array $operations): string {
$count = count($operations);
$lastItem = $count - 1;
$left = '';
$right = '';
for ($i = 0; $i < $count; $i++) {
$returnType = implode('|', array_map(static fn (string $className): string => strpos($className, '\\') === 0 ? $className : $namespace . 'Schema\\' . $className, array_unique($operations[$i]->returnType)));
$returnType = implode('|', array_map(static fn (string $className): string => strpos($className, '\\') === 0 ? $className : 'Schema\\' . $className, array_unique($operations[$i]->returnType)));
if ($i !== $lastItem) {
$left .= '($call is ' . $namespace . 'Operation\\' . $operations[$i]->classNameSanitized . '::OPERATION_MATCH ? ' . '\\' . PromiseInterface::class . '<' . $returnType . '>' . ' : ';
$left .= '($call is ' . 'Operation\\' . $operations[$i]->classNameSanitized . '::OPERATION_MATCH ? ' . '\\' . PromiseInterface::class . '<' . $returnType . '>' . ' : ';
} else {
$left .= '\\' . PromiseInterface::class . '<' . $returnType . '>';
}
$right .= ')';
}
return $left . $right;
})($namespace, $operations),
})($operations),
' */',
]))
)->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))
Expand Down
8 changes: 4 additions & 4 deletions src/Generator/Hydrators.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
$hydrators = array_filter($hydrators, static fn (\ApiClients\Tools\OpenApiClientGenerator\Representation\Hydrator $hydrator): bool => count($usefullHydrators[$hydrator->className]) > 0);

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

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

foreach ($hydrators as $hydrator) {
$class->addStmt(
$factory->method('getObjectMapper' . ucfirst($hydrator->methodName))->makePublic()->setReturnType($namespace . 'Hydrator\\' . str_replace('/', '\\', $hydrator->className))->addStmts([
$factory->method('getObjectMapper' . ucfirst($hydrator->methodName))->makePublic()->setReturnType('Hydrator\\' . str_replace('/', '\\', $hydrator->className))->addStmts([
new Node\Stmt\If_(
new Node\Expr\BinaryOp\Identical(
new Node\Expr\Instanceof_(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
$hydrator->methodName
),
new Node\Expr\ConstFetch(new Node\Name($namespace . 'Hydrator\\' . str_replace('/', '\\', $hydrator->className))),
new Node\Expr\ConstFetch(new Node\Name('Hydrator\\' . str_replace('/', '\\', $hydrator->className))),
),
new Node\Expr\ConstFetch(new Node\Name('false')),
),
Expand All @@ -244,7 +244,7 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
$hydrator->methodName
),
new Node\Expr\New_(
new Node\Name($namespace . 'Hydrator\\' . str_replace('/', '\\', $hydrator->className))
new Node\Name('Hydrator\\' . str_replace('/', '\\', $hydrator->className))
),
),
),
Expand Down
Loading