1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks; |
4
|
|
|
|
5
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Api\SearchAndReplaceAPI; |
6
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Replaces a bunch of code snippets in preparation of the upgrade. |
10
|
|
|
* Controversial replacements will be replaced with a comment |
11
|
|
|
* next to it so you can review replacements easily. |
12
|
|
|
*/ |
13
|
|
|
class FixBadUseStatements extends Task |
14
|
|
|
{ |
15
|
|
|
protected $taskStep = 's50'; |
16
|
|
|
|
17
|
|
|
protected $debug = false; |
18
|
|
|
|
19
|
|
|
protected $replacementArray = [ |
20
|
|
|
'src' => [ |
21
|
|
|
'php' => [ |
22
|
|
|
'use bool;', |
23
|
|
|
'use string;', |
24
|
|
|
'use int;', |
25
|
|
|
'use array;', |
26
|
|
|
], |
27
|
|
|
], |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
private $checkReplacementIssues = false; |
31
|
|
|
|
32
|
|
|
private $ignoreFolderArray = [ |
33
|
|
|
'.git', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
public function getTitle() |
37
|
|
|
{ |
38
|
|
|
return 'Look for single use statements and comment them out as they are not correct.'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getDescription() |
42
|
|
|
{ |
43
|
|
|
return ' |
44
|
|
|
Goes through code and removes, for example, "use bool;", lines, as they do not make sense.'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function setCheckReplacementIssues($b) |
48
|
|
|
{ |
49
|
|
|
$this->checkReplacementIssues = $b; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function setIgnoreFolderArray($a) |
55
|
|
|
{ |
56
|
|
|
$this->ignoreFolderArray = $a; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function setReplacementArray($a) |
62
|
|
|
{ |
63
|
|
|
$this->replacementArray = $a; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function runActualTask($params = []) |
69
|
|
|
{ |
70
|
|
|
//replacement data patterns that will be searched for |
71
|
|
|
|
72
|
|
|
if ($this->debug) { |
73
|
|
|
$this->mu()->colourPrint(print_r($this->replacementArray, 1)); |
74
|
|
|
} |
75
|
|
|
foreach ($this->mu()->getExistingModuleDirLocations() as $moduleDir) { |
76
|
|
|
//Start search machine from the module location. replace API |
77
|
|
|
$textSearchMachine = new SearchAndReplaceAPI($moduleDir); |
78
|
|
|
$textSearchMachine->setIsReplacingEnabled(true); |
79
|
|
|
$textSearchMachine->addToIgnoreFolderArray($this->ignoreFolderArray); |
80
|
|
|
|
81
|
|
|
/*For all the different patterns listed in the replacement array |
82
|
|
|
* iterate over them such that the $path would be 'src' and $patharray would be 'php' |
83
|
|
|
* together making it ['src']['php'] |
84
|
|
|
*/ |
85
|
|
|
foreach ($this->replacementArray as $path => $pathArray) { |
86
|
|
|
$path = $moduleDir . '/' . $path ?: ''; |
87
|
|
|
$path = $this->mu()->checkIfPathExistsAndCleanItUp($path); |
88
|
|
|
if (! file_exists($path)) { |
89
|
|
|
$this->mu()->colourPrint("SKIPPING ${path} as it does not exist."); |
90
|
|
|
} else { |
91
|
|
|
$textSearchMachine->setSearchPath($path); |
92
|
|
|
foreach ($pathArray as $extension => $extensionArray) { |
93
|
|
|
$textSearchMachine->setExtensions(explode('|', $extension)); //setting extensions to search files within |
94
|
|
|
foreach ($extensionArray as $find) { |
95
|
|
|
$ignoreCase = true; |
|
|
|
|
96
|
|
|
$caseSensitive = false; |
97
|
|
|
$isStraightReplace = true; |
|
|
|
|
98
|
|
|
$replacementType = 'BASIC'; |
99
|
|
|
|
100
|
|
|
// $this->mu()->colourPrint( |
101
|
|
|
// "++++++++++++++++++++++++++++++++++++\n". |
102
|
|
|
// "CHECKING\n". |
103
|
|
|
// "IN $path\n". |
104
|
|
|
// "FOR $extension FILES\n". |
105
|
|
|
// "BASE ".$moduleDir."\n". |
106
|
|
|
// "FIND '".$find."'\n". |
107
|
|
|
// "REPLACE ''\n". |
108
|
|
|
// "++++++++++++++++++++++++++++++++++++\n" |
109
|
|
|
// ); |
110
|
|
|
$textSearchMachine->setSearchKey($find, $caseSensitive, $replacementType); |
111
|
|
|
$textSearchMachine->setReplacementKey(''); |
112
|
|
|
$textSearchMachine->startSearchAndReplace(); |
113
|
|
|
} |
114
|
|
|
//SHOW TOTALS |
115
|
|
|
$replacements = $textSearchMachine->showFormattedSearchTotals(); |
116
|
|
|
if (! $replacements) { |
117
|
|
|
//flush output anyway! |
118
|
|
|
$this->mu()->colourPrint("No replacements for ${extension}"); |
119
|
|
|
} |
120
|
|
|
$this->mu()->colourPrint($textSearchMachine->getOutput()); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function hasCommitAndPush() |
128
|
|
|
{ |
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|