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

PhpNamespace::unresolveName() does not preserve intentional leading backslash #21

Closed
mrtnzlml opened this issue Nov 26, 2015 · 6 comments

Comments

@mrtnzlml
Copy link

Due to this commit (6e1b293) it's not possible to generate namespace with leading backslash. I know it was intended, but unfortunately it can be BC break. For example Kdyby\AOP generates class namespaces with leading backslash, but classes are in custom block namespace and if the leading ** is removed, it generates nonsense:

namespace Kdyby\Aop_CG\Container_1ad39c5e84 {

use Kdyby\Aop\Pointcut\Matcher\Criteria;
use Symfony\Component\PropertyAccess\PropertyAccess;

final class Model_PagesClass_85_Model_Pages extends Model\Pages
{
    // ...                                          ^- should be \Model\Pages

It would be great to preserve leading ** (example why). Should I prepare PR or it's completely wrong approach and should it be fixed in Kdyby?

Suggested behavior (PhpNamespace.phpt:13):

$namespace = new PhpNamespace;

Assert::same('\A', $namespace->unresolveName('\A')); //preserve leading backslash
Assert::same('A', $namespace->unresolveName('A'));
Assert::same('foo\A', $namespace->unresolveName('foo\A'));
@dg
Copy link
Member

dg commented Nov 27, 2015

What about to preserve class names when no namespace is passed to class constructor?

@mrtnzlml
Copy link
Author

It would be satisfactory i guess. Namespace doesn't change anyway in this particular case. In other words it means return name immediately if there is no namespace (passed through constructor), right? If so, I'll prepare PR.

@dg
Copy link
Member

dg commented Nov 27, 2015

I already have PR :)

dg added a commit that referenced this issue Nov 27, 2015
… specified [#21]

Fixed BC break introduced in v2.3.3
@mrtnzlml
Copy link
Author

It's much more better than my naive implementation. Thank you.

dg added a commit that referenced this issue Nov 29, 2015
… specified [#21]

Fixed BC break introduced in v2.3.3
@ignasbernotas
Copy link

ignasbernotas commented Jun 16, 2017

@mrtnzlml @dg due to this change I am unable to extend a class with another class on a separate namespace. the only workaround is to use the full namespace after the extends keyword (which sucks :/).

I'm on v2.6 since I need php 5.6 support.

Example:

namespace App\Http\Controllers\Users;

use App\Http\Controllers\Controller;

class UserController extends \Controller
{
}

@dg
Copy link
Member

dg commented Jun 19, 2017

Why not?

$namespace = new PhpNamespace('Foo');
$class = $namespace->addClass('A');
$class->addExtend('B');
echo $namespace;

generates

namespace Foo;

class A extends \B
{
}

or

$namespace = new PhpNamespace('Foo');
$namespace->addUse('B');
$class = $namespace->addClass('A');
$class->addExtend('B');
echo $namespace;

generates

namespace Foo;        
                      
use B;                
                      
class A extends B     
{                     
}                     

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

3 participants