Skip to content

ContextFactory is not clever enough about use statements #8

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

Closed
asgrim opened this issue Jul 14, 2015 · 4 comments
Closed

ContextFactory is not clever enough about use statements #8

asgrim opened this issue Jul 14, 2015 · 4 comments

Comments

@asgrim
Copy link
Contributor

asgrim commented Jul 14, 2015

I have a use case where used traits are being incorrectly detected as namespace imports.

Looking at ContextFactory->createForNamespace, it seems that the check is simply looking for all T_USE tokens, and not checking whether they are within the context of a T_CLASS token.

For example, given this PHP code:

<?php
namespace Foo;

trait FooTrait {}

class FooClass {
    use FooTrait;
}

... when creating context for this, the namespace aliases should be simply empty array [] as there ARE no namespace aliases. However, ContextFactory currently picks up the use FooTrait; and thinks it is a namespace alias.

I have provided a failing test case that can be added to tests/unit/Types/ContextFactoryTest.php:

public function testTraitUseIsNotDetectedAsNamespaceUse()
{
    $fixture = new ContextFactory();

    $php = "<?php
    namespace Foo;

    trait FooTrait {}

    class FooClass {
        use FooTrait;
    }
    ";

    $fixture = new ContextFactory();
    $context = $fixture->createForNamespace('Foo', $php);

    $this->assertSame([], $context->getNamespaceAliases());
}
@asgrim
Copy link
Contributor Author

asgrim commented Jul 14, 2015

Created proposed fix for this issue in #9

@asgrim
Copy link
Contributor Author

asgrim commented Jul 19, 2015

Also see improvements in #10

@mvriel
Copy link
Member

mvriel commented Dec 2, 2015

@asgrim can you verify that your pull requests (for which I am very grateful) have fixed this issue?

@asgrim
Copy link
Contributor Author

asgrim commented Dec 2, 2015

@mvriel yep, looks like it, thank you :)

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