-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathDebugExpression.php
More file actions
146 lines (121 loc) · 4.64 KB
/
DebugExpression.php
File metadata and controls
146 lines (121 loc) · 4.64 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
<?php
declare(strict_types=1);
namespace Arkitect\CLI\Command;
use Arkitect\Analyzer\FileParserFactory;
use Arkitect\Analyzer\ParsingError;
use Arkitect\Analyzer\ParsingErrors;
use Arkitect\ClassSet;
use Arkitect\CLI\TargetPhpVersion;
use Arkitect\Rules\Violations;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class DebugExpression extends Command
{
/** @var string|null */
public static $defaultDescription = <<< 'EOT'
Check which classes respect an expression
EOT;
/** @var string */
public static $help = <<< 'EOT'
Check which classes respect an expression
EOT;
public function __construct()
{
parent::__construct('debug:expression');
}
protected function configure(): void
{
$this
->setHelp(self::$help)
->addArgument('expression', InputArgument::REQUIRED)
->addArgument('arguments', InputArgument::IS_ARRAY)
->addOption(
'from-dir',
'd',
InputOption::VALUE_REQUIRED,
'The folder in which to search the classes',
'.'
)
->addOption(
'target-php-version',
't',
InputOption::VALUE_OPTIONAL,
'Target php version to use for parsing'
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$fileParser = $this->getParser($input);
$classSet = ClassSet::fromDir($input->getOption('from-dir'));
$ruleName = $input->getArgument('expression');
/** @var class-string $ruleFQCN */
$ruleFQCN = 'Arkitect\Expression\ForClasses\\'.$ruleName;
$arguments = $input->getArgument('arguments');
$argumentError = $this->getArgumentsError($arguments, $ruleName, $ruleFQCN);
if (null !== $argumentError) {
$output->writeln($argumentError);
return 2;
}
$rule = new $ruleFQCN(...$arguments);
foreach ($classSet as $file) {
$result = $fileParser->parse($file->getContents(), $file->getRelativePathname());
$this->showParsingErrors($result->parsingErrors(), $output);
foreach ($result->classDescriptions() as $classDescription) {
$violations = new Violations();
$rule->evaluate($classDescription, $violations, '');
if (0 === $violations->count()) {
$output->writeln($classDescription->getFQCN());
}
}
}
return 0;
}
/**
* @throws \Arkitect\Exceptions\PhpVersionNotValidException
*/
private function getParser(InputInterface $input): \Arkitect\Analyzer\Parser
{
$phpVersion = $input->getOption('target-php-version');
$targetPhpVersion = TargetPhpVersion::create($phpVersion);
$fileParser = FileParserFactory::createFileParser($targetPhpVersion, true, null);
return $fileParser;
}
private function showParsingErrors(ParsingErrors $parsingErrors, OutputInterface $output): void
{
if (\count($parsingErrors) > 0) {
$output->writeln('WARNING: Some files could not be parsed for these errors:');
/** @var ParsingError $parsedError */
foreach ($parsingErrors as $parsedError) {
$output->writeln(' - '.$parsedError);
}
$output->writeln('');
}
}
private function getArgumentsError(array $arguments, string $ruleName, string $ruleFQCN): ?string
{
try {
/** @var class-string $ruleFQCN */
$expressionReflection = new \ReflectionClass($ruleFQCN);
} catch (\ReflectionException $exception) {
return "Error: Expression '$ruleName' not found.";
}
$constructorReflection = $expressionReflection->getConstructor();
if (null === $constructorReflection) {
$maxNumberOfArguments = 0;
$minNumberOfArguments = 0;
} else {
$maxNumberOfArguments = $constructorReflection->getNumberOfParameters();
$minNumberOfArguments = $constructorReflection->getNumberOfRequiredParameters();
}
if (\count($arguments) < $minNumberOfArguments) {
return "Error: Too few arguments for '$ruleName'.";
}
if (\count($arguments) > $maxNumberOfArguments) {
return "Error: Too many arguments for '$ruleName'.";
}
return null;
}
}