Skip to content

Commit

Permalink
Merge pull request #9 from asgrim/ignore-class-trait-usage
Browse files Browse the repository at this point in the history
Ignore any tokens within classes when looking for namespaces
  • Loading branch information
mvriel committed Jul 18, 2015
2 parents 14bb2da + 0c11dd9 commit 83e3125
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Types/ContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ public function createForNamespace($namespace, $fileContents)
case T_NAMESPACE:
$currentNamespace = $this->parseNamespace($tokens);
break;
case T_CLASS:
// Fast-forward the iterator through the class so that any
// T_USE tokens found within are skipped - these are not
// valid namespace use statements so should be ignored.
$braceLevel = 0;
$firstBraceFound = false;
while ($tokens->valid() && ($braceLevel > 0 || !$firstBraceFound)) {
if ($tokens->current() === '{') {
if (!$firstBraceFound) {
$firstBraceFound = true;
}
$braceLevel++;
}

if ($tokens->current() === '}') {
$braceLevel--;
}
$tokens->next();
}
break;
case T_USE:
if ($currentNamespace === $namespace) {
$useStatements = array_merge($useStatements, $this->parseUseStatement($tokens));
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/Types/ContextFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ public function testReadsAliasesFromProvidedNamespaceAndContent()

$this->assertSame($expected, $context->getNamespaceAliases());
}

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());
}
}
}

Expand Down

0 comments on commit 83e3125

Please sign in to comment.