Skip to content

Commit

Permalink
Added package names for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ariddlestone committed Jul 12, 2023
1 parent 2388165 commit 80800bc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Core/Ast/Parser/NikicPhpParser/FileReferenceVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@ private function enterFunction(Node\Stmt\Function_ $node): void
}
}
}

$docComment = $node->getDocComment();
if (null !== $docComment) {
$tokens = new TokenIterator($this->lexer->tokenize($docComment->getText()));
$docNode = $this->docParser->parse($tokens);
$packageNames = array_map(
static fn (PhpDocTagNode $docNode) => new PackageName((string) $docNode->value),
$docNode->getTagsByName('@package')
);
foreach ($packageNames as $packageName) {
$this->currentReference->addMetaDatum($packageName);
}
}
}

private function getClassReferenceName(ClassLike $node): ?string
Expand Down
11 changes: 11 additions & 0 deletions tests/Core/Ast/Parser/NikicPhpParser/Fixtures/PackageNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ class PackageAClass
class NoPackageClass
{
}

/**
* @package PackageB
*/
function packageBFunction(): void
{
}

function noPackageFunction(): void
{
}
10 changes: 10 additions & 0 deletions tests/Core/Ast/Parser/NikicPhpParser/NikicPhpParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,15 @@ public function testParsePackageNames(): void

$packageNames = array_filter($astClassReferences[1]->getMetaData(), $filterPackageNames);
self::assertCount(0, $packageNames);

$astFunctionReferences = $astFileReference->functionReferences;
self::assertCount(2, $astFunctionReferences);

$packageNames = array_filter($astFunctionReferences[0]->getMetaData(), $filterPackageNames);
self::assertCount(1, $packageNames);
$this->assertSame('PackageB', $packageNames[0]->getPackageName());

$packageNames = array_filter($astFunctionReferences[1]->getMetaData(), $filterPackageNames);
self::assertCount(0, $packageNames);
}
}

0 comments on commit 80800bc

Please sign in to comment.