|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SavinMikhail\CommentsDensity\AnalyzeComments\Commands; |
|
6
|
|
|
|
|
7
|
|
|
use RecursiveDirectoryIterator; |
|
8
|
|
|
use RecursiveIteratorIterator; |
|
9
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\AnalyzerFactory; |
|
|
|
|
|
|
10
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\ConfigLoader; |
|
|
|
|
|
|
11
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\ConfigDTO; |
|
|
|
|
|
|
12
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\Exception\CommentsDensityException; |
|
13
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\Reporters\ReporterFactory; |
|
|
|
|
|
|
14
|
|
|
use SavinMikhail\CommentsDensity\Baseline\Storage\TreePhpBaselineStorage; |
|
15
|
|
|
use SplFileInfo; |
|
16
|
|
|
use Symfony\Component\Console\Command\Command; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
|
|
20
|
|
|
final class AnalyzeCommentCommand extends Command |
|
21
|
|
|
{ |
|
22
|
|
|
protected function configure(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$this->setName('analyze:comments') |
|
25
|
|
|
->setDescription('Analyzes the comment density in files within a directory.') |
|
26
|
|
|
->setHelp('This command allows you to analyze the comments in PHP files within a specified directory.'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
30
|
|
|
{ |
|
31
|
|
|
$configLoader = new ConfigLoader(); |
|
32
|
|
|
$path = $configLoader->getProjectRoot() . '/baseline.php'; |
|
33
|
|
|
|
|
34
|
|
|
$storage = new TreePhpBaselineStorage(); |
|
35
|
|
|
$storage->init($path); |
|
36
|
|
|
|
|
37
|
|
|
$configDto = $configLoader->getConfigDto(); |
|
38
|
|
|
$files = $this->getFilesFromDirectories($configDto->directories); |
|
39
|
|
|
|
|
40
|
|
|
$reporter = (new ReporterFactory())->createReporter($output, $configDto); |
|
41
|
|
|
$analyzerFactory = new AnalyzerFactory(); |
|
42
|
|
|
|
|
43
|
|
|
$analyzer = $analyzerFactory->getAnalyzer($configDto, $output, $storage); |
|
44
|
|
|
|
|
45
|
|
|
$outputDTO = $analyzer->analyze($files); |
|
46
|
|
|
|
|
47
|
|
|
$reporter->report($outputDTO); |
|
48
|
|
|
|
|
49
|
|
|
if ($outputDTO->exceedThreshold) { |
|
50
|
|
|
$output->writeln('<error>Comment thresholds were exceeded!</error>'); |
|
51
|
|
|
|
|
52
|
|
|
return Command::FAILURE; |
|
53
|
|
|
} |
|
54
|
|
|
$output->writeln('<info>Comment thresholds are passed!</info>'); |
|
55
|
|
|
|
|
56
|
|
|
return Command::SUCCESS; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string[] $directories |
|
61
|
|
|
* @return SplFileInfo[] |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function getFilesFromDirectories(array $directories): iterable |
|
64
|
|
|
{ |
|
65
|
|
|
foreach ($directories as $directory) { |
|
66
|
|
|
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)); |
|
67
|
|
|
foreach ($iterator as $file) { |
|
68
|
|
|
yield $file; |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths