This repository has been archived by the owner on Jul 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
CLI.php
351 lines (314 loc) · 11.2 KB
/
CLI.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
/**
* Handles command line interaction for Scisr
*
* @todo We could extend PHP_CodeSniffer_CLI and make use of some of the parsing
* stuff instead of rolling our own. We would gain some flexibility in where
* flags can be placed in the list, and it would fit reasonably well with the
* need for "scisr command [variable arity args]".
*
* Downsides are that it won't quite work out of the box ($argv overrides our
* testing stuff), and it's not flexible about --flag=val vs. --flag val.
*/
class Scisr_CLI implements Scisr_Output
{
const OPT_NONE = 0;
const OPT_REQUIRED = 1;
/**
* If true, quit without running and show usage instead
* @var boolean
*/
private $showHelp = false;
/**
* When renaming a method, true if we should also rename children methods
* @var boolean
*/
private $withInheritance = true;
public function __construct($output=null)
{
if ($output === null) {
$output = new Scisr_Output_CLI();
}
$this->output = $output;
$this->scisr = ScisrRunner::createRunner();
$this->scisr->setOutput($this->output);
}
/**
* Parse command line options
* @param array $args array of options passed to the program
*/
protected function parseOpts($args)
{
// Parse all other options
$shortOptions = 'athi:e:';
$longOptions = array('aggressive', 'timid', 'no-inheritance', 'help', 'ignore=', 'extensions=');
$options = $this->getopt($args, $shortOptions, $longOptions);
$unparsedOptions = $options[1];
$this->parseOtherOpts($options[0]);
if ($this->showHelp) {
return;
}
$this->parseActionOpts($unparsedOptions);
if (count($unparsedOptions) == 0) {
throw new Exception('No paths provided to examine');
}
$this->scisr->addFiles($unparsedOptions);
}
private function parseActionOpts(&$params)
{
// Get the action name
$action = $this->getArg($params);
$actionOpts = array();
switch ($action) {
case 'rename-class':
$oldName = $this->getArg($params);
$newName = $this->getArg($params);
$this->scisr->setRenameClass($oldName, $newName);
break;
case 'rename-method':
$class = $this->getArg($params);
$oldName = $this->getArg($params);
$newName = $this->getArg($params);
$this->scisr->setRenameMethod($class, $oldName, $newName, $this->withInheritance);
break;
case 'rename-file':
$oldName = $this->getArg($params);
$newName = $this->getArg($params);
$this->scisr->setRenameFile($oldName, $newName);
break;
case 'rename-class-file':
$oldName = $this->getArg($params);
$newName = $this->getArg($params);
$this->scisr->setRenameClassFile($oldName, $newName);
break;
case 'split-class-files':
$outputPath = $this->getArg($params);
$this->scisr->setSplitClassFiles($outputPath);
break;
default:
throw new Exception("Command \"$action\" not recognized");
}
}
private function getArg(&$params)
{
$arg = array_shift($params);
if ($arg === null) {
throw new Exception("Not enough arguments");
}
return $arg;
}
private function parseOtherOpts($params)
{
foreach ($params as $key => $value) {
switch ($key) {
case "a":
case "aggressive":
$this->scisr->setEditMode(ScisrRunner::MODE_AGGRESSIVE);
break;
case "no-inheritance":
$this->withInheritance = false;
break;
case "t":
case "timid":
$this->scisr->setEditMode(ScisrRunner::MODE_TIMID);
break;
case "i":
case "ignore":
// We doctor and pass this value in to let phpcs run a pattern on it
$fakekey = 'ignore=' . $value;
$cli = new PHP_CodeSniffer_CLI();
$result = $cli->processLongArgument($fakekey, null, array());
$this->scisr->setIgnorePatterns($result['ignored']);
break;
case "e":
case "extensions":
// We doctor and pass this value in to let phpcs run a pattern on it
$fakekey = 'extensions=' . $value;
$cli = new PHP_CodeSniffer_CLI();
$result = $cli->processLongArgument($fakekey, null, array());
$this->scisr->setAllowedFileExtensions($result['extensions']);
break;
case "help":
$this->showHelp = true;
break;
}
}
}
/**
* For testing use only. Dependency injection.
* @ignore
* @param ScisrRunner
*/
public function setRunner($scisr)
{
$this->scisr = $scisr;
}
/**
* Process the CLI arguments and run Scisr
* @param array $args the command arguments - for use in testing only
*/
public function process($args=null)
{
// Get options from the command line
if ($args === null) {
global $argv;
$args = $argv;
}
// Remove our own filename
array_shift($args);
// Send to the options handler
try {
$this->parseOpts($args);
if ($this->showHelp) {
$this->printUsage();
return 0;
}
} catch (Exception $e) {
$this->outputString('Error: ' . $e->getMessage());
$this->outputString("\n");
$this->printUsage();
return 2;
}
// Run Scisr
$this->scisr->run();
return 0;
}
// We proxy output through here for simplicity
public function outputString($message)
{
$this->output->outputString($message);
}
/**
* Parse command line options
*
* Believe me, I'm not happy to be reinventing the wheel here. It's just
* that all the other wheels PHP and third parties have to offer in this
* department are inferior. This wheel is inferior too, but at least in ways
* that work for me.
*
* @param array $args the array of arguments from the command line
* @param string $shortOpts short options as proscribed by PHP's getopt()
* @param string $longOpts long options as proscribed by PHP's getopt()
*/
protected function getopt($args, $shortOpts, $longOpts)
{
$longOpts = $this->parseLongOpts($longOpts);
$shortOpts = $this->parseShortOpts($shortOpts);
$len = count($args);
$i = 0;
$parsedOptions = array();
$nonOptions = array();
while ($i < $len) {
$curr = $args[$i];
if (substr($curr, 0, 2) == '--') {
$opt = substr($curr, 2);
// If there is a "=", split this argument into opt and value
if (($pos = strpos($opt, '=')) !== false) {
$value = substr($opt, $pos + 1);
$opt = substr($opt, 0, $pos);
if ($longOpts[$opt] != self::OPT_REQUIRED) {
throw new Exception("Value given for option \"$opt\", which does not accept a value");
}
} else if ($longOpts[$opt] == self::OPT_REQUIRED) {
$value = $args[++$i];
} else {
$value = null;
}
if (!array_key_exists($opt, $longOpts)) {
throw new Exception("Option \"$opt\" not recognized");
}
$parsedOptions[$opt] = $value;
} else if (substr($curr, 0, 1) == '-') {
$opt = substr($curr, 1, 1);
if (strlen($curr) > 2) {
$value = substr($curr, 2);
if ($shortOpts[$opt] != self::OPT_REQUIRED) {
throw new Exception("Value given for option \"$opt\", which does not accept a value");
}
} else if ($shortOpts[$opt] == self::OPT_REQUIRED) {
$value = $args[++$i];
} else {
$value = null;
}
if (!array_key_exists($opt, $shortOpts)) {
throw new Exception("Option \"$opt\" not recognized");
}
$parsedOptions[$opt] = $value;
} else {
$nonOptions[] = $curr;
}
$i++;
}
return array($parsedOptions, $nonOptions);
}
/**
* Helper function to {@link $this->getopt()}
*/
private function parseShortOpts($opts)
{
$result = array();
$opts = preg_split('/(\w:?:?)/', $opts, null, PREG_SPLIT_DELIM_CAPTURE);
foreach ($opts as $opt) {
if ($opt == '') continue;
if (substr($opt, -2) == '::') {
// We aren't handling this case for now
} else if (substr($opt, -1) == ':') {
$name = substr($opt, 0, -1);
$req = self::OPT_REQUIRED;
} else {
$name = $opt;
$req = self::OPT_NONE;
}
$result[$name] = $req;
}
return $result;
}
/**
* Helper function to {@link $this->getopt()}
*/
private function parseLongOpts($opts)
{
$result = array();
foreach ($opts as $opt) {
if (substr($opt, -2) == '==') {
// We aren't handling this case for now
} else if (substr($opt, -1) == '=') {
$name = substr($opt, 0, -1);
$req = self::OPT_REQUIRED;
} else {
$name = $opt;
$req = self::OPT_NONE;
}
$result[$name] = $req;
}
return $result;
}
/**
* Print usage information to our output handler
*/
public function printUsage()
{
$usage = <<<EOL
Usage:
scisr rename-class OldName NewName [options] [files]
scisr rename-method OwningClassName oldMethodName newMethodName [options] [files]
scisr rename-file old/file_name new/dir/new_file_name [options] [files]
scisr rename-class-file OldName NewName [options] [files]
scisr split-class-files OutputDir [options] [files]
[files] is any number of files and/or directories to be searched and modified.
Options:
--no-inheritance Only with rename-method. Do not rename method in descendants of
the given class.
-t, --timid Do not make changes to the files, just list filenames
with line numbers.
-a, --aggressive Make changes even when we're not sure they're correct.
-e<extensions>, --extensions=<extensions>
Specify a comma-separated list of allowed file extensions.
-i<patterns>, --ignore=<patterns>
Specify a comma-separated list of patterns used to
ignore directories and files.
-h, --help Print usage instructions.
EOL;
$this->outputString($usage);
}
}