| Conditions | 3 |
| Paths | 4 |
| Total Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 declare(strict_types=1); |
||
| 90 | public function updateUserscript() : void { |
||
| 91 | $baseFileName = './public/userscripts/manga-tracker.user.js'; |
||
| 92 | $baseDomain = 'trackr.moe'; |
||
| 93 | if(file_exists('./public/userscripts/manga-tracker.dev.user.js')) { |
||
| 94 | $baseFileName = './public/userscripts/manga-tracker.dev.user.js'; |
||
| 95 | $baseDomain = 'manga-tracker.localhost:20180'; |
||
| 96 | } |
||
| 97 | |||
| 98 | $baseFile = file_get_contents($baseFileName); |
||
| 99 | |||
| 100 | $parse = parse_url($this->baseURL); |
||
| 101 | if(strpos($baseFile, $parse['host']) !== false) die("Domain already exists in userscript?"); |
||
| 102 | |||
| 103 | preg_match('/\@updated ([0-9\-]+)[\r\n]+.*?\@version ([0-9\.]+)/s', $baseFile, $matches); |
||
| 104 | |||
| 105 | //Add @include |
||
| 106 | $include = '// @include /^'.str_replace('https', 'https?', preg_replace('/([\/\.])/', '\\\\$1', $this->baseURL)).'\/read\/.*?\/[a-z]+\/[0-9]+\/[0-9]+(\/.*)?$/'; |
||
| 107 | $baseFile = str_replace('// @updated', "{$include}\r\n// @updated", $baseFile); |
||
| 108 | |||
| 109 | //Update @updated |
||
| 110 | $currentDate = date("Y-m-d", time()); |
||
| 111 | $baseFile = str_replace("@updated {$matches[1]}","@updated {$currentDate}", $baseFile); |
||
| 112 | |||
| 113 | //Update @version |
||
| 114 | $currentVersion = explode('.', $matches[2]); |
||
| 115 | $newVersion = "{$currentVersion[0]}.{$currentVersion[1]}.". (((int) $currentVersion[2]) + 1); |
||
| 116 | $baseFile = str_replace("@version {$matches[2]}","@version {$newVersion}", $baseFile); |
||
| 117 | |||
| 118 | //Add @require |
||
| 119 | // @resource fontAwesome |
||
| 120 | $require = <<<EOT |
||
| 121 | // @require https://{$baseDomain}/userscripts/sites/{$this->className}.1.js |
||
| 122 | // @resource fontAwesome |
||
| 123 | EOT; |
||
| 124 | $baseFile = str_replace('// @resource fontAwesome', $require, $baseFile); |
||
| 125 | |||
| 126 | file_put_contents($baseFileName, $baseFile); |
||
| 127 | |||
| 128 | //Update .meta.js |
||
| 129 | $baseFileMeta = file_get_contents('./public/userscripts/manga-tracker.meta.js'); |
||
| 130 | $baseFileMeta = str_replace("// @version {$matches[2]}", "// @version {$newVersion}", $baseFileMeta); |
||
| 131 | file_put_contents('./public/userscripts/manga-tracker.meta.js', $baseFileMeta); |
||
| 132 | |||
| 133 | |||
| 134 | // Create site js |
||
| 135 | $siteData = <<<EOT |
||
| 136 | (function(sites) { |
||
| 137 | /** |
||
| 138 | * {$this->className} (FoolSlide) |
||
| 139 | * @type {SiteObject} |
||
| 140 | */ |
||
| 141 | sites['{$parse['host']}'] = { |
||
| 142 | preInit : function(callback) { |
||
| 143 | this.setupFoolSlide(); |
||
| 144 | callback(); |
||
| 145 | } |
||
| 146 | }; |
||
| 147 | })(window.trackerSites = (window.trackerSites || {})); |
||
| 148 | |||
| 149 | EOT; |
||
| 150 | |||
| 151 | file_put_contents("./public/userscripts/sites/{$this->className}.js", $siteData); |
||
| 152 | } |
||
| 153 | |||
| 199 |