Skip to content

Commit 14e6024

Browse files
Minor refactor
1 parent 49bca27 commit 14e6024

File tree

2 files changed

+61
-44
lines changed

2 files changed

+61
-44
lines changed

src/Business/Cognitive/CognitiveMetricsCollector.php

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Events\ParserFailed;
99
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Events\SourceFilesFound;
1010
use Phauthentic\CognitiveCodeAnalysis\Business\Utility\DirectoryScanner;
11+
use Phauthentic\CognitiveCodeAnalysis\Business\Utility\FilenameNormalizer;
1112
use Phauthentic\CognitiveCodeAnalysis\CognitiveAnalysisException;
1213
use Phauthentic\CognitiveCodeAnalysis\Config\CognitiveConfig;
1314
use Phauthentic\CognitiveCodeAnalysis\Config\ConfigService;
@@ -118,7 +119,7 @@ private function findMetrics(iterable $files): CognitiveMetricsCollection
118119
$metricsCollection = $this->processMethodMetrics(
119120
$metrics,
120121
$metricsCollection,
121-
$this->normalizeFilename($file)
122+
FilenameNormalizer::normalize($file)
122123
);
123124
}
124125

@@ -141,10 +142,8 @@ private function processMethodMetrics(
141142
continue;
142143
}
143144

144-
145145
[$class, $method] = explode('::', $classAndMethod);
146146

147-
148147
$metricsArray = array_merge($metrics, [
149148
'class' => $class,
150149
'method' => $method,
@@ -192,26 +191,6 @@ private function findSourceFiles(string $path, array $exclude = []): iterable
192191
);
193192
}
194193

195-
/**
196-
* Get the project root directory path.
197-
*
198-
* Start from the current file's directory and traverse up to find composer.json
199-
*
200-
* @return string|null The project root path or null if not found
201-
*/
202-
private function getProjectRoot(): ?string
203-
{
204-
$currentDir = __DIR__;
205-
206-
while ($currentDir !== dirname($currentDir)) {
207-
if (file_exists($currentDir . DIRECTORY_SEPARATOR . 'composer.json')) {
208-
return $currentDir;
209-
}
210-
$currentDir = dirname($currentDir);
211-
}
212-
213-
return null;
214-
}
215194

216195
/**
217196
* Generate a cache key for a file based on path, modification time, and config hash
@@ -260,27 +239,6 @@ public function clearCache(): void
260239
$this->cachePool->clear();
261240
}
262241

263-
/**
264-
* Normalize filename for the test environment
265-
*
266-
* This is to ensure consistent file paths in test outputs
267-
*/
268-
private function normalizeFilename(SplFileInfo $file): string
269-
{
270-
$filename = $file->getRealPath();
271-
272-
if (getenv('APP_ENV') !== 'test') {
273-
return $filename;
274-
}
275-
276-
$projectRoot = $this->getProjectRoot();
277-
if ($projectRoot && str_starts_with($filename, $projectRoot)) {
278-
$filename = substr($filename, strlen($projectRoot) + 1);
279-
}
280-
281-
return $filename;
282-
}
283-
284242
/**
285243
* Try to get cached metrics for a file
286244
*
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phauthentic\CognitiveCodeAnalysis\Business\Utility;
6+
7+
use SplFileInfo;
8+
9+
/**
10+
* Utility class for normalizing filenames, especially for test environments.
11+
*/
12+
class FilenameNormalizer
13+
{
14+
/**
15+
* Normalize filename for the test environment
16+
*
17+
* This is to ensure consistent file paths in test outputs
18+
*
19+
* @param SplFileInfo $file The file to normalize
20+
* @return string The normalized filename
21+
*/
22+
public static function normalize(SplFileInfo $file): string
23+
{
24+
$filename = $file->getRealPath();
25+
26+
if (getenv('APP_ENV') !== 'test') {
27+
return $filename;
28+
}
29+
30+
$projectRoot = self::getProjectRoot();
31+
if ($projectRoot && str_starts_with($filename, $projectRoot)) {
32+
$filename = substr($filename, strlen($projectRoot) + 1);
33+
}
34+
35+
return $filename;
36+
}
37+
38+
/**
39+
* Get the project root directory by traversing up from the current directory
40+
* until composer.json is found.
41+
*
42+
* Start from the current file's directory and traverse up to find composer.json
43+
*
44+
* @return string|null The project root path or null if not found
45+
*/
46+
private static function getProjectRoot(): ?string
47+
{
48+
$currentDir = __DIR__;
49+
50+
while ($currentDir !== dirname($currentDir)) {
51+
if (file_exists($currentDir . DIRECTORY_SEPARATOR . 'composer.json')) {
52+
return $currentDir;
53+
}
54+
$currentDir = dirname($currentDir);
55+
}
56+
57+
return null;
58+
}
59+
}

0 commit comments

Comments
 (0)