Skip to content
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

Fix Generator not generating namespaces to the written classes #98

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 10 additions & 3 deletions src/Codegen/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
PHP);

$file->name = $classType->getName() . '.php';
$file->content = self::asPhpFile($classType);
$file->content = self::asPhpFile($classType, $namespace);

return $file;
}
Expand Down Expand Up @@ -144,18 +144,25 @@
return array_reverse($parts)[0];
}

protected static function asPhpFile(ClassType $classType): string
protected static function asPhpFile(ClassType $classType, string $namespace): string
{
$printer = new PsrPrinter();
$phpNamespace = $classType->getNamespace();
$class = $printer->printClass($classType, $phpNamespace);

$outputNamespace = (string) $phpNamespace;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not rely on magic __toString().

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This workaround needs a healthy, long code comment to explain its purpose.

if (empty(trim($outputNamespace))) {

Check failure on line 154 in src/Codegen/Generator.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis

Construct empty() is not allowed. Use more strict comparison.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare with null or ''.

$outputNamespace = "namespace {$namespace};

Check warning on line 155 in src/Codegen/Generator.php

View check run for this annotation

Codecov / codecov/patch

src/Codegen/Generator.php#L155

Added line #L155 was not covered by tests

";

Check warning on line 157 in src/Codegen/Generator.php

View check run for this annotation

Codecov / codecov/patch

src/Codegen/Generator.php#L157

Added line #L157 was not covered by tests
}

return <<<PHP
<?php

declare(strict_types=1);

{$phpNamespace}{$class}
{$outputNamespace}{$class}
PHP;
}

Expand Down
Loading