forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-rule-error-classes.php
executable file
·58 lines (47 loc) · 1.76 KB
/
generate-rule-error-classes.php
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
#!/usr/bin/env php
<?php declare(strict_types=1);
(function () {
require_once __DIR__ . '/../vendor/autoload.php';
$template = <<<'php'
<?php declare(strict_types = 1);
namespace PHPStan\Rules\RuleErrors;
/**
* @internal Use PHPStan\Rules\RuleErrorBuilder instead.
*/
class RuleError%s implements %s
{
%s
%s
}
php;
;
$ruleErrorTypes = \PHPStan\Rules\RuleErrorBuilder::getRuleErrorTypes();
$maxTypeNumber = array_sum(array_keys($ruleErrorTypes));
foreach (range(1, $maxTypeNumber) as $typeCombination) {
if (($typeCombination & 1) !== 1) {
continue;
}
$properties = [];
$interfaces = [];
foreach ($ruleErrorTypes as $typeNumber => [$interface, $propertyName, $nativePropertyType, $phpDocPropertyType]) {
if (($typeCombination & $typeNumber) === $typeNumber) {
$interfaces[] = '\\' . $interface;
if ($propertyName !== null && $nativePropertyType !== null && $phpDocPropertyType !== null) {
$properties[] = [$propertyName, $nativePropertyType, $phpDocPropertyType];
}
}
}
$phpClass = sprintf(
$template,
$typeCombination,
implode(', ', $interfaces),
implode("\n\n\t", array_map(function (array $property): string {
return sprintf("%spublic %s $%s;", $property[2] !== $property[1] ? sprintf("/** @var %s */\n\t", $property[2]) : '', $property[1], $property[0]);
}, $properties)),
implode("\n\n\t", array_map(function (array $property): string {
return sprintf("%spublic function get%s(): %s\n\t{\n\t\treturn \$this->%s;\n\t}", $property[2] !== $property[1] ? sprintf("/**\n\t * @return %s\n\t */\n\t", $property[2]) : '', ucfirst($property[0]), $property[1], $property[0]);
}, $properties))
);
file_put_contents(__DIR__ . '/../src/Rules/RuleErrors/RuleError' . $typeCombination . '.php', $phpClass);
}
})();