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

Printing empty namespace #147

Closed
GameCharmer opened this issue Dec 5, 2023 · 5 comments
Closed

Printing empty namespace #147

GameCharmer opened this issue Dec 5, 2023 · 5 comments

Comments

@GameCharmer
Copy link

Version: 4.1.2

Bug Description

Implementing interfaces by alias results in those aliases being addressed as root.

Steps To Reproduce

$cNamespace = new PhpNamespace('GCWorld\\ORM\\Generated');
$cClass     = $cNamespace->addClass('ExampleClass');
$cNamespace->addUse('GCWorld\\ORM\\Abstracts\\DirectSingle', 'dbc');
$cNamespace->addUse('GCWorld\\ORM\\Interfaces\\GeneratedInterafce','dbi');
$cClass->setExtends('dbc');
$cClass->addImplement('dbi');

echo $cNamespace;

results in

namespace GCWorld\ORM\Generated;

use GCWorld\ORM\Abstracts\DirectSingle as dbc;
use GCWorld\ORM\Interfaces\GeneratedInterafce as dbi;

class ExampleClass extends \dbc implements \dbi
{
}

Expected Behavior

The backslashes in extends and implements shouldn't exist.

Possible Solution

@dg
Copy link
Member

dg commented Dec 5, 2023

@dg dg closed this as completed Dec 5, 2023
@GameCharmer
Copy link
Author

I just updated from 4.0.5 to 4.1.2 and couldn't find any reference to this new "feature" in the release notes. /shrug

@dg
Copy link
Member

dg commented Dec 5, 2023

This isn't a new feature, it's how it's worked since the beginning.

@GameCharmer
Copy link
Author

This works in 4.0.5 without the leading slashes, but fails catastrophically in 4.1.2

$cNamespace = new PhpNamespace('GCWorld\\ORM\\Generated');
$cClass     = new ClassType('ExampleClass', $cNamespace);
$cNamespace->addUse('GCWorld\\ORM\\Abstracts\\DirectSingle', 'dbc');
$cNamespace->addUse('GCWorld\\ORM\\Interfaces\\GeneratedInterafce','dbi');
$cClass->setExtends('dbc');
$cClass->addImplement('dbi');

$cPrinter  = new PsrPrinter();
$contents  = '<?php'.PHP_EOL;
$contents .= $cPrinter->printNamespace($cNamespace);
$contents .= $cPrinter->printClass($cClass);
// Write contents to file

Seems there was a backwards incompatible change somewhere in there.

@dg
Copy link
Member

dg commented Dec 6, 2023

Well, that's a different code. I'll fix the behavior, for now you can use $cPrinter->omitEmptyNamespaces = false;

@dg dg changed the title Broken Aliases Printing empty namespace Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants