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