Skip to content

Commit 8301dbb

Browse files
committed
Count processed traits toward LoC/s
1 parent 8acd93b commit 8301dbb

File tree

5 files changed

+44
-22
lines changed

5 files changed

+44
-22
lines changed

src/Analyser/Analyser.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(
3636
/**
3737
* @param string[] $files
3838
* @param Closure(string $file): void|null $preFileCallback
39-
* @param Closure(int ): void|null $postFileCallback
39+
* @param Closure(int, list<string>): void|null $postFileCallback
4040
* @param string[]|null $allAnalysedFiles
4141
*/
4242
public function analyse(
@@ -91,6 +91,7 @@ public function analyse(
9191
$errors = array_merge($errors, $fileAnalyserResult->getErrors());
9292
$filteredPhpErrors = array_merge($filteredPhpErrors, $fileAnalyserResult->getFilteredPhpErrors());
9393
$allPhpErrors = array_merge($allPhpErrors, $fileAnalyserResult->getAllPhpErrors());
94+
$processedFiles = $fileAnalyserResult->getProcessedFiles();
9495

9596
$locallyIgnoredErrors = array_merge($locallyIgnoredErrors, $fileAnalyserResult->getLocallyIgnoredErrors());
9697
$linesToIgnore[$file] = $fileAnalyserResult->getLinesToIgnore();
@@ -114,6 +115,7 @@ public function analyse(
114115
InternalError::STACK_TRACE_METADATA_KEY => InternalError::prepareTrace($t),
115116
InternalError::STACK_TRACE_AS_STRING_METADATA_KEY => $t->getTraceAsString(),
116117
]);
118+
$processedFiles = [$file];
117119
if ($internalErrorsCount >= $this->internalErrorsCountLimit) {
118120
$reachedInternalErrorsCountLimit = true;
119121
break;
@@ -124,7 +126,7 @@ public function analyse(
124126
continue;
125127
}
126128

127-
$postFileCallback(1);
129+
$postFileCallback(1, $processedFiles);
128130
}
129131

130132
return new AnalyserResult(

src/Analyser/FileAnalyser.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ public function analyseFile(
8383
/** @var list<Error> $fileErrors */
8484
$fileErrors = [];
8585

86+
/** @var list<string> $processedFiles */
87+
$processedFiles = [];
88+
8689
/** @var list<Error> $locallyIgnoredErrors */
8790
$locallyIgnoredErrors = [];
8891

@@ -98,10 +101,11 @@ public function analyseFile(
98101
try {
99102
$this->collectErrors($analysedFiles);
100103
$parserNodes = $this->parser->parseFile($file);
104+
$processedFiles[] = $file;
101105
$linesToIgnore = $unmatchedLineIgnores = [$file => $this->getLinesToIgnoreFromTokens($parserNodes)];
102106
$ignoreErrorExtensions = $this->ignoreErrorExtensionProvider->getExtensions();
103107
$temporaryFileErrors = [];
104-
$nodeCallback = function (Node $node, $scope) use (&$fileErrors, &$fileCollectedData, &$fileDependencies, &$usedTraitFileDependencies, &$exportedNodes, $file, $ruleRegistry, $collectorRegistry, $outerNodeCallback, $analysedFiles, &$linesToIgnore, &$unmatchedLineIgnores, &$temporaryFileErrors, $parserNodes, $ignoreErrorExtensions): void {
108+
$nodeCallback = function (Node $node, $scope) use (&$fileErrors, &$fileCollectedData, &$fileDependencies, &$usedTraitFileDependencies, &$exportedNodes, $file, $ruleRegistry, $collectorRegistry, $outerNodeCallback, $analysedFiles, &$linesToIgnore, &$unmatchedLineIgnores, &$temporaryFileErrors, &$processedFiles, $parserNodes, $ignoreErrorExtensions): void {
105109
/** @var Scope&NodeCallbackInvoker $scope */
106110
if ($node instanceof Node\Stmt\Trait_) {
107111
foreach (array_keys($linesToIgnore[$file] ?? []) as $lineToIgnore) {
@@ -115,6 +119,11 @@ public function analyseFile(
115119
if ($node instanceof InTraitNode) {
116120
$traitNode = $node->getOriginalNode();
117121
$linesToIgnore[$scope->getFileDescription()] = $this->getLinesToIgnoreFromTokens([$traitNode]);
122+
123+
$traitFileName = $node->getTraitReflection()->getFileName();
124+
if ($traitFileName !== null) {
125+
$processedFiles[] = $traitFileName;
126+
}
118127
}
119128

120129
if ($scope->isInTrait()) {
@@ -330,6 +339,7 @@ public function analyseFile(
330339
$exportedNodes,
331340
$linesToIgnore,
332341
$unmatchedLineIgnores,
342+
$processedFiles,
333343
);
334344
}
335345

src/Analyser/FileAnalyserResult.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ final class FileAnalyserResult
2323
* @param list<RootExportedNode> $exportedNodes
2424
* @param LinesToIgnore $linesToIgnore
2525
* @param LinesToIgnore $unmatchedLineIgnores
26+
* @param list<string> $processedFiles
2627
*/
2728
public function __construct(
2829
private array $errors,
@@ -35,6 +36,7 @@ public function __construct(
3536
private array $exportedNodes,
3637
private array $linesToIgnore,
3738
private array $unmatchedLineIgnores,
39+
private array $processedFiles,
3840
)
3941
{
4042
}
@@ -119,4 +121,12 @@ public function getUnmatchedLineIgnores(): array
119121
return $this->unmatchedLineIgnores;
120122
}
121123

124+
/**
125+
* @return list<string>
126+
*/
127+
public function getProcessedFiles(): array
128+
{
129+
return $this->processedFiles;
130+
}
131+
122132
}

src/Command/AnalyseApplication.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\ShouldNotHappenException;
1717
use Symfony\Component\Console\Input\InputInterface;
1818
use function array_merge;
19+
use function array_unique;
1920
use function count;
2021
use function fclose;
2122
use function feof;
@@ -237,35 +238,34 @@ private function runAnalyser(
237238
$errorOutput->getStyle()->progressAdvance($allAnalysedFilesCount - $filesCount);
238239
} else {
239240
$startTime = null;
240-
$linesOfCode = null;
241-
$preFileCallback = static function (string $file) use ($stdOutput, &$startTime, &$linesOfCode): void {
241+
$preFileCallback = static function (string $file) use ($stdOutput, &$startTime): void {
242242
$stdOutput->writeLineFormatted($file);
243243
$startTime = microtime(true);
244-
$count = 0;
245-
$handle = @fopen($file, 'r');
246-
if ($handle === false) {
247-
return;
248-
}
249-
250-
while (!feof($handle)) {
251-
fgets($handle);
252-
$count++;
253-
}
254-
fclose($handle);
255-
$linesOfCode = $count;
256244
};
257245
$postFileCallback = null;
258246
if ($stdOutput->isDebug()) {
259247
$previousMemory = memory_get_peak_usage(true);
260-
$postFileCallback = static function () use ($stdOutput, &$previousMemory, &$startTime, &$linesOfCode): void {
248+
$postFileCallback = static function (int $step, array $processedFiles = []) use ($stdOutput, &$previousMemory, &$startTime, &$linesOfCode): void {
261249
if ($startTime === null) {
262250
throw new ShouldNotHappenException();
263251
}
264-
if ($linesOfCode === null) {
265-
throw new ShouldNotHappenException();
266-
}
267252
$currentTotalMemory = memory_get_peak_usage(true);
268253
$elapsedTime = microtime(true) - $startTime;
254+
255+
$linesOfCode = 0;
256+
foreach (array_unique($processedFiles) as $processedFile) {
257+
$handle = @fopen($processedFile, 'r');
258+
if ($handle === false) {
259+
continue;
260+
}
261+
262+
while (!feof($handle)) {
263+
fgets($handle);
264+
$linesOfCode++;
265+
}
266+
fclose($handle);
267+
}
268+
269269
$stdOutput->writeLineFormatted(sprintf('--- consumed %s, total %s, took %.2f s, %.3f LoC/s', BytesHelper::bytes($currentTotalMemory - $previousMemory), BytesHelper::bytes($currentTotalMemory), $elapsedTime, $linesOfCode / $elapsedTime));
270270
$previousMemory = $currentTotalMemory;
271271
};

src/Command/AnalyserRunner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(
3737
* @param string[] $files
3838
* @param string[] $allAnalysedFiles
3939
* @param Closure(string $file): void|null $preFileCallback
40-
* @param Closure(int ): void|null $postFileCallback
40+
* @param Closure(int, list<string>=): void|null $postFileCallback
4141
*/
4242
public function runAnalyser(
4343
array $files,

0 commit comments

Comments
 (0)