Skip to content

Commit

Permalink
add file finder
Browse files Browse the repository at this point in the history
  • Loading branch information
savinmikhail committed Jan 4, 2025
1 parent 2e020ce commit f31de9d
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 96 deletions.
18 changes: 9 additions & 9 deletions src/AnalyzeComments/Analyzer/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\CommentTypeFactory;
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\Config;
use SavinMikhail\CommentsDensity\AnalyzeComments\Exception\CommentsDensityException;
use SavinMikhail\CommentsDensity\AnalyzeComments\File\CommentFinder;
use SavinMikhail\CommentsDensity\AnalyzeComments\File\FileContentExtractor;
use SavinMikhail\CommentsDensity\AnalyzeComments\File\FileFinder;
use SavinMikhail\CommentsDensity\AnalyzeComments\File\FileTotalLinesCounter;
use SavinMikhail\CommentsDensity\AnalyzeComments\Metrics\MetricsFacade;
use SavinMikhail\CommentsDensity\Baseline\Storage\BaselineStorageInterface;
use SplFileInfo;
Expand All @@ -28,25 +32,21 @@ public function __construct(
) {}

/**
* @param SplFileInfo[] $files
* @throws CommentsDensityException|InvalidArgumentException
* @throws CommentsDensityException
* @throws InvalidArgumentException
*/
public function analyze(iterable $files): Report
public function analyze(): Report
{
$this->metrics->startPerformanceMonitoring();
$comments = [];
$filesAnalyzed = 0;
$totalLinesOfCode = 0;

foreach ($files as $file) {
$contentExtractor = new FileContentExtractor($file, $this->configDTO);
if ($contentExtractor->shouldSkipFile()) {
continue;
}
foreach ((new FileFinder($this->configDTO))() as $file) {
$commentFinder = new CommentFinder(
$this->commentFactory,
$this->configDTO,
);
$contentExtractor = new FileContentExtractor($file, $this->configDTO);

$fileComments = $this->cache->get(
$this->getCacheKey($file),
Expand Down
46 changes: 0 additions & 46 deletions src/AnalyzeComments/Analyzer/FileContentExtractor.php

This file was deleted.

20 changes: 1 addition & 19 deletions src/AnalyzeComments/Commands/AnalyzeCommentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@

namespace SavinMikhail\CommentsDensity\AnalyzeComments\Commands;

use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\AnalyzerFactory;
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\ConfigLoader;
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\HtmlOutputDTO;
use SavinMikhail\CommentsDensity\AnalyzeComments\Formatter\ConsoleFormatter;
use SavinMikhail\CommentsDensity\AnalyzeComments\Formatter\HtmlFormatter;
use SavinMikhail\CommentsDensity\Baseline\Storage\TreePhpBaselineStorage;
use SplFileInfo;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -42,7 +39,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->storage->init($path);

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

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

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

$report = $analyzer->analyze($files);
$report = $analyzer->analyze();

$formatter->report($report);

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

return Command::SUCCESS;
}

/**
* @param string[] $directories
* @return SplFileInfo[]
*/
protected function getFilesFromDirectories(array $directories): iterable
{
foreach ($directories as $directory) {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
foreach ($iterator as $file) {
yield $file;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer;
namespace SavinMikhail\CommentsDensity\AnalyzeComments\File;

use PhpParser\NodeTraverser;
use PhpParser\Parser;
Expand Down
21 changes: 21 additions & 0 deletions src/AnalyzeComments/File/FileContentExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace SavinMikhail\CommentsDensity\AnalyzeComments\File;

use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\Config;
use SplFileInfo;

final readonly class FileContentExtractor
{
public function __construct(
private SplFileInfo $file,
private Config $configDTO,
) {}

public function getContent(): string
{
return file_get_contents($this->file->getRealPath());
}
}
58 changes: 58 additions & 0 deletions src/AnalyzeComments/File/FileFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace SavinMikhail\CommentsDensity\AnalyzeComments\File;

use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\Config;
use SplFileInfo;

final readonly class FileFinder
{
public function __construct(
private Config $config,
) {}

/**
* @return SplFileInfo[]
*/
public function __invoke(): iterable
{
foreach ($this->config->directories as $directory) {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
foreach ($iterator as $file) {
if ($this->shouldSkipFile($file)) {
continue;
}
yield $file;
}
}
}

private function shouldSkipFile(SplFileInfo $file): bool
{
return
$this->isInWhitelist($file->getRealPath())
|| $file->getSize() === 0
|| !$this->isPhpFile($file)
|| !$file->isReadable();
}

private function isPhpFile(SplFileInfo $file): bool
{
return $file->isFile() && $file->getExtension() === 'php';
}

private function isInWhitelist(string $filePath): bool
{
foreach ($this->config->exclude as $whitelistedDir) {
if (str_contains($filePath, $whitelistedDir)) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer;
namespace SavinMikhail\CommentsDensity\AnalyzeComments\File;

use SplFileInfo;

Expand Down
20 changes: 1 addition & 19 deletions src/Baseline/Commands/BaselineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

namespace SavinMikhail\CommentsDensity\Baseline\Commands;

use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\AnalyzerFactory;
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\ConfigLoader;
use SavinMikhail\CommentsDensity\Baseline\Storage\TreePhpBaselineStorage;
use SplFileInfo;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -39,29 +36,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->storage->init($path);

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

$analyzer = $this->analyzerFactory->getAnalyzer($configDto, $this->storage);
$report = $analyzer->analyze($files);
$report = $analyzer->analyze();

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

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

return Command::SUCCESS;
}

/**
* @param string[] $directories
* @return SplFileInfo[]
*/
protected function getFilesFromDirectories(array $directories): iterable
{
foreach ($directories as $directory) {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
foreach ($iterator as $file) {
yield $file;
}
}
}
}
2 changes: 1 addition & 1 deletion tests/MissingDocblock/MissingDocBlockAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\CommentFinder;
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\CommentDTO;
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\CommentTypeFactory;
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\MissingDocBlock;
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\Config;
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\ConsoleOutputDTO;
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\MissingDocblockConfigDTO;
use SavinMikhail\CommentsDensity\AnalyzeComments\File\CommentFinder;

final class MissingDocBlockAnalyzerTest extends TestCase
{
Expand Down

0 comments on commit f31de9d

Please sign in to comment.