Skip to content

Prevent failing addNamespace method #352

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

Merged
merged 4 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/Domain/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
/**
* Code originally taken from {SebastianBergmann\PHPLOC\Analyser}.
*
* @method string getNamespaceName(array $tokens, int $i)
* @method bool isClassDeclaration(array $tokens, int $i)
* @method int|bool getPreviousNonWhitespaceTokenPos(array $tokens, $start)
* @method int|bool getNextNonWhitespaceTokenPos(array $tokens, $start)
* @method string getClassName(string $namespace, array $tokens, int $i)
* @method string|false getNamespaceName(array $tokens, int $i)
* @method bool isClassDeclaration(array $tokens, int $i)
* @method int|bool getPreviousNonWhitespaceTokenPos(array $tokens, $start)
* @method int|bool getNextNonWhitespaceTokenPos(array $tokens, $start)
* @method string getClassName(string $namespace, array $tokens, int $i)
*
* @internal
*/
Expand Down Expand Up @@ -163,7 +163,10 @@ private function analyseFile(Collector $collector, string $filename): void
switch ($token) {
case \T_NAMESPACE:
$namespace = $this->getNamespaceName($tokens, $i);
$collector->addNamespace($namespace);

if ($namespace !== false) {
$collector->addNamespace($namespace);
}

break;

Expand Down
23 changes: 23 additions & 0 deletions tests/Domain/Insights/EmptyNamespaceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Tests\Domain\Insights;

use NunoMaduro\PhpInsights\Domain\Analyser;
use Tests\TestCase;

final class EmptyNamespaceTest extends TestCase
{
public function testNotFailingOnAnalyzingEmptyNamespaceFile(): void
{
$files = [
__DIR__ . '/Fixtures/EmptyNamespaceFile.php',
];

$analyzer = new Analyser();
$collector = $analyzer->analyse(__DIR__ . '/Fixtures/', $files);

self::assertNotEmpty($collector);
}
}
4 changes: 4 additions & 0 deletions tests/Domain/Insights/Fixtures/EmptyNamespaceFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

namespace {
}