-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRector.php
More file actions
80 lines (64 loc) · 2.18 KB
/
Rector.php
File metadata and controls
80 lines (64 loc) · 2.18 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
<?php
declare(strict_types=1);
namespace Spaceemotion\PhpCodingStandard\Tools;
use Spaceemotion\PhpCodingStandard\Context;
use Spaceemotion\PhpCodingStandard\DiffViolation;
use Spaceemotion\PhpCodingStandard\Formatter\File;
use Spaceemotion\PhpCodingStandard\Formatter\Result;
use function array_map;
use function basename;
use function implode;
use function preg_replace;
use function str_replace;
use function strtolower;
class Rector extends Tool
{
/** @var string */
protected $name = 'rector';
public function run(Context $context): bool
{
$binary = self::vendorBinary('rector');
$output = [];
if (
$this->execute($binary, array_merge([
'process',
'--output-format=json',
'--no-progress-bar',
$context->isFixing ? '' : '--dry-run',
], $context->files), $output) === 0
) {
return true;
}
$json = self::parseJson(end($output));
$result = new Result();
foreach ($json['file_diffs'] ?? [] as $diff) {
$file = new File();
$file->violations = DiffViolation::make($this, $diff['diff'], function (int $idx) use ($diff): string {
return $idx > 0
? '(contd.)'
: "Code issues:\n- " . implode(
"\n- ",
self::prettifyRectors($diff['applied_rectors'])
);
});
$result->files[$diff['file']] = $file;
}
$context->addResult($result);
return false;
}
/**
* @param string[] $applied_rectors
* @return string[]
*
* @psalm-return array<array-key, string>
*/
private static function prettifyRectors(array $applied_rectors): array
{
return array_map(static function (string $rector): string {
$className = basename(str_replace(['\\'], '/', $rector));
$withoutSuffix = preg_replace('/Rector$/', '', $className);
$name = strtolower(preg_replace('/(?<!^)[A-Z]/', ' $0', $withoutSuffix));
return $name . " <gray>({$rector})</gray>";
}, $applied_rectors);
}
}