| Total Complexity | 40 |
| Total Lines | 329 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 2 | Features | 0 |
Complex classes like AddFileToModule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AddFileToModule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | abstract class AddFileToModule extends Object |
||
| 11 | { |
||
| 12 | protected $gitReplaceArray = array( |
||
| 13 | '+++long-module-name-goes-here+++' => 'LongModuleName', |
||
| 14 | '+++medium-module-name-goes-here+++' => 'MediumModuleName', |
||
| 15 | '+++short-module-name-goes-here+++' => 'ShortModuleName', |
||
| 16 | '+++module-name-goes-here+++' => 'ShortModuleName', |
||
| 17 | '+++short-module-name-first-letter-capital+++' => 'ModuleNameFirstLetterCapital' |
||
| 18 | ); |
||
| 19 | |||
| 20 | |||
| 21 | protected $replaceArray = array( |
||
| 22 | '+++README_DOCUMENTATION+++' => 'Documentation', |
||
| 23 | '+++README_SUGGESTED_MODULES+++' => 'SuggestedModules', |
||
| 24 | '+++README_REQUIREMENTS+++' => 'Requirements', |
||
| 25 | '+++README_INSTALLATION+++' => 'Installation', |
||
| 26 | '+++README_AUTHOR+++' => 'Author', |
||
| 27 | '+++README_ASSISTANCE+++' => 'Assistance', |
||
| 28 | '+++README_CONTRIBUTING+++' => 'Contributing', |
||
| 29 | '+++README_CONFIGURATION+++' => 'Configuration' |
||
| 30 | ); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * root dir for module |
||
| 34 | * e.g. /var/www/modules/mymodule |
||
| 35 | * no final slash |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $rootDirForModule = ''; |
||
| 40 | |||
| 41 | |||
| 42 | /** |
||
| 43 | * root dir for module |
||
| 44 | * e.g. |
||
| 45 | * - README.md |
||
| 46 | * OR |
||
| 47 | * - docs/index.php |
||
| 48 | * |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $fileLocation = ''; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * e.g. |
||
| 56 | * http://www.mysite.com/myfile.txt |
||
| 57 | * myfile.txt |
||
| 58 | * where examples.txt will have the base dir + modulechecks director added to it |
||
| 59 | * e.g. |
||
| 60 | * examples/myfile.txt becomes |
||
| 61 | * /var/www/myproject/modulechecks/examples/myfile.txt |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $sourceLocation = 'please set in files that extend '; |
||
| 65 | |||
| 66 | protected $useCustomisationFile = false; |
||
| 67 | |||
| 68 | protected $gitObject=null; |
||
| 69 | |||
| 70 | public function __construct($gitObject) |
||
| 71 | { |
||
| 72 | parent::__construct(); |
||
| 73 | $this->gitObject = $gitObject; |
||
| 74 | $rootDirForModule = $gitObject->Directory(); |
||
| 75 | $this->rootDirForModule = $rootDirForModule; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function setRootDirForModule($rootDirForModule) |
||
| 79 | { |
||
| 80 | $this->$rootDirForModule = $rootDirForModule; |
||
| 81 | } |
||
| 82 | |||
| 83 | public function setSourceLocation($sourceLocation) |
||
| 84 | { |
||
| 85 | $this->sourceLocation = $sourceLocation; |
||
| 86 | } |
||
| 87 | |||
| 88 | public function setFileLocation($relativeDirAndFileName) |
||
| 89 | { |
||
| 90 | $this->fileLocation = $relativeDirAndFileName; |
||
| 91 | } |
||
| 92 | |||
| 93 | |||
| 94 | public function run() |
||
| 95 | { |
||
| 96 | if (! $this->rootDirForModule) { |
||
| 97 | user_error('no root dir for module has been set'); |
||
| 98 | } |
||
| 99 | if (! $this->fileLocation) { |
||
| 100 | user_error('File location not set'); |
||
| 101 | } |
||
| 102 | $fileContent = $this->getStandardFile(); |
||
| 103 | |||
| 104 | if ($this->useCustomisationFile) { |
||
| 105 | $fileContent = $this->customiseStandardFile($fileContent); |
||
| 106 | } |
||
| 107 | |||
| 108 | $this->saveFile($fileContent); |
||
| 109 | if ($fileContent) { |
||
| 110 | $this->replaceWordsInFile(); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * you can either return the string from the |
||
| 116 | * `$sourceLocation` or you can just have a string here |
||
| 117 | * that returns the data directly.... |
||
| 118 | * |
||
| 119 | * @param string $fileContent |
||
| 120 | * |
||
| 121 | * @return bool - true on success, false on failure |
||
| 122 | */ |
||
| 123 | protected function getStandardFile() |
||
| 124 | { |
||
| 125 | $isURL = (strpos($this->sourceLocation, '//') !== false); |
||
| 126 | |||
| 127 | if ($isURL) { |
||
| 128 | $fullFileName = $this->sourceLocation; |
||
| 129 | } else { |
||
| 130 | $fullFileName = Director::baseFolder().'/'.$this->sourceLocation; |
||
| 131 | } |
||
| 132 | |||
| 133 | print("<li>$fullFileName</li>"); |
||
| 134 | |||
| 135 | $file = fopen($fullFileName, "r"); |
||
| 136 | if ($file) { |
||
|
|
|||
| 137 | $fileSize = filesize($fullFileName); |
||
| 138 | |||
| 139 | if ($fileSize > 0) { |
||
| 140 | $fileContents = fread($file, filesize($fullFileName)); |
||
| 141 | } else { |
||
| 142 | $fileContents = ""; |
||
| 143 | } |
||
| 144 | fclose($file); |
||
| 145 | |||
| 146 | return $fileContents; |
||
| 147 | } else { |
||
| 148 | return false; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * takes the standard file and adds any |
||
| 154 | * customisations to it from the module |
||
| 155 | * |
||
| 156 | * @return bool - true on success, false on failure |
||
| 157 | */ |
||
| 158 | protected function customiseStandardFile($fileContent) |
||
| 159 | { |
||
| 160 | $obj = $this->getCustomisationFile(); |
||
| 161 | $fileContent = $obj->customiseFile($this->fileLocation, $fileContent); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * writes the file |
||
| 166 | * |
||
| 167 | * @param string $fileContent |
||
| 168 | * |
||
| 169 | * @return bool - true on success, false on failure |
||
| 170 | */ |
||
| 171 | protected function saveFile($fileContent) |
||
| 172 | { |
||
| 173 | GeneralMethods::output_to_screen("<li> Adding " . $this->fileLocation . " to module </li>"); |
||
| 174 | |||
| 175 | /* |
||
| 176 | * If fileLocation contains folder, name then need to check |
||
| 177 | * if folder exists |
||
| 178 | */ |
||
| 179 | if (strpos($this->fileLocation, '/')!==false) { |
||
| 180 | $folderPath = substr($this->fileLocation, 0, strrpos($this->fileLocation, '/')); |
||
| 181 | |||
| 182 | //print_r ($this->rootDirForModule.'/'.$folderPath); |
||
| 183 | |||
| 184 | if (!file_exists($this->rootDirForModule.'/'.$folderPath)) { |
||
| 185 | $folder = Filesystem::makeFolder($this->rootDirForModule.'/'.$folderPath); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | if (isset($folderPath) && !file_exists($this->rootDirForModule.'/'.$folderPath)) { |
||
| 190 | user_error('could not find or create directory ' . $this->rootDirForModule.'/'.$folderPath); |
||
| 191 | } |
||
| 192 | |||
| 193 | $fileName = $this->rootDirForModule.'/'.$this->fileLocation; |
||
| 194 | $this->fileLocation; |
||
| 195 | |||
| 196 | $file = fopen($fileName, "w"); |
||
| 197 | |||
| 198 | if ($file) { |
||
| 199 | $result = fwrite($file, $fileContent); |
||
| 200 | $a = file_exists($fileName); |
||
| 201 | } else { |
||
| 202 | return false; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | /** |
||
| 206 | * |
||
| 207 | * |
||
| 208 | * @return ModuleConfig (instance of ModuleConfigInterface) |
||
| 209 | */ |
||
| 210 | protected function getCustomisationFile() |
||
| 211 | { |
||
| 212 | require_once( |
||
| 213 | $this->rootDirForModule . '/ssmoduleconfigs/ModuleConfig.php' |
||
| 214 | ); |
||
| 215 | return Injector::inst()->get('ModuleConfig'); |
||
| 216 | } |
||
| 217 | |||
| 218 | |||
| 219 | protected function getReadMeComponent($componentName) |
||
| 220 | { |
||
| 221 | $temp_dir = GitHubModule::Config()->get('absolute_temp_folder'); |
||
| 222 | $moduleName = $this->gitObject->ModuleName; |
||
| 223 | |||
| 224 | $fileName = $temp_dir . '/' . $moduleName . '/docs/en/' . strtoupper($componentName) . '.md'; |
||
| 225 | |||
| 226 | set_error_handler(array($this, 'catchFopenWarning'), E_WARNING); |
||
| 227 | $file = fopen($fileName, 'r'); |
||
| 228 | restore_error_handler(); |
||
| 229 | |||
| 230 | if ($file) { |
||
| 231 | $content = fread($file, filesize($filename)); |
||
| 232 | } else { |
||
| 233 | $content = ""; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | /* |
||
| 238 | * |
||
| 239 | * */ |
||
| 240 | private function catchFopenWarning($errno, $errstr) |
||
| 241 | { |
||
| 242 | // |
||
| 243 | } |
||
| 244 | |||
| 245 | |||
| 246 | protected function Configuration() |
||
| 247 | { |
||
| 248 | return $this->getReadMeComponent('configuration'); |
||
| 249 | } |
||
| 250 | |||
| 251 | protected function Contributing() |
||
| 254 | } |
||
| 255 | |||
| 256 | protected function Documentation() |
||
| 257 | { |
||
| 258 | return $this->getReadMeComponent('documentation'); |
||
| 259 | } |
||
| 260 | |||
| 261 | protected function Requirements() |
||
| 262 | { |
||
| 263 | return $this->getReadMeComponent('requirements'); |
||
| 264 | } |
||
| 265 | |||
| 266 | protected function Installation() |
||
| 267 | { |
||
| 268 | return $this->getReadMeComponent('installation'); |
||
| 269 | } |
||
| 270 | |||
| 271 | protected function Author() |
||
| 272 | { |
||
| 273 | return $this->getReadMeComponent('author'); |
||
| 274 | } |
||
| 275 | |||
| 276 | protected function Assistance() |
||
| 277 | { |
||
| 278 | return $this->getReadMeComponent('assistance'); |
||
| 279 | } |
||
| 280 | |||
| 281 | protected function SuggestedModules() |
||
| 282 | { |
||
| 283 | return $this->getReadMeComponent('suggestedmodules'); |
||
| 284 | } |
||
| 285 | |||
| 286 | |||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * @param string $file |
||
| 291 | * @param GitHubModule $gitObject |
||
| 292 | * |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public function replaceWordsInFile() |
||
| 296 | { |
||
| 297 | foreach ($this->gitReplaceArray as $searchTerm => $replaceMethod) { |
||
| 298 | $fileName = $this->rootDirForModule.'/'.$this->fileLocation; |
||
| 299 | GeneralMethods::replaceInFile($fileName, $searchTerm, $this->gitObject->$replaceMethod()); |
||
| 300 | } |
||
| 301 | |||
| 302 | foreach ($this->replaceArray as $searchTerm => $replaceMethod) { |
||
| 303 | $fileName = $this->rootDirForModule.'/'.$this->fileLocation; |
||
| 304 | GeneralMethods::replaceInFile($fileName, $searchTerm, $this->$replaceMethod()); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | public function getFileLocation() |
||
| 313 | { |
||
| 314 | return $this->fileLocation; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param string $text |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | public function replaceWordsInText($text) |
||
| 332 | } |
||
| 333 | |||
| 334 | public function compareWithText($compareText) |
||
| 335 | { |
||
| 336 | $fileText = $this->getStandardFile(); |
||
| 337 | $text = $this->replaceWordsInText($fileText); |
||
| 338 | return (trim($text) == trim($compareText)); |
||
| 339 | } |
||
| 340 | } |
||
| 341 |