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