| Conditions | 17 |
| Paths | 400 |
| Total Lines | 109 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 50 | public function runActualTask($params = []) |
||
| 51 | { |
||
| 52 | //replacement data patterns that will be searched for |
||
| 53 | $replacementArray = [ |
||
| 54 | 'src' => [ |
||
| 55 | 'php' => [ |
||
| 56 | 'Requirements::javascript(' => [ |
||
| 57 | 'R' => '', |
||
| 58 | ], |
||
| 59 | 'Requirements::css(' => [ |
||
| 60 | 'R' => '', |
||
| 61 | ], |
||
| 62 | 'Requirements::themedCSS(' => [ |
||
| 63 | 'R' => '', |
||
| 64 | ], |
||
| 65 | ], |
||
| 66 | ], |
||
| 67 | ]; |
||
| 68 | |||
| 69 | if ($this->debug) { |
||
| 70 | $this->mu()->colourPrint(print_r($replacementArray, 1)); |
||
| 71 | } |
||
| 72 | foreach ($this->mu()->getExistingModuleDirLocations() as $moduleDir) { |
||
| 73 | //Start search machine from the module location. replace API |
||
| 74 | $textSearchMachine = new SearchAndReplaceAPI($moduleDir); |
||
| 75 | $textSearchMachine->setIsReplacingEnabled(true); |
||
| 76 | $textSearchMachine->addToIgnoreFolderArray($this->ignoreFolderArray); |
||
| 77 | |||
| 78 | /*For all the different patterns listed in the replacement array |
||
| 79 | * iterate over them such that the $path would be 'src' and $patharray would be 'php' |
||
| 80 | * together making it ['src']['php'] |
||
| 81 | */ |
||
| 82 | foreach ($replacementArray as $path => $pathArray) { |
||
| 83 | $path = $moduleDir . '/' . $path ?: ''; |
||
| 84 | $path = $this->mu()->checkIfPathExistsAndCleanItUp($path); |
||
| 85 | if (! file_exists($path)) { |
||
| 86 | $this->mu()->colourPrint("SKIPPING ${path} as it does not exist."); |
||
| 87 | } else { |
||
| 88 | $textSearchMachine->setSearchPath($path); |
||
| 89 | foreach ($pathArray as $extension => $extensionArray) { |
||
| 90 | $textSearchMachine->setExtensions(explode('|', $extension)); //setting extensions to search files within |
||
| 91 | $this->mu()->colourPrint( |
||
| 92 | "++++++++++++++++++++++++++++++++++++\n" . |
||
| 93 | "CHECKING\n" . |
||
| 94 | "IN ${path}\n" . |
||
| 95 | "FOR ${extension} FILES\n" . |
||
| 96 | 'BASE ' . $moduleDir . "\n" . |
||
| 97 | "++++++++++++++++++++++++++++++++++++\n" |
||
| 98 | ); |
||
| 99 | foreach ($extensionArray as $find => $findDetails) { |
||
| 100 | $replace = isset($findDetails['R']) ? $findDetails['R'] : $find; |
||
| 101 | $comment = isset($findDetails['C']) ? $findDetails['C'] : ''; |
||
|
|
|||
| 102 | $ignoreCase = true; |
||
| 103 | $caseSensitive = ! $ignoreCase; |
||
| 104 | |||
| 105 | $isStraightReplace = true; |
||
| 106 | |||
| 107 | // REPLACMENT PATTERN! |
||
| 108 | //Requirements::javascript(moduledirfolder/bla); |
||
| 109 | //Requirements::javascript(vpl: bla); |
||
| 110 | $findWithPackageName = $find . strtolower($this->mu()->getPackageName()); |
||
| 111 | $vendorAndPackageFolderNameForInstall = $this->mu()->getVendorAndPackageFolderNameForInstall(); |
||
| 112 | if (! $find) { |
||
| 113 | user_error("no find is specified, replace is: ${replace}"); |
||
| 114 | } |
||
| 115 | $replacementType = $isStraightReplace ? 'BASIC' : 'COMPLEX'; |
||
| 116 | |||
| 117 | foreach (['\'', '"'] as $quoteMark) { |
||
| 118 | $finalReplace = $find . $quoteMark . $vendorAndPackageFolderNameForInstall . ': '; |
||
| 119 | if (! $finalReplace && $finalReplace !== ' ') { |
||
| 120 | user_error("no replace is specified, find is: ${find}. We suggest setting your final replace to a single space if you would like to replace with NOTHING."); |
||
| 121 | } |
||
| 122 | $finalFind = $find . $quoteMark; |
||
| 123 | $this->mu()->colourPrint( |
||
| 124 | ' --- FIND: ' . $finalFind . "\n" . |
||
| 125 | ' --- REPLACE: ' . $finalReplace . "\n" |
||
| 126 | ); |
||
| 127 | |||
| 128 | $textSearchMachine->setSearchKey($finalFind, $caseSensitive, $replacementType); |
||
| 129 | $textSearchMachine->setReplacementKey($finalReplace); |
||
| 130 | $textSearchMachine->startSearchAndReplace(); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | //fix double-ups |
||
| 135 | //fixes things like |
||
| 136 | //vendor/packagename: silverstripe/admin |
||
| 137 | //to |
||
| 138 | //silverstripe/admin: only |
||
| 139 | foreach (['cms', 'framework', 'siteconfig', 'reports'] as $ssModule) { |
||
| 140 | $isStraightReplace = true; |
||
| 141 | $finalFind = $vendorAndPackageFolderNameForInstall . ': silverstripe/' . $ssModule . ': '; |
||
| 142 | $finalReplace = 'silverstripe/' . $ssModule . ': '; |
||
| 143 | $this->mu()->colourPrint( |
||
| 144 | ' --- FIND: ' . $finalFind . "\n" . |
||
| 145 | ' --- REPLACE: ' . $finalReplace . "\n" |
||
| 146 | ); |
||
| 147 | $textSearchMachine->setSearchKey($finalFind, $isStraightReplace, 'silverstripe/' . $ssModule . '/@@@@double-up@@@@'); |
||
| 148 | $textSearchMachine->setReplacementKey($finalReplace); |
||
| 149 | $textSearchMachine->startSearchAndReplace(); |
||
| 150 | } |
||
| 151 | |||
| 152 | //SHOW TOTALS |
||
| 153 | $replacements = $textSearchMachine->showFormattedSearchTotals(); |
||
| 154 | if (! $replacements) { |
||
| 155 | //flush output anyway! |
||
| 156 | $this->mu()->colourPrint("No replacements for ${extension}"); |
||
| 157 | } |
||
| 158 | $this->mu()->colourPrint($textSearchMachine->getOutput()); |
||
| 159 | } |
||
| 170 |