Skip to content

Commit

Permalink
Handle multiple namespaces in single file (#1020)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann authored Dec 10, 2021
1 parent 6bfe1c4 commit c24704d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Analysers/TokenScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected function scanTokens(array $tokens): array
$isInterface = false;
$namespace = '';
$currentName = null;
$unitLevel = 0;
$lastToken = null;
$stack = [];

Expand All @@ -44,6 +45,9 @@ protected function scanTokens(array $tokens): array
break;
case '}':
array_pop($stack);
if (count($stack) == $unitLevel) {
$currentName = null;
}
break;
}
continue;
Expand All @@ -69,7 +73,7 @@ protected function scanTokens(array $tokens): array
break;

case T_CLASS:
if ($stack) {
if ($currentName) {
break;
}

Expand All @@ -95,28 +99,31 @@ protected function scanTokens(array $tokens): array

$isInterface = false;
$currentName = $namespace . '\\' . $token[1];
$unitLevel = count($stack);
$units[$currentName] = ['uses' => $uses, 'interfaces' => [], 'traits' => [], 'methods' => [], 'properties' => []];
break;

case T_INTERFACE:
if ($stack) {
if ($currentName) {
break;
}

$isInterface = true;
$token = $this->nextToken($tokens);
$currentName = $namespace . '\\' . $token[1];
$unitLevel = count($stack);
$units[$currentName] = ['uses' => $uses, 'interfaces' => [], 'traits' => [], 'methods' => [], 'properties' => []];
break;

case T_TRAIT:
if ($stack) {
if ($currentName) {
break;
}

$isInterface = false;
$token = $this->nextToken($tokens);
$currentName = $namespace . '\\' . $token[1];
$unitLevel = count($stack);
$this->skipTo($tokens, '{', true);
$units[$currentName] = ['uses' => $uses, 'interfaces' => [], 'traits' => [], 'methods' => [], 'properties' => []];
break;
Expand All @@ -129,7 +136,7 @@ protected function scanTokens(array $tokens): array
if (!is_array($token) || T_IMPLEMENTS !== $token[0]) {
break;
}
// no break
// no break
case T_IMPLEMENTS:
$fqns = $this->parseFQNStatement($tokens, $token);
if ($currentName) {
Expand All @@ -140,7 +147,7 @@ protected function scanTokens(array $tokens): array
case T_FUNCTION:
$token = $this->nextToken($tokens);

if (1 == count($stack) && $currentName) {
if (($unitLevel + 1) == count($stack) && $currentName) {
if (!$isInterface) {
// more nesting
$this->skipTo($tokens, '{', true);
Expand All @@ -153,7 +160,7 @@ protected function scanTokens(array $tokens): array
break;

case T_VARIABLE:
if (1 == count($stack) && $currentName) {
if (($unitLevel + 1) == count($stack) && $currentName) {
$units[$currentName]['properties'][] = substr($token[1], 1);
}
break;
Expand Down
38 changes: 38 additions & 0 deletions tests/Analysers/TokenScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,44 @@ public function scanCases()
],
],
],
'namespaces1' => [
'PHP/namespaces1.php',
[
'Foo\\FooClass' => [
'uses' => [],
'interfaces' => [],
'traits' => [],
'methods' => [],
'properties' => [],
],
'Bar\\BarClass' => [
'uses' => [],
'interfaces' => [],
'traits' => [],
'methods' => [],
'properties' => [],
],
],
'namespaces2' => [
'PHP/namespaces2.php',
[
'Foo\\FooClass' => [
'uses' => [],
'interfaces' => [],
'traits' => [],
'methods' => [],
'properties' => [],
],
'Bar\\BarClass' => [
'uses' => [],
'interfaces' => [],
'traits' => [],
'methods' => [],
'properties' => [],
],
],
],
],
];
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Fixtures/PHP/namespaces1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Foo;

class FooClass
{
}


namespace Bar;

class BarClass
{
}
16 changes: 16 additions & 0 deletions tests/Fixtures/PHP/namespaces2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Foo {

class FooClass
{
}
}

namespace Bar {

class BarClass
{
}

}

0 comments on commit c24704d

Please sign in to comment.