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 PhpCsFixerOld 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 (PhpCsFixerOld)'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
3 |
|
public function isSupported() |
60
|
|
|
{ |
61
|
3 |
|
if (!file_exists($this->binFile)) { |
62
|
|
|
$this->logger->warning( |
63
|
|
|
sprintf( |
64
|
|
|
'PHP-CS-Fixer could not be found in: %s', |
65
|
|
|
$this->binFile |
66
|
|
|
) |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
3 |
|
public function execute(InputInterface $input, OutputInterface $output) |
79
|
|
|
{ |
80
|
3 |
|
$files = $this->files->toArray(); |
81
|
|
|
|
82
|
3 |
|
if (empty($files)) { |
83
|
1 |
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
$progress = $this->createProgressBar($input, $output); |
87
|
2 |
|
$progress->start(count($files)); |
88
|
|
|
|
89
|
2 |
|
$failed = []; |
90
|
|
|
|
91
|
2 |
|
foreach ($files as $file) { |
92
|
2 |
|
$progress->setMessage('Checking <info>' . $file . '</info>...'); |
93
|
|
|
|
94
|
2 |
|
$process = new Process( |
95
|
|
|
sprintf( |
96
|
2 |
|
'php -f %s -- fix %s %s', |
97
|
2 |
|
escapeshellarg($this->binFile), |
98
|
|
|
escapeshellarg($file), |
99
|
2 |
|
$this->configFile ? ('--config-file=' . escapeshellarg($this->configFile)) : '' |
100
|
|
|
) |
101
|
|
|
); |
102
|
2 |
|
$process->run(); |
103
|
|
|
|
104
|
2 |
|
switch ($process->getExitCode()) { |
105
|
2 |
|
case 0: |
|
|
|
|
106
|
|
|
// file has been changed |
107
|
1 |
|
if ($this->addAutomatically) { |
108
|
|
|
exec('git add ' . escapeshellarg($file)); |
109
|
|
|
} |
110
|
1 |
|
break; |
111
|
1 |
|
case 1: |
112
|
|
|
// file not changed |
113
|
1 |
|
break; |
114
|
|
|
default: |
115
|
|
|
// some sort of error |
116
|
|
|
$failed[$file] = explode("\n", str_replace(PHP_EOL, "\n", $process->getOutput())); |
117
|
|
|
break; |
118
|
|
|
} |
119
|
|
|
|
120
|
2 |
|
$progress->advance(); |
121
|
|
|
} |
122
|
|
|
|
123
|
2 |
|
if (count($failed)) { |
124
|
|
|
$message = 'PhpCsFixerOld failed for the following file(s):'; |
125
|
|
|
foreach ($failed as $file => $result) { |
126
|
|
|
$message .= PHP_EOL . '- ' . $file . ':'; |
127
|
|
|
$message .= PHP_EOL . ' - ' . implode(PHP_EOL . ' - ', $result); |
128
|
|
|
} |
129
|
|
|
throw new \RuntimeException($message); |
130
|
|
|
} |
131
|
|
|
|
132
|
2 |
|
$progress->setMessage('Finished.'); |
133
|
2 |
|
$progress->finish(); |
134
|
2 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
protected function getBinFile() |
140
|
|
|
{ |
141
|
|
|
return PROJECT_ROOT . 'vendor' |
142
|
|
|
. DIRECTORY_SEPARATOR . 'friendsofphp' |
143
|
|
|
. DIRECTORY_SEPARATOR . 'php-cs-fixer' |
144
|
|
|
. DIRECTORY_SEPARATOR . 'php-cs-fixer'; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|