Skip to content

Commit f31de9d

Browse files
committed
add file finder
1 parent 2e020ce commit f31de9d

File tree

9 files changed

+93
-96
lines changed

9 files changed

+93
-96
lines changed

src/AnalyzeComments/Analyzer/Analyzer.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\CommentTypeFactory;
1212
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\Config;
1313
use SavinMikhail\CommentsDensity\AnalyzeComments\Exception\CommentsDensityException;
14+
use SavinMikhail\CommentsDensity\AnalyzeComments\File\CommentFinder;
15+
use SavinMikhail\CommentsDensity\AnalyzeComments\File\FileContentExtractor;
16+
use SavinMikhail\CommentsDensity\AnalyzeComments\File\FileFinder;
17+
use SavinMikhail\CommentsDensity\AnalyzeComments\File\FileTotalLinesCounter;
1418
use SavinMikhail\CommentsDensity\AnalyzeComments\Metrics\MetricsFacade;
1519
use SavinMikhail\CommentsDensity\Baseline\Storage\BaselineStorageInterface;
1620
use SplFileInfo;
@@ -28,25 +32,21 @@ public function __construct(
2832
) {}
2933

3034
/**
31-
* @param SplFileInfo[] $files
32-
* @throws CommentsDensityException|InvalidArgumentException
35+
* @throws CommentsDensityException
36+
* @throws InvalidArgumentException
3337
*/
34-
public function analyze(iterable $files): Report
38+
public function analyze(): Report
3539
{
3640
$this->metrics->startPerformanceMonitoring();
3741
$comments = [];
3842
$filesAnalyzed = 0;
3943
$totalLinesOfCode = 0;
40-
41-
foreach ($files as $file) {
42-
$contentExtractor = new FileContentExtractor($file, $this->configDTO);
43-
if ($contentExtractor->shouldSkipFile()) {
44-
continue;
45-
}
44+
foreach ((new FileFinder($this->configDTO))() as $file) {
4645
$commentFinder = new CommentFinder(
4746
$this->commentFactory,
4847
$this->configDTO,
4948
);
49+
$contentExtractor = new FileContentExtractor($file, $this->configDTO);
5050

5151
$fileComments = $this->cache->get(
5252
$this->getCacheKey($file),

src/AnalyzeComments/Analyzer/FileContentExtractor.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/AnalyzeComments/Commands/AnalyzeCommentCommand.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44

55
namespace SavinMikhail\CommentsDensity\AnalyzeComments\Commands;
66

7-
use RecursiveDirectoryIterator;
8-
use RecursiveIteratorIterator;
97
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\AnalyzerFactory;
108
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\ConfigLoader;
119
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\HtmlOutputDTO;
1210
use SavinMikhail\CommentsDensity\AnalyzeComments\Formatter\ConsoleFormatter;
1311
use SavinMikhail\CommentsDensity\AnalyzeComments\Formatter\HtmlFormatter;
1412
use SavinMikhail\CommentsDensity\Baseline\Storage\TreePhpBaselineStorage;
15-
use SplFileInfo;
1613
use Symfony\Component\Console\Command\Command;
1714
use Symfony\Component\Console\Input\InputInterface;
1815
use Symfony\Component\Console\Output\OutputInterface;
@@ -42,7 +39,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4239
$this->storage->init($path);
4340

4441
$configDto = $this->configLoader->getConfigDto();
45-
$files = $this->getFilesFromDirectories($configDto->directories);
4642

4743
$formatters = ['console' => new ConsoleFormatter($output)];
4844
if ($configDto->output instanceof HtmlOutputDTO) {
@@ -52,7 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5248

5349
$analyzer = $this->analyzerFactory->getAnalyzer($configDto, $this->storage);
5450

55-
$report = $analyzer->analyze($files);
51+
$report = $analyzer->analyze();
5652

5753
$formatter->report($report);
5854

@@ -65,18 +61,4 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6561

6662
return Command::SUCCESS;
6763
}
68-
69-
/**
70-
* @param string[] $directories
71-
* @return SplFileInfo[]
72-
*/
73-
protected function getFilesFromDirectories(array $directories): iterable
74-
{
75-
foreach ($directories as $directory) {
76-
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
77-
foreach ($iterator as $file) {
78-
yield $file;
79-
}
80-
}
81-
}
8264
}

src/AnalyzeComments/Analyzer/CommentFinder.php renamed to src/AnalyzeComments/File/CommentFinder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer;
5+
namespace SavinMikhail\CommentsDensity\AnalyzeComments\File;
66

77
use PhpParser\NodeTraverser;
88
use PhpParser\Parser;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SavinMikhail\CommentsDensity\AnalyzeComments\File;
6+
7+
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\Config;
8+
use SplFileInfo;
9+
10+
final readonly class FileContentExtractor
11+
{
12+
public function __construct(
13+
private SplFileInfo $file,
14+
private Config $configDTO,
15+
) {}
16+
17+
public function getContent(): string
18+
{
19+
return file_get_contents($this->file->getRealPath());
20+
}
21+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SavinMikhail\CommentsDensity\AnalyzeComments\File;
6+
7+
use RecursiveDirectoryIterator;
8+
use RecursiveIteratorIterator;
9+
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\Config;
10+
use SplFileInfo;
11+
12+
final readonly class FileFinder
13+
{
14+
public function __construct(
15+
private Config $config,
16+
) {}
17+
18+
/**
19+
* @return SplFileInfo[]
20+
*/
21+
public function __invoke(): iterable
22+
{
23+
foreach ($this->config->directories as $directory) {
24+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
25+
foreach ($iterator as $file) {
26+
if ($this->shouldSkipFile($file)) {
27+
continue;
28+
}
29+
yield $file;
30+
}
31+
}
32+
}
33+
34+
private function shouldSkipFile(SplFileInfo $file): bool
35+
{
36+
return
37+
$this->isInWhitelist($file->getRealPath())
38+
|| $file->getSize() === 0
39+
|| !$this->isPhpFile($file)
40+
|| !$file->isReadable();
41+
}
42+
43+
private function isPhpFile(SplFileInfo $file): bool
44+
{
45+
return $file->isFile() && $file->getExtension() === 'php';
46+
}
47+
48+
private function isInWhitelist(string $filePath): bool
49+
{
50+
foreach ($this->config->exclude as $whitelistedDir) {
51+
if (str_contains($filePath, $whitelistedDir)) {
52+
return true;
53+
}
54+
}
55+
56+
return false;
57+
}
58+
}

src/AnalyzeComments/Analyzer/FileTotalLinesCounter.php renamed to src/AnalyzeComments/File/FileTotalLinesCounter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer;
5+
namespace SavinMikhail\CommentsDensity\AnalyzeComments\File;
66

77
use SplFileInfo;
88

src/Baseline/Commands/BaselineCommand.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44

55
namespace SavinMikhail\CommentsDensity\Baseline\Commands;
66

7-
use RecursiveDirectoryIterator;
8-
use RecursiveIteratorIterator;
97
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\AnalyzerFactory;
108
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\ConfigLoader;
119
use SavinMikhail\CommentsDensity\Baseline\Storage\TreePhpBaselineStorage;
12-
use SplFileInfo;
1310
use Symfony\Component\Console\Command\Command;
1411
use Symfony\Component\Console\Input\InputInterface;
1512
use Symfony\Component\Console\Output\OutputInterface;
@@ -39,29 +36,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3936
$this->storage->init($path);
4037

4138
$configDto = $this->configLoader->getConfigDto();
42-
$files = $this->getFilesFromDirectories($configDto->directories);
4339

4440
$analyzer = $this->analyzerFactory->getAnalyzer($configDto, $this->storage);
45-
$report = $analyzer->analyze($files);
41+
$report = $analyzer->analyze();
4642

4743
$this->storage->setComments($report->comments); // todo create some baseline reporter
4844

4945
$output->writeln('<info>Baseline generated successfully!</info>');
5046

5147
return Command::SUCCESS;
5248
}
53-
54-
/**
55-
* @param string[] $directories
56-
* @return SplFileInfo[]
57-
*/
58-
protected function getFilesFromDirectories(array $directories): iterable
59-
{
60-
foreach ($directories as $directory) {
61-
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
62-
foreach ($iterator as $file) {
63-
yield $file;
64-
}
65-
}
66-
}
6749
}

tests/MissingDocblock/MissingDocBlockAnalyzerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
use Generator;
88
use PHPUnit\Framework\Attributes\DataProvider;
99
use PHPUnit\Framework\TestCase;
10-
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\CommentFinder;
1110
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\CommentDTO;
1211
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\CommentTypeFactory;
1312
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\MissingDocBlock;
1413
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\Config;
1514
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\ConsoleOutputDTO;
1615
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\MissingDocblockConfigDTO;
16+
use SavinMikhail\CommentsDensity\AnalyzeComments\File\CommentFinder;
1717

1818
final class MissingDocBlockAnalyzerTest extends TestCase
1919
{

0 commit comments

Comments
 (0)