Test Failed
Push — main ( 6d127d...41f629 )
by mikhail
13:20
created

Command::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\Commands;
6
7
use RecursiveDirectoryIterator;
8
use RecursiveIteratorIterator;
9
use SavinMikhail\CommentsDensity\CommentDensity;
10
use SavinMikhail\CommentsDensity\Comments\CommentFactory;
11
use SavinMikhail\CommentsDensity\DTO\Input\ConfigDTO;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDensity\DTO\Input\ConfigDTO was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use SavinMikhail\CommentsDensity\FileAnalyzer;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDensity\FileAnalyzer was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use SavinMikhail\CommentsDensity\Metrics\CDS;
14
use SavinMikhail\CommentsDensity\Metrics\ComToLoc;
15
use SavinMikhail\CommentsDensity\Metrics\Metrics;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDensity\Metrics\Metrics was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use SavinMikhail\CommentsDensity\Metrics\PerformanceMonitor;
17
use SavinMikhail\CommentsDensity\MissingDocBlockAnalyzer;
18
use SavinMikhail\CommentsDensity\Reporters\ReporterInterface;
19
use Symfony\Component\Console\Command\Command as SymfonyCommand;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Yaml\Parser;
22
23
use function array_map;
24
use function dirname;
25
26
use const DIRECTORY_SEPARATOR;
27
28
abstract class Command  extends SymfonyCommand
29
{
30
    protected const CONFIG_FILE = 'comments_density.yaml';
31
32
    protected function parseConfigFile(string $configFile): array
33
    {
34
        $yamlParser = new Parser();
35
        return $yamlParser->parseFile($configFile);
36
    }
37
38
    protected function getProjectRoot(): string
39
    {
40
        return dirname(__DIR__, 5);
41
    }
42
43
    protected function getConfig(): array
44
    {
45
        $configFile = $this->getProjectRoot() . DIRECTORY_SEPARATOR . self::CONFIG_FILE;
46
        return $this->parseConfigFile($configFile);
47
    }
48
49
    protected function getDirectories(ConfigDTO $config): array
50
    {
51
        return array_map(
52
            fn($dir) => $this->getProjectRoot() . '/' . $dir,
53
            $config->directories
54
        );
55
    }
56
57
    protected function getExcludes(array $config): array
58
    {
59
        return array_map(
60
            fn($dir) => $this->getProjectRoot() . '/' . $dir,
61
            $config['exclude']
62
        );
63
    }
64
65
    protected function getFilesFromDirectories(array $directories): array
66
    {
67
        $files = [];
68
        foreach ($directories as $directory) {
69
            $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
70
            foreach ($iterator as $file) {
71
                $files[] = $file;
72
            }
73
        }
74
        return $files;
75
    }
76
77
    protected function analyze(CommentDensity $analyzer, array $files, OutputInterface $output): int
78
    {
79
        $limitExceeded = $analyzer->analyzeFiles($files);
80
81
        if ($limitExceeded) {
82
            $output->writeln('<error>Comment thresholds were exceeded!</error>');
83
            return SymfonyCommand::FAILURE;
84
        }
85
        $output->writeln('<info>Comment thresholds are passed!</info>');
86
        return SymfonyCommand::SUCCESS;
87
    }
88
89
    protected function getConfigDto(): ConfigDTO
90
    {
91
        $config = $this->getConfig();
92
        $exclude = $this->getExcludes($config);
93
        $thresholds = $config['thresholds'];
94
        $outputConfig = $config['output'];
95
        $directories = $config['directories'];
96
97
        return new ConfigDTO(
98
            $thresholds,
99
            $exclude,
100
            $outputConfig,
101
            $directories,
102
        );
103
    }
104
105
    protected function getAnalyzer(
106
        ConfigDTO $configDto,
107
        OutputInterface $output,
108
        ReporterInterface $reporter
109
    ): CommentDensity {
110
        $commentFactory = new CommentFactory();
111
        $missingDocBlock = new MissingDocBlockAnalyzer();
112
        $cds = new CDS($configDto->thresholds, $commentFactory);
113
114
        $fileAnalyzer = new FileAnalyzer(
115
            $output,
116
            $missingDocBlock,
117
            $cds,
118
            $commentFactory
119
        );
120
121
        $metrics = new Metrics(
122
            $cds,
123
            new ComToLoc($configDto->thresholds),
124
            new PerformanceMonitor()
125
        );
126
127
        return new CommentDensity(
128
            $configDto,
129
            $commentFactory,
130
            $fileAnalyzer,
131
            $reporter,
132
            $missingDocBlock,
133
            $metrics
134
        );
135
    }
136
}
137