-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAnalyzeCommand.php
More file actions
147 lines (119 loc) · 4.88 KB
/
AnalyzeCommand.php
File metadata and controls
147 lines (119 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
declare(strict_types=1);
namespace Rector\Behastan\Command;
use Entropy\Console\Contract\CommandInterface;
use Entropy\Console\Enum\ExitCode;
use Entropy\Console\Output\OutputPrinter;
use Rector\Behastan\DefinitionPatternsExtractor;
use Rector\Behastan\Finder\BehatMetafilesFinder;
use Rector\Behastan\Reporting\PatternCollectionStatsPrinter;
use Rector\Behastan\RulesRegistry;
use Rector\Behastan\ValueObject\RuleError;
use Webmozart\Assert\Assert;
final readonly class AnalyzeCommand implements CommandInterface
{
public function __construct(
private DefinitionPatternsExtractor $definitionPatternsExtractor,
private PatternCollectionStatsPrinter $patternCollectionStatsPrinter,
private OutputPrinter $outputPrinter,
private RulesRegistry $rulesRegistry,
) {
}
/**
* @param string $projectDirectory Project directory (we find *.Context.php definition files and *.feature script files there)
* @param string[] $skips Skip a rule by identifier
*
* @return ExitCode::*
*/
public function run(?string $projectDirectory = null, array $skips = []): int
{
// fallback to current directory
if ($projectDirectory === null) {
$projectDirectory = getcwd();
Assert::string($projectDirectory);
}
Assert::directory($projectDirectory);
$contextFileInfos = BehatMetafilesFinder::findContextFiles([$projectDirectory]);
if ($contextFileInfos === []) {
$this->outputPrinter->redBackground(sprintf(
'No *.Context files found in "%s".%sPlease provide correct directory',
$projectDirectory,
PHP_EOL
));
return ExitCode::ERROR;
}
$featureFileInfos = BehatMetafilesFinder::findFeatureFiles([$projectDirectory]);
if ($featureFileInfos === []) {
$this->outputPrinter->redBackground(sprintf(
'No *.feature files found in "%s".%sPlease provide correct directory',
$projectDirectory,
PHP_EOL
));
return ExitCode::ERROR;
}
$this->outputPrinter->writeln(sprintf(
'<fg=green>Found %d Context and %d feature files</>',
count($contextFileInfos),
count($featureFileInfos)
));
$this->outputPrinter->writeln('<fg=yellow>Extracting definitions patterns...</>');
$patternCollection = $this->definitionPatternsExtractor->extract($contextFileInfos);
$this->outputPrinter->newLine();
$this->patternCollectionStatsPrinter->print($patternCollection);
$this->outputPrinter->newLine();
$this->outputPrinter->writeln('<fg=yellow>Running analysis...</>');
$this->outputPrinter->newLine();
/** @var RuleError[] $allRuleErrors */
$allRuleErrors = [];
foreach ($this->rulesRegistry->all() as $rule) {
if (in_array($rule->getIdentifier(), $skips, true)) {
$this->outputPrinter->writeln(sprintf('<fg=cyan>Skipping "%s" rule</>', $rule->getIdentifier()));
$this->outputPrinter->newLine();
continue;
}
$ruleErrors = $rule->process($contextFileInfos, $featureFileInfos, $patternCollection, $projectDirectory);
$allRuleErrors = array_merge($allRuleErrors, $ruleErrors);
}
if ($allRuleErrors === []) {
$this->outputPrinter->newLine(2);
$this->outputPrinter->greenBackground('No errors found. Good job!');
return ExitCode::SUCCESS;
}
$this->outputPrinter->newLine(2);
$i = 1;
foreach ($allRuleErrors as $allRuleError) {
$this->outputPrinter->writeln(
sprintf(
'<fg=yellow>%d) %s</> [id: <fg=cyan>%s</>]',
$i,
$allRuleError->getMessage(),
$allRuleError->getIdentifier()
),
1
);
foreach ($allRuleError->getLineFilePaths() as $lineFilePath) {
// compared to listing() this allow to make paths clickable in IDE
// use relative paths to getcwd()
$relativePath = str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $lineFilePath);
$this->outputPrinter->writeln($relativePath);
}
$this->outputPrinter->newLine(2);
++$i;
}
$this->outputPrinter->newLine();
$this->outputPrinter->redBackground(sprintf(
'Found %d error%s',
count($allRuleErrors),
count($allRuleErrors) > 1 ? 's' : ''
));
return ExitCode::ERROR;
}
public function getName(): string
{
return 'analyze';
}
public function getDescription(): string
{
return 'Run complete static analysis on Behat definitions and features';
}
}