1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Infrastructure\PhpCsFixer; |
4
|
|
|
|
5
|
|
|
use IgnoreFiles\IgnoreFiles; |
6
|
|
|
use PhpGitHooks\Application\Message\MessageConfigData; |
7
|
|
|
use PhpGitHooks\Command\BadJobLogo; |
8
|
|
|
use PhpGitHooks\Command\OutputHandlerInterface; |
9
|
|
|
use PhpGitHooks\Infrastructure\Common\InteractiveToolInterface; |
10
|
|
|
use PhpGitHooks\Infrastructure\Common\ToolHandler; |
11
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
12
|
|
|
|
13
|
|
|
class PhpCsFixerHandler extends ToolHandler implements InteractiveToolInterface, PhpCsFixerHandlerInterface |
14
|
|
|
{ |
15
|
|
|
/** @var array */ |
16
|
|
|
private $files; |
17
|
|
|
/** @var string */ |
18
|
|
|
private $filesToAnalyze; |
19
|
|
|
/** @var array */ |
20
|
|
|
private $levels = []; |
21
|
|
|
/** |
22
|
|
|
* @var IgnoreFiles |
23
|
|
|
*/ |
24
|
|
|
private $ignoreFiles; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* PhpCsFixerHandler constructor. |
28
|
|
|
* |
29
|
|
|
* @param OutputHandlerInterface $outputHandler |
30
|
|
|
* @param IgnoreFiles $ignoreFiles |
31
|
|
|
*/ |
32
|
|
|
public function __construct(OutputHandlerInterface $outputHandler, IgnoreFiles $ignoreFiles) |
33
|
|
|
{ |
34
|
|
|
parent::__construct($outputHandler); |
35
|
|
|
$this->ignoreFiles = $ignoreFiles; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @throws PhpCsFixerException |
40
|
|
|
*/ |
41
|
|
|
public function run(array $messages) |
42
|
|
|
{ |
43
|
|
|
foreach ($this->levels as $level => $value) { |
44
|
|
|
if (true === $value) { |
45
|
|
|
$this->outputHandler->setTitle('Checking '.strtoupper($level).' code style with PHP-CS-FIXER'); |
46
|
|
|
$this->output->write($this->outputHandler->getTitle()); |
47
|
|
|
|
48
|
|
|
$errors = array(); |
49
|
|
|
|
50
|
|
|
foreach ($this->files as $file) { |
51
|
|
|
$srcFile = preg_match($this->filesToAnalyze, $file); |
52
|
|
|
|
53
|
|
|
if (!$srcFile) { |
54
|
|
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
if (false === $this->ignoreFiles->isIgnored($file)) { |
|
|
|
|
58
|
|
|
$processBuilder = new ProcessBuilder( |
59
|
|
|
array( |
60
|
|
|
'php', |
61
|
|
|
'bin/php-cs-fixer', |
62
|
|
|
'--dry-run', |
63
|
|
|
'fix', |
64
|
|
|
$file, |
65
|
|
|
'--level='.$level, |
66
|
|
|
) |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$phpCsFixer = $processBuilder->getProcess(); |
70
|
|
|
$phpCsFixer->run(); |
71
|
|
|
|
72
|
|
|
if (false === $phpCsFixer->isSuccessful()) { |
73
|
|
|
$errors[] = $phpCsFixer->getOutput(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
if ($errors) { |
|
|
|
|
79
|
|
|
$this->outputHandler->setError($this->getErrors($errors)); |
80
|
|
|
$this->output->writeln($this->outputHandler->getError()); |
81
|
|
|
$this->output->writeln(BadJobLogo::paint($messages[MessageConfigData::KEY_ERROR_MESSAGE])); |
82
|
|
|
|
83
|
|
|
throw new PhpCsFixerException(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->output->writeln($this->outputHandler->getSuccessfulStepMessage()); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* throw new PhpCsFixerException(implode('', $errors)); |
93
|
|
|
* }. |
94
|
|
|
* |
95
|
|
|
* $this->output->writeln($this->outputHandler->getSuccessfulStepMessage()); |
96
|
|
|
* } |
97
|
|
|
* |
98
|
|
|
* /** |
99
|
|
|
* @param array $files |
100
|
|
|
*/ |
101
|
|
|
public function setFiles(array $files) |
102
|
|
|
{ |
103
|
|
|
$this->files = $files; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $filesToAnalyze |
108
|
|
|
*/ |
109
|
|
|
public function setFilesToAnalyze($filesToAnalyze) |
110
|
|
|
{ |
111
|
|
|
$this->filesToAnalyze = $filesToAnalyze; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param array $levels |
116
|
|
|
*/ |
117
|
|
|
public function setLevels(array $levels) |
118
|
|
|
{ |
119
|
|
|
$this->levels = $levels; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param array $errors |
124
|
|
|
* |
125
|
|
|
* @return null|string |
126
|
|
|
*/ |
127
|
|
|
private function getErrors(array $errors) |
128
|
|
|
{ |
129
|
|
|
$errorText = null; |
130
|
|
|
|
131
|
|
|
foreach ($errors as $error) { |
132
|
|
|
$errorText .= trim($error)."\n"; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $errorText; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.