1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace uuf6429\ElderBrother\Action; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
use uuf6429\ElderBrother\Change\FileList; |
9
|
|
|
|
10
|
|
|
class PhpCsFixer extends ActionAbstract |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var FileList |
14
|
|
|
*/ |
15
|
|
|
protected $files; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string|null |
19
|
|
|
*/ |
20
|
|
|
protected $binFile; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string|null |
24
|
|
|
*/ |
25
|
|
|
protected $configFile; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
protected $addAutomatically; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Runs all the provided files through PHP-CS-Fixer, fixing any code style issues. |
34
|
|
|
* |
35
|
|
|
* @param FileList $files The files to check |
36
|
|
|
* @param string|null $binFile (Optional, default is from vendor) File path to PHP-CS-Fixer binary |
37
|
|
|
* @param string|null $configFile (Optional, default is project root) File path to PHP-CS-Fixer config |
38
|
|
|
* @param bool $addAutomatically (Optional, default is true) Whether to add modified files to commit or not |
39
|
|
|
*/ |
40
|
3 |
|
public function __construct(FileList $files, $binFile = null, $configFile = null, $addAutomatically = true) |
41
|
|
|
{ |
42
|
3 |
|
$this->files = $files; |
43
|
3 |
|
$this->binFile = $binFile ?: $this->getBinFile(); |
44
|
3 |
|
$this->configFile = $configFile; |
45
|
3 |
|
$this->addAutomatically = $addAutomatically; |
46
|
3 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function getName() |
52
|
|
|
{ |
53
|
|
|
return 'PHP Code Style Fixer (PhpCsFixer)'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function checkSupport() |
60
|
|
|
{ |
61
|
|
|
return file_exists($this->binFile); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
3 |
|
public function execute(InputInterface $input, OutputInterface $output) |
68
|
|
|
{ |
69
|
3 |
|
$files = $this->files->toArray(); |
70
|
|
|
|
71
|
3 |
|
$progress = $this->createProgressBar($input, $output, count($files)); |
72
|
3 |
|
$progress->start(); |
73
|
|
|
|
74
|
3 |
|
$failed = []; |
75
|
|
|
|
76
|
3 |
|
foreach ($files as $file) { |
77
|
2 |
|
$progress->setMessage('Processing ' . $file . '...'); |
78
|
|
|
|
79
|
2 |
|
$process = new Process( |
80
|
|
|
sprintf( |
81
|
2 |
|
'php -f %s -- fix %s %s', |
82
|
2 |
|
escapeshellarg($this->binFile), |
83
|
|
|
escapeshellarg($file), |
84
|
2 |
|
$this->configFile ? ('--config-file=' . escapeshellarg($this->configFile)) : '' |
85
|
|
|
) |
86
|
|
|
); |
87
|
2 |
|
$process->run(); |
88
|
|
|
|
89
|
2 |
|
switch ($process->getExitCode()) { |
90
|
2 |
|
case 0: |
|
|
|
|
91
|
|
|
// file has been changed |
92
|
1 |
|
if ($this->addAutomatically) { |
93
|
|
|
exec('git add ' . escapeshellarg($file)); |
94
|
|
|
} |
95
|
1 |
|
break; |
96
|
1 |
|
case 1: |
97
|
|
|
// file not changed |
98
|
1 |
|
break; |
99
|
|
|
default: |
100
|
|
|
// some sort of error |
101
|
|
|
$failed[$file] = explode("\n", str_replace(PHP_EOL, "\n", $process->getOutput())); |
102
|
|
|
break; |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
$progress->advance(); |
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
if (count($failed)) { |
109
|
|
|
$message = 'PhpCsFixer failed for the following file(s):'; |
110
|
|
|
foreach ($failed as $file => $result) { |
111
|
|
|
$message .= PHP_EOL . '- ' . $file . ':'; |
112
|
|
|
$message .= PHP_EOL . ' - ' . implode(PHP_EOL . ' - ', $result); |
113
|
|
|
} |
114
|
|
|
throw new \RuntimeException($message); |
115
|
|
|
} |
116
|
|
|
|
117
|
3 |
|
$progress->finish(); |
118
|
3 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
protected function getBinFile() |
124
|
|
|
{ |
125
|
|
|
return PROJECT_ROOT . 'vendor' |
126
|
|
|
. DIRECTORY_SEPARATOR . 'friendsofphp' |
127
|
|
|
. DIRECTORY_SEPARATOR . 'php-cs-fixer' |
128
|
|
|
. DIRECTORY_SEPARATOR . 'php-cs-fixer'; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|