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\Formatter\ConsoleFormatter; |
|
|
|
|
14
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\Formatter\FormatterFactory; |
|
|
|
|
15
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\Formatter\HtmlFormatter; |
|
|
|
|
16
|
|
|
use SavinMikhail\CommentsDensity\Baseline\Storage\TreePhpBaselineStorage; |
17
|
|
|
use SplFileInfo; |
18
|
|
|
use Symfony\Component\Console\Command\Command; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
|
22
|
|
|
final class AnalyzeCommentCommand extends Command |
23
|
|
|
{ |
24
|
|
|
public function __construct( |
25
|
|
|
private readonly ConfigLoader $configLoader = new ConfigLoader(), |
26
|
|
|
private readonly TreePhpBaselineStorage $storage = new TreePhpBaselineStorage(), |
27
|
|
|
private readonly AnalyzerFactory $analyzerFactory = new AnalyzerFactory(), |
28
|
|
|
?string $name = null, |
29
|
|
|
) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($name); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function configure(): void |
35
|
|
|
{ |
36
|
|
|
$this->setName('analyze') |
37
|
|
|
->setDescription('Analyzes the comment density in files within a directory.') |
38
|
|
|
->setHelp('This command allows you to analyze the comments in PHP files within a specified directory.'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
42
|
|
|
{ |
43
|
|
|
$path = $this->configLoader->getProjectRoot() . '/baseline.php'; |
44
|
|
|
|
45
|
|
|
$this->storage->init($path); |
46
|
|
|
|
47
|
|
|
$configDto = $this->configLoader->getConfigDto(); |
48
|
|
|
$files = $this->getFilesFromDirectories($configDto->directories); |
49
|
|
|
|
50
|
|
|
$formatters = ['console' => new ConsoleFormatter($output)]; |
51
|
|
|
if ($configDto->output->type === 'html') { |
52
|
|
|
$formatters['html'] = new HtmlFormatter($configDto->output->file); |
53
|
|
|
} |
54
|
|
|
$formatter = $formatters[$configDto->output->type] ?? $formatters['console']; |
55
|
|
|
|
56
|
|
|
$analyzer = $this->analyzerFactory->getAnalyzer($configDto, $this->storage); |
57
|
|
|
|
58
|
|
|
$report = $analyzer->analyze($files); |
59
|
|
|
|
60
|
|
|
$formatter->report($report); |
61
|
|
|
|
62
|
|
|
if ($report->exceedThreshold) { |
63
|
|
|
$output->writeln('<error>Comment thresholds were exceeded!</error>'); |
64
|
|
|
|
65
|
|
|
return Command::FAILURE; |
66
|
|
|
} |
67
|
|
|
$output->writeln('<info>Comment thresholds are passed!</info>'); |
68
|
|
|
|
69
|
|
|
return Command::SUCCESS; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string[] $directories |
74
|
|
|
* @return SplFileInfo[] |
75
|
|
|
*/ |
76
|
|
|
protected function getFilesFromDirectories(array $directories): iterable |
77
|
|
|
{ |
78
|
|
|
foreach ($directories as $directory) { |
79
|
|
|
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)); |
80
|
|
|
foreach ($iterator as $file) { |
81
|
|
|
yield $file; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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