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
/
Copy pathScisrRunner.php
457 lines (411 loc) · 15.7 KB
/
ScisrRunner.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
<?php
/*
* Copyright (C) 2009, 2010 Ian Young
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Turn on error reporting
error_reporting(E_ALL);
// Register our autoloader
spl_autoload_register(array('ScisrRunner', 'scisrAutoload'));
// Include the main CodeSniffer file (this will register its own autoloader as well)
require_once(dirname(__FILE__) . '/PHP/CodeSniffer.php');
/**
* The main Scisr controller
*
* Coordinates the tasks to take place for the given action(s)
*/
class ScisrRunner
{
const MODE_TIMID = 0;
const MODE_CONSERVATIVE = 1;
const MODE_AGGRESSIVE = 2;
/**
* Callbacks to be invoked after the first, read-only pass
* @var array
*/
protected $_firstPassCallbacks = array();
/**
* CodeSniffer listener objects to be used during the main processing pass
* @var array
*/
protected $_listeners = array();
/**
* The output object to handle our messages
* @var Scisr_Output
*/
protected $_output;
/**
* Our codesniffer instance
* @var Scisr_CodeSniffer
*/
private $_sniffer;
/**
* If we must run a first pass
* @var boolean
*/
protected $_firstPassRequired = false;
/**
* The edit mode
* @var int one of MODE_TIMID, MODE_CONSERVATIVE, or MODE_AGGRESSIVE
*/
private $_mode;
/**
* @var Scisr_ChangeRegistry
*/
private $_changeRegistry;
/**
* @var Scisr_Operations_Factory
*/
private $_operationsFactory;
/**
* @var Scisr_Db_Files
*/
private $_dbFiles;
/**
* @var Scisr_Db_FileIncludes
*/
private $_dbFileIncludes;
/**
* @var Scisr_Db_Classes
*/
private $_dbClasses;
public function __construct(Scisr_ChangeRegistry $changeRegistry, Scisr_CodeSniffer $sniffer, Scisr_Db_Classes $dbClasses, Scisr_Operations_Factory $operationsFactory)
{
$this->setEditMode(self::MODE_CONSERVATIVE);
$this->_output = new Scisr_Output_Null();
$this->_dbClasses = $dbClasses;
$this->_changeRegistry = $changeRegistry;
$this->_sniffer = $sniffer;
$this->_operationsFactory = $operationsFactory;
}
public function setOutput($output)
{
$this->_output = $output;
}
public static function createRunner($className = 'ScisrRunner', $db = null)
{
if ($db === null) {
$db_path = dirname(__FILE__) . '/cache.db';
$db = new PDO("sqlite:$db_path", null, null, array(PDO::ATTR_PERSISTENT => true));
}
$dbFiles = new Scisr_Db_Files($db);
$dbFiles->init();
$dbFileIncludes = new Scisr_Db_FileIncludes($db);
$dbFileIncludes->init();
$dbClasses = new Scisr_Db_Classes($db);
$dbClasses->init();
$dbVariableTypes = new Scisr_Db_VariableTypes($db);
$dbVariableTypes->init();
$variableTypes = new Scisr_Operations_VariableTypes($dbClasses, $dbFileIncludes, $dbVariableTypes);
$changeRegistry = new Scisr_ChangeRegistry();
$sniffer = new Scisr_CodeSniffer($dbFiles);
$operationsFactory = new Scisr_Operations_Factory(array(
$changeRegistry,
$dbFiles,
$dbFileIncludes,
$dbClasses,
$dbVariableTypes,
$variableTypes
));
return new $className($changeRegistry, $sniffer, $dbClasses, $operationsFactory);
}
public function getFactory()
{
return $this->_operationsFactory;
}
/**
* For testing use only. Dependency injection.
* @ignore
* @param Scisr_CodeSniffer
*/
public function setSniffer($sniffer)
{
$this->_sniffer = $sniffer;
}
/**
* Set filename patterns to ignore when parsing
* @see PHP_CodeSniffer::setIgnorePatterns
*/
public function setIgnorePatterns($patterns)
{
// Just pass it right through to phpcs
$this->_sniffer->setIgnorePatterns($patterns);
}
/**
* Set allowed file extensions
* @see PHP_CodeSniffer::setAllowedFileExtensions
*/
public function setAllowedFileExtensions($extensions)
{
// Just pass it right through to phpcs
$this->_sniffer->setAllowedFileExtensions($extensions);
}
/**
* Get the list of allowed file extensions
* @return array(string) a list of file extensions
*/
public function getAllowedFileExtensions()
{
return $this->_sniffer->allowedFileExtensions;
}
/**
* Get the listeners to be run on the first (read-only) pass
*/
protected function getFirstPassListeners()
{
$listeners = array();
$listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_TrackGlobalVariables');
$listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_TrackVariableTypes');
$listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_TrackVariableTypeHints');
$listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_TrackCommentVariableTypes');
$listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_TrackIncludedFiles');
$listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_TrackClasses');
$listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_TrackReturnStatementTypes');
$includesOp = $this->_operationsFactory->getOperation('Scisr_Operations_TrackClassIncludes');
$this->_firstPassCallbacks[] = array(array($includesOp, 'registerIncludes'), array());
$listeners[] = $includesOp;
return $listeners;
}
/**
* Rename a class
* @param string $oldClass the class to be renamed
* @param string $newClass the new class name to be given
*/
public function setRenameClass($oldClass, $newClass)
{
$this->_listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_RenameClass', $oldClass, $newClass);
$this->_listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_ChangeClassNameComments', $oldClass, $newClass);
$this->_listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_ChangeCommentWords', $oldClass, $newClass);
$this->_listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_ChangeStringWords', $oldClass, $newClass);
}
/**
* Rename a class method
* @param string $class the class that contains the method to be renamed
* @param string $oldMethod the method to be renamed
* @param string $newMethod the new method name
* @param boolean $withInheritance if true, also rename method on child classes
*/
public function setRenameMethod($class, $oldMethod, $newMethod, $withInheritance)
{
if ($withInheritance) {
$this->_firstPassCallbacks[] = array(array($this, 'doRenameChildMethods'), array($class, $oldMethod, $newMethod));
}
$this->_listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_RenameMethod', $class, $oldMethod, $newMethod);
// Look for matches in comments and strings
$this->_listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_ChangeCommentWords', $oldMethod, $newMethod);
$this->_listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_ChangeStringWords', $oldMethod, $newMethod);
$fullOldString = "$class(->|::)$oldMethod";
$fullNewString = "$class\\1$newMethod";
$this->_listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_ChangeCommentWords', $fullOldString, $fullNewString, false);
$this->_listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_ChangeStringWords', $fullOldString, $fullNewString, false);
$this->_firstPassRequired = true;
}
/**
* Callback that uses the class information from the first pass to rename
* methods on child classes.
*/
protected function doRenameChildMethods($class, $oldMethod, $newMethod)
{
$classes = $this->_dbClasses->getChildClasses($class);
foreach ($classes as $child) {
$this->setRenameMethod($child, $oldMethod, $newMethod, false);
}
}
/**
* Rename a file
* @param string $oldFilePath the path to the file to be renamed
* @param string $newFilePath the new path to give it
*/
public function setRenameFile($oldFilePath, $newFilePath)
{
$this->_listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_RenameFile', $oldFilePath, $newFilePath);
if (!file_exists($oldFilePath)) {
$msg = 'does not exist, so will not be moved.';
} else if (!is_writeable($oldFilePath)) {
$msg = 'could not be moved.';
} else {
$this->_changeRegistry->addRename($oldFilePath, $newFilePath);
}
if (isset($msg)) {
$msg = "File \"$oldFilePath\" " . $msg;
$this->sendOutput($msg);
}
}
/**
* Split a file containing several classes into a set of files
* @param string $oldFilePath the path to the file to be split
* @param string $outputPath the path where the files should be saved
*/
public function setSplitClassFiles($outputPath)
{
$this->_listeners[] = $this->_operationsFactory->getOperation('Scisr_Operations_SplitClassFile', $outputPath);
if (!is_dir($outputPath) || !is_writeable($outputPath)) {
$msg = $outputPath . ' is not writeable or a directory.';
}
if (isset($msg)) {
$msg = "File \"$outputPath\" " . $msg;
$this->sendOutput($msg);
}
}
/**
* Rename a class, renaming the file as well if possible
* @param string $oldClass the class to be renamed
* @param string $newClass the new class name to be given
*/
public function setRenameClassFile($oldClass, $newClass)
{
$this->_firstPassCallbacks[] = array(array($this, 'doRenameClassFile'), array($oldClass, $newClass));
$this->setRenameClass($oldClass, $newClass);
$this->_firstPassRequired = true;
}
/**
* Callback that uses the class information from the first pass to figure
* out how to rename class files.
*/
protected function doRenameClassFile($oldClass, $newClass)
{
$oldFilePath = $this->_dbClasses->getClassFile($oldClass);
if ($oldFilePath !== null) {
$pieces = explode('_', $oldClass);
foreach (array_keys($this->getAllowedFileExtensions()) as $ext) {
if (basename($oldFilePath) == "$oldClass.$ext") {
$dir = dirname($oldFilePath);
$this->setRenameFile($oldFilePath, "$dir/$newClass.$ext");
break;
} else if (count($pieces) > 1) {
$namespacedFile = implode('/', $pieces) . ".$ext";
$baseDir = Scisr_Operations_RenameFile::matchPaths($oldFilePath, $namespacedFile);
if ($baseDir !== false) {
$newNamespacedFile = implode('/', explode('_', $newClass)) . ".$ext";
$this->setRenameFile($oldFilePath, "$baseDir$newNamespacedFile");
break;
}
}
}
}
}
/**
* Add a file or directory to be parsed
* @param string $filename the path to the file or directory
*/
public function addFile($filename)
{
$filename = Scisr_File::getAbsolutePath($filename);
$this->files[] = $filename;
}
/**
* Add multiple files or directories to be parsed
* @param array an array of file or directory paths
*/
public function addFiles($fileArray)
{
array_map(array($this, 'addFile'), $fileArray);
}
/**
* Set how destructive we are editing
* @param int one of the following class constants:
* + MODE_TIMID: Make no changes to the files, just report possible changes
* + MODE_CONSERVATIVE: Make changes we are relatively sure are correct.
* Warn about possible changes we aren't sure about.
* + MODE_AGGRESSIVE: Make any changes we find.
*/
public function setEditMode($mode)
{
$this->_mode = $mode;
}
/**
* Perform the requested changes
*/
public function run()
{
$sniffer = $this->_sniffer;
// If we need to, make a read-only pass to populate our type information
if ($this->_firstPassRequired) {
foreach ($this->getFirstPassListeners() as $listener) {
$sniffer->addListener($listener);
}
$sniffer->process($this->files, false, true);
}
// Now call any callbacks registered to run after the first pass
foreach ($this->_firstPassCallbacks as $cbArray) {
$callback = $cbArray[0];
$args = $cbArray[1];
call_user_func_array($callback, $args);
}
// Clear out the first pass listeners before we run the second pass
$sniffer->clearListeners();
foreach ($this->_listeners as $listener) {
$sniffer->addListener($listener);
}
// Run the sniffer
$sniffer->process($this->files);
// Get the changes that have been registered
$changes = $this->_changeRegistry->get('storedChanges');
if (!is_array($changes)) {
$changes = array();
}
// Now make the actual changes
$filesChanged = 0;
$totalWarnings = 0;
$warnings = array();
foreach ($changes as $file) {
$changed = $file->process($this->_mode);
$filesChanged += ($changed ? 1 : 0);
$numWarnings = $file->getNumChangesNotProcessed();
if ($numWarnings > 0) {
$totalWarnings += $numWarnings;
$warnings[$file->filename] = $file->getLinesNotProcessed();
}
}
// Display a summary line
$msg = "Changed $filesChanged files";
$this->sendOutput($msg);
// If we have any notifications, display them
if ($totalWarnings > 0) {
// Display a summary
$numFiles = count($warnings);
$msg = "Found $totalWarnings possible changes in $numFiles files that were not applied:";
$this->sendOutput($msg);
// Now display each line where we found changes
foreach ($warnings as $filename => $lines) {
foreach ($lines as $lineNo) {
$this->sendOutput("$filename:$lineNo");
}
}
}
}
/**
* Send output to the user.
* @param string $message the message to send
*/
public function sendOutput($message)
{
$this->_output->outputString($message);
}
/**
* An autoload function for Scisr
* @param string
*/
public static function scisrAutoload($className)
{
if (strpos($className, 'Scisr_') === 0) {
$className = substr($className, 6);
$path = str_replace('_', '/', $className).'.php';
if (is_file(dirname(__FILE__).'/'.$path) === true) {
include dirname(__FILE__).'/'.$path;
}
}
}
}