-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdoc
More file actions
executable file
·89 lines (74 loc) · 3.05 KB
/
doc
File metadata and controls
executable file
·89 lines (74 loc) · 3.05 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
#!/usr/bin/env php
<?php
declare(strict_types=1);
use PedroTroller\CS\Fixer\AbstractFixer;
use PedroTroller\CS\Fixer\Fixers;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionInterface;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\Tokenizer\Tokens;
use SebastianBergmann\Diff\Differ;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
include sprintf('%s/../vendor/autoload.php', __DIR__);
include 'Utils.php';
$fixers = array_map(static function (AbstractFixer $fixer) {
$samples = $fixer->getDefinition()->getCodeSamples();
return [
'name' => $fixer->getName(),
'doc' => [
'summary' => $fixer->getDefinition()->getSummary(),
],
'deprecated' => $fixer->isDeprecated(),
'replacement' => $fixer->getDeprecationReplacement(),
'options' => array_map(
static fn (FixerOptionInterface $option) => [
'name' => $option->getName(),
'description' => $option->getDescription(),
'required' => false === $option->hasDefault(),
'allowedValues' => $option->getAllowedValues(),
'allowedTypes' => $option->getAllowedTypes(),
'defaultValue' => $option->getDefault(),
],
$fixer instanceof ConfigurableFixerInterface
? $fixer->getConfigurationDefinition()->getOptions()
: []
),
'samples' => array_map(static function (CodeSample $sample) use ($fixer) {
if ($fixer instanceof ConfigurableFixerInterface) {
$fixer->configure($sample->getConfiguration());
}
$tokens = Tokens::fromCode($fixer->getSampleCode());
$differ = new Differ();
if ($fixer->isCandidate($tokens)) {
$fixer->fix(new SplFileInfo(__FILE__), $tokens);
$diff = explode("\n", $differ->diff($fixer->getSampleCode(), $tokens->generateCode()));
foreach ($diff as $num => $line) {
if (strlen($line) > 80 + 1) {
continue;
}
while (strlen($line) < 80 + 1) {
$line .= ' ';
}
if (0 === $num) {
$line .= '// 80 chars';
} else {
$line .= '//';
}
$diff[$num] = $line;
}
} else {
$diff = ['+ Fixing not supported by your PHP version.'];
}
return [
'diff' => implode("\n", $diff),
'configuration' => $sample->getConfiguration()
? Utils::arrayToString($sample->getConfiguration())
: [],
];
}, $samples),
];
}, [...(new Fixers())]);
$loader = new FilesystemLoader([__DIR__]);
$twig = new Environment($loader);
echo $twig->render('doc.twig', ['fixers' => json_decode(json_encode($fixers))]);