|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Symplify |
|
7
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Symplify\MultiCodingStandard\PhpCsFixer\Factory; |
|
11
|
|
|
|
|
12
|
|
|
use PhpCsFixer\Config; |
|
13
|
|
|
use PhpCsFixer\Console\ConfigurationResolver; |
|
14
|
|
|
use PhpCsFixer\Fixer\FixerInterface; |
|
15
|
|
|
|
|
16
|
|
|
final class FixerFactory |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @return FixerInterface[] |
|
20
|
|
|
*/ |
|
21
|
7 |
|
public function createFromLevelsFixersAndExcludedFixers(array $fixerLevels, array $fixers, array $excludedFixers) |
|
22
|
|
|
{ |
|
23
|
7 |
|
$fixersFromLevels = $this->createFromLevelsAndExcludedFixers($fixerLevels, $excludedFixers); |
|
24
|
7 |
|
$standaloneFixers = $this->createFromFixers($fixers); |
|
25
|
|
|
|
|
26
|
7 |
|
return array_merge($fixersFromLevels, $standaloneFixers); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
7 |
|
private function createFromLevelsAndExcludedFixers(array $fixerLevels, array $excludedFixers) : array |
|
30
|
|
|
{ |
|
31
|
7 |
|
if (!count($fixerLevels)) { |
|
32
|
2 |
|
return []; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
5 |
|
$fixers = []; |
|
36
|
5 |
|
foreach ($fixerLevels as $fixerLevel) { |
|
37
|
5 |
|
$excludedFixersAsString = $this->turnExcludedFixersToString($excludedFixers); |
|
38
|
5 |
|
$newFixers = $this->resolveFixersForLevelsAndFixers($fixerLevel, $excludedFixersAsString); |
|
39
|
|
|
|
|
40
|
5 |
|
$fixers = array_merge($fixers, $newFixers); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
5 |
|
return $fixers; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
7 |
|
private function createFromFixers(array $fixers) : array |
|
47
|
|
|
{ |
|
48
|
7 |
|
if (!count($fixers)) { |
|
49
|
6 |
|
return []; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
$fixersAsString = $this->turnFixersToString($fixers); |
|
53
|
|
|
|
|
54
|
1 |
|
return $this->resolveFixersForLevelsAndFixers('none', $fixersAsString); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return FixerInterface[] |
|
59
|
|
|
*/ |
|
60
|
6 |
|
private function resolveFixersForLevelsAndFixers(string $level, string $fixersAsString) : array |
|
61
|
|
|
{ |
|
62
|
6 |
|
$config = new Config(); |
|
63
|
6 |
|
$configurationResolver = new ConfigurationResolver($config, [ |
|
64
|
6 |
|
'rules' => $this->combineSetAndFixersToRules($level, $fixersAsString) |
|
65
|
|
|
], getcwd()); |
|
66
|
|
|
|
|
67
|
6 |
|
return $configurationResolver->getFixers(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
private function turnFixersToString(array $fixers) : string |
|
71
|
|
|
{ |
|
72
|
1 |
|
return $this->implodeWithPresign($fixers); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
5 |
|
private function turnExcludedFixersToString(array $excludedFixers) : string |
|
76
|
|
|
{ |
|
77
|
5 |
|
return $this->implodeWithPresign($excludedFixers, '-'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
6 |
|
private function implodeWithPresign(array $items, string $presign = '') |
|
81
|
|
|
{ |
|
82
|
6 |
|
if (count($items)) { |
|
83
|
3 |
|
return $presign.implode(','.$presign, $items); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
return ''; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
6 |
|
private function combineSetAndFixersToRules(string $level, string $fixersAsString) : string |
|
90
|
|
|
{ |
|
91
|
6 |
|
$rules = ''; |
|
92
|
6 |
|
if ($level && $level !== 'none') { |
|
93
|
5 |
|
$rules .= '@' . strtoupper($level); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
6 |
|
if ($fixersAsString) { |
|
97
|
3 |
|
if ($rules) { |
|
98
|
2 |
|
$rules .= ','; |
|
99
|
|
|
} |
|
100
|
3 |
|
$rules .= $fixersAsString; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
6 |
|
return $rules; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|