Skip to content

Commit

Permalink
Implement Class_::file()
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Oct 9, 2024
1 parent 27a4a34 commit 99b1561
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/StaticAnalysis/CodeUnitFindingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
*/
final class CodeUnitFindingVisitor extends NodeVisitorAbstract
{
/**
* @var non-empty-string
*/
private string $file;

/**
* @var array<string, \SebastianBergmann\CodeCoverage\StaticAnalysis\Interface_>
*/
Expand All @@ -54,6 +59,14 @@ final class CodeUnitFindingVisitor extends NodeVisitorAbstract
*/
private array $functions = [];

/**
* @param non-empty-string $file
*/
public function __construct(string $file)
{
$this->file = $file;
}

public function enterNode(Node $node): void
{
if ($node instanceof Interface_) {
Expand Down Expand Up @@ -109,6 +122,7 @@ public function leaveNode(Node $node): void
$this->classes[$namespacedClassName]->name(),
$this->classes[$namespacedClassName]->namespacedName(),
$this->classes[$namespacedClassName]->namespace(),
$this->classes[$namespacedClassName]->file(),
$this->classes[$namespacedClassName]->startLine(),
$this->classes[$namespacedClassName]->endLine(),
$this->classes[$namespacedClassName]->parentClass(),
Expand Down Expand Up @@ -274,6 +288,7 @@ private function processClass(Class_ $node): void
$name,
$namespacedName,
$this->namespace($namespacedName, $name),
$this->file,
$node->getStartLine(),
$node->getEndLine(),
$parentClass,
Expand Down
2 changes: 1 addition & 1 deletion src/StaticAnalysis/ParsingFileAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private function analyse(string $filename): void
assert($nodes !== null);

$traverser = new NodeTraverser;
$codeUnitFindingVisitor = new CodeUnitFindingVisitor;
$codeUnitFindingVisitor = new CodeUnitFindingVisitor($filename);
$lineCountingVisitor = new LineCountingVisitor($linesOfCode);
$ignoredLinesFindingVisitor = new IgnoredLinesFindingVisitor($this->useAnnotationsForIgnoringCode, $this->ignoreDeprecatedCode);
$executableLinesFindingVisitor = new ExecutableLinesFindingVisitor($source);
Expand Down
17 changes: 16 additions & 1 deletion src/StaticAnalysis/Value/Class_.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
private string $namespacedName;
private string $namespace;

/**
* @var non-empty-string
*/
private string $file;

/**
* @var non-negative-int
*/
Expand Down Expand Up @@ -58,18 +63,20 @@
/**
* @param non-empty-string $name
* @param non-empty-string $namespacedName
* @param non-empty-string $file
* @param non-negative-int $startLine
* @param non-negative-int $endLine
* @param ?non-empty-string $parentClass
* @param list<non-empty-string> $interfaces
* @param list<non-empty-string> $traits
* @param array<non-empty-string, Method> $methods
*/
public function __construct(string $name, string $namespacedName, string $namespace, int $startLine, int $endLine, ?string $parentClass, array $interfaces, array $traits, array $methods)
public function __construct(string $name, string $namespacedName, string $namespace, string $file, int $startLine, int $endLine, ?string $parentClass, array $interfaces, array $traits, array $methods)
{
$this->name = $name;
$this->namespacedName = $namespacedName;
$this->namespace = $namespace;
$this->file = $file;
$this->startLine = $startLine;
$this->endLine = $endLine;
$this->parentClass = $parentClass;
Expand Down Expand Up @@ -104,6 +111,14 @@ public function namespace(): string
return $this->namespace;
}

/**
* @return non-empty-string
*/
public function file(): string
{
return $this->file;
}

/**
* @return non-negative-int
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/StaticAnalysis/CodeUnitFindingVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ private function findCodeUnits(string $filename): CodeUnitFindingVisitor
assert($nodes !== null);

$traverser = new NodeTraverser;
$codeUnitFindingVisitor = new CodeUnitFindingVisitor;
$codeUnitFindingVisitor = new CodeUnitFindingVisitor($filename);

$traverser->addVisitor(new NameResolver);
$traverser->addVisitor(new ParentConnectingVisitor);
Expand Down
6 changes: 6 additions & 0 deletions tests/tests/StaticAnalysis/Value/ClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function testHasNamespaced(): void
$this->assertSame('example', $this->class()->namespace());
}

public function testHasFile(): void
{
$this->assertSame('file.php', $this->class()->file());
}

public function testHasStartLine(): void
{
$this->assertSame(1, $this->class()->startLine());
Expand Down Expand Up @@ -81,6 +86,7 @@ private function class(?string $parentClass = null, array $interfaces = [], arra
'Example',
'example\Example',
'example',
'file.php',
1,
2,
$parentClass,
Expand Down

0 comments on commit 99b1561

Please sign in to comment.