Skip to content

Commit 738d4d6

Browse files
Add line number tracking to CognitiveMetrics (#51)
1 parent 5194050 commit 738d4d6

4 files changed

Lines changed: 19 additions & 2 deletions

File tree

src/Business/Cognitive/CognitiveMetrics.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class CognitiveMetrics implements JsonSerializable
3131
private string $class;
3232
private string $method;
3333
private string $file;
34+
private int $line;
3435
private ?int $timesChanged = null;
3536

3637
private int $lineCount = 0;
@@ -75,6 +76,7 @@ public function __construct(array $metrics)
7576
$this->method = $metrics['method'];
7677
$this->class = $metrics['class'];
7778
$this->file = $metrics['file'] ?? null;
79+
$this->line = $metrics['line'] ?? 0;
7880

7981
$this->setRequiredMetricProperties($metrics);
8082
$this->setOptionalMetricProperties($metrics);
@@ -388,6 +390,11 @@ public function getFileName(): string
388390
return $this->file;
389391
}
390392

393+
public function getLine(): int
394+
{
395+
return $this->line;
396+
}
397+
391398
public function equals(self $metrics): bool
392399
{
393400
return $metrics->getClass() === $this->class
@@ -403,6 +410,7 @@ public function toArray(): array
403410
'class' => $this->class,
404411
'method' => $this->method,
405412
'file' => $this->file,
413+
'line' => $this->line,
406414
'lineCount' => $this->lineCount,
407415
'argCount' => $this->argCount,
408416
'returnCount' => $this->returnCount,

src/PhpParser/CognitiveMetricsVisitor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function resetAll(): void
9797
private function createMetricsArray(Node\Stmt\ClassMethod $node): array
9898
{
9999
return [
100+
'line' => $node->getStartLine(),
100101
'lineCount' => $this->calculateLineCount($node),
101102
'argCount' => $this->countMethodArguments($node),
102103
'returnCount' => 0,

tests/Unit/Business/Cognitive/CognitiveMetricsTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ protected function setUp(): void
2424
'class' => 'TestClass',
2525
'method' => 'testMethod',
2626
'file' => 'TestClass.php',
27+
'line' => 42,
2728
'lineCount' => 10,
2829
'argCount' => 2,
2930
'returnCount' => 1,
@@ -42,6 +43,7 @@ public function testConstructor(): void
4243

4344
$this->assertSame('TestClass', $metrics->getClass());
4445
$this->assertSame('testMethod', $metrics->getMethod());
46+
$this->assertSame(42, $metrics->getLine());
4547
$this->assertSame(10, $metrics->getLineCount());
4648
$this->assertSame(2, $metrics->getArgCount());
4749
$this->assertSame(1, $metrics->getReturnCount());
@@ -67,6 +69,7 @@ public function testFromArray(): void
6769

6870
$this->assertSame('TestClass', $metrics->getClass());
6971
$this->assertSame('testMethod', $metrics->getMethod());
72+
$this->assertSame(42, $metrics->getLine());
7073
$this->assertSame(10, $metrics->getLineCount());
7174
$this->assertSame(2, $metrics->getArgCount());
7275
$this->assertSame(1, $metrics->getReturnCount());
@@ -94,6 +97,7 @@ public function testToArray(): void
9497
'class' => 'TestClass',
9598
'method' => 'testMethod',
9699
'file' => 'TestClass.php',
100+
'line' => 42,
97101
'lineCount' => 10,
98102
'argCount' => 2,
99103
'returnCount' => 1,
@@ -132,6 +136,7 @@ public function testJsonSerialize(): void
132136
'class' => 'TestClass',
133137
'method' => 'testMethod',
134138
'file' => 'TestClass.php',
139+
'line' => 42,
135140
'lineCount' => 10,
136141
'argCount' => 2,
137142
'returnCount' => 1,
@@ -169,6 +174,7 @@ public function testCalculateDeltas(): void
169174
'class' => 'TestClass',
170175
'method' => 'testMethod',
171176
'file' => 'TestClass.php',
177+
'line' => 42,
172178
'lineCount' => 10,
173179
'argCount' => 2,
174180
'returnCount' => 1,
@@ -194,6 +200,7 @@ public function testCalculateDeltas(): void
194200
'class' => 'TestClass',
195201
'method' => 'testMethod',
196202
'file' => 'TestClass.php',
203+
'line' => 42,
197204
'lineCount' => 10,
198205
'argCount' => 2,
199206
'returnCount' => 1,

tests/Unit/PhpParser/CognitiveMetricsVisitorTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function testMethodMetricsCalculation(): void
5555
$this->assertArrayHasKey('\\MyNamespace\\MyClass::myMethod', $methodMetrics);
5656

5757
$metrics = $methodMetrics['\\MyNamespace\\MyClass::myMethod'];
58+
$this->assertEquals(8, $metrics['line']);
5859
$this->assertEquals(12, $metrics['lineCount']);
5960
$this->assertEquals(2, $metrics['argCount']);
6061
$this->assertEquals(3, $metrics['returnCount']);
@@ -150,7 +151,7 @@ public function normalMethod() {
150151
// Verify that all metrics have the required keys
151152
foreach ($methodMetrics as $methodKey => $metrics) {
152153
$requiredKeys = [
153-
'lineCount', 'argCount', 'returnCount', 'variableCount',
154+
'line', 'lineCount', 'argCount', 'returnCount', 'variableCount',
154155
'propertyCallCount', 'ifCount', 'ifNestingLevel', 'elseCount'
155156
];
156157

@@ -228,7 +229,7 @@ public function __construct(private int $value) {
228229
// Verify that the metrics have all required keys
229230
$metrics = $methodMetrics['\\TestNamespace\\TestClass::methodWithMultipleAnonymousClasses'];
230231
$requiredKeys = [
231-
'lineCount', 'argCount', 'returnCount', 'variableCount',
232+
'line', 'lineCount', 'argCount', 'returnCount', 'variableCount',
232233
'propertyCallCount', 'ifCount', 'ifNestingLevel', 'elseCount'
233234
];
234235

0 commit comments

Comments
 (0)