-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixer-prio-graph.php
86 lines (70 loc) · 3.09 KB
/
fixer-prio-graph.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
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
<?php
use Fhaculty\Graph\Graph;
use Fhaculty\Graph\Vertex;
use Graphp\GraphViz\GraphViz;
use PhpCsFixer\Fixer\Comment\CommentToPhpdocFixer;
use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\Fixer\Phpdoc\AlignMultilineCommentFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocIndentFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocScalarFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocToCommentFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocTypesFixer;
use PhpCsFixer\FixerFactory;
use PhpCsFixer\Tests\AutoReview\FixerFactoryTest;
/** @var \Composer\Autoload\ClassLoader $loader */
$loader = require __DIR__ . '/vendor/autoload.php';
if (!$fixerFactoryFile = $loader->findFile(FixerFactory::class)) {
die('PHP-CS-Fixer not found!');
}
// Manually add php-cs-fixer's tests to the autoloader, as composer doesn't add
// autoload-dev. It also doesn't include the tests itself when using packagist,
// so we just use a git clone. (See composer.json)
$fixerTestDir = dirname($fixerFactoryFile, 2) . '/tests';
$loader->addPsr4('PhpCsFixer\\Tests\\', $fixerTestDir);
// Et voila, we can use a(n internal) test-class
$factory = new FixerFactoryTest();
/** @var Vertex[] $vertices */
$vertices = [];
$graph = new Graph();
// Left to right provides a more readable graph in our case than top-down
$graph->setAttribute('graphviz.graph.rankdir', 'LR');
// Ratio: height / width, stretch a little for readability
//$graph->setAttribute('graphviz.graph.ratio', 9 / 22); // Ultra-wide
$graph->setAttribute('graphviz.graph.ratio', 10 / 16); // Normal wide screen
$addPriorityCase = function (FixerInterface $higher, FixerInterface $lower) use ($graph, &$vertices) {
$nameHigher = $higher->getName();
$nameLower = $lower->getName();
if (!isset($vertices[$nameHigher])) {
$vertices[$nameHigher] = $graph->createVertex($nameHigher);
$vertices[$nameHigher]->setBalance($higher->getPriority());
}
if (!isset($vertices[$nameLower])) {
$vertices[$nameLower] = $graph->createVertex($nameLower);
$vertices[$nameLower]->setBalance($lower->getPriority());
}
if (!$vertices[$nameHigher]->hasEdgeTo($vertices[$nameLower])) {
$edge = $vertices[$nameHigher]->createEdgeTo($vertices[$nameLower]);
$edge->setAttribute('graphviz.color', 'grey');
}
};
/**
* @var FixerInterface $higher
* @var FixerInterface $lower
*/
foreach ($factory->provideFixersPriorityCases() as [$higher, $lower]) {
$addPriorityCase($higher, $lower);
}
foreach ($factory->provideFixersPrioritySpecialPhpdocCases() as [$higher, $lower]) {
if (
($higher instanceof AlignMultilineCommentFixer && !$lower instanceof CommentToPhpdocFixer)
|| ($higher instanceof CommentToPhpdocFixer && !$lower instanceof PhpdocToCommentFixer)
|| ($higher instanceof PhpdocToCommentFixer && !$lower instanceof PhpdocIndentFixer)
|| ($higher instanceof PhpdocIndentFixer && !$lower instanceof PhpdocTypesFixer)
|| ($higher instanceof PhpdocTypesFixer && !$lower instanceof PhpdocScalarFixer)
) {
continue;
}
$addPriorityCase($higher, $lower);
}
$graphViz = new GraphViz();
$graphViz->display($graph);