|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Module\PhpCsFixer\Service; |
|
4
|
|
|
|
|
5
|
|
|
use PhpGitHooks\Module\Git\Contract\Response\BadJobLogoResponse; |
|
6
|
|
|
use PhpGitHooks\Module\Git\Service\PreCommitOutputWriter; |
|
7
|
|
|
use PhpGitHooks\Module\PhpCsFixer\Contract\Exception\PhpCsFixerViolationsException; |
|
8
|
|
|
use PhpGitHooks\Module\PhpCsFixer\Model\PhpCsFixerToolProcessorInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
class PhpCsFixerTool |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var OutputInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
private $output; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var PhpCsFixerToolProcessorInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $phpCsFixerToolProcessor; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* PhpCsFixerTool constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param OutputInterface $output |
|
26
|
|
|
* @param PhpCsFixerToolProcessorInterface $phpCsFixerToolProcessor |
|
27
|
|
|
*/ |
|
28
|
2 |
|
public function __construct(OutputInterface $output, PhpCsFixerToolProcessorInterface $phpCsFixerToolProcessor) |
|
29
|
|
|
{ |
|
30
|
2 |
|
$this->output = $output; |
|
31
|
2 |
|
$this->phpCsFixerToolProcessor = $phpCsFixerToolProcessor; |
|
32
|
2 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param array $files |
|
36
|
|
|
* @param bool $psr0 |
|
37
|
|
|
* @param bool $psr1 |
|
38
|
|
|
* @param bool $psr2 |
|
39
|
|
|
* @param bool $symfony |
|
40
|
|
|
* @param string $errorMessage |
|
41
|
|
|
*/ |
|
42
|
2 |
|
public function execute(array $files, $psr0, $psr1, $psr2, $symfony, $errorMessage) |
|
43
|
|
|
{ |
|
44
|
2 |
|
if (true === $psr0) { |
|
45
|
2 |
|
$this->executeTool($files, 'PSR0', $errorMessage); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
if (true === $psr1) { |
|
49
|
1 |
|
$this->executeTool($files, 'PSR1', $errorMessage); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
if (true === $psr2) { |
|
53
|
1 |
|
$this->executeTool($files, 'PSR2', $errorMessage); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
if (true === $symfony) { |
|
57
|
1 |
|
$this->executeTool($files, 'SYMFONY', $errorMessage); |
|
58
|
|
|
} |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param array $files |
|
63
|
|
|
* @param string $level |
|
64
|
|
|
* @param string $errorMessage |
|
65
|
|
|
* |
|
66
|
|
|
* @throws PhpCsFixerViolationsException |
|
67
|
|
|
*/ |
|
68
|
2 |
|
private function executeTool(array $files, $level, $errorMessage) |
|
69
|
|
|
{ |
|
70
|
2 |
|
$outputMessage = new PreCommitOutputWriter(sprintf('Checking %s code style with PHP-CS-FIXER', $level)); |
|
71
|
2 |
|
$this->output->write($outputMessage->getMessage()); |
|
72
|
|
|
|
|
73
|
2 |
|
$errors = []; |
|
74
|
2 |
|
foreach ($files as $file) { |
|
75
|
2 |
|
$errors[] = $this->phpCsFixerToolProcessor->process($file, $level); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
$errors = array_filter($errors); |
|
79
|
|
|
|
|
80
|
2 |
|
if (!empty($errors)) { |
|
81
|
1 |
|
$this->output->writeln($outputMessage->getFailMessage()); |
|
82
|
1 |
|
$errorsText = $outputMessage->setError(implode('', $errors)); |
|
83
|
1 |
|
$this->output->writeln($errorsText); |
|
84
|
1 |
|
$this->output->writeln(BadJobLogoResponse::paint($errorMessage)); |
|
85
|
1 |
|
throw new PhpCsFixerViolationsException(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
$this->output->writeln($outputMessage->getSuccessfulMessage()); |
|
89
|
1 |
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|