Conditions | 11 |
Paths | 12 |
Total Lines | 49 |
Code Lines | 36 |
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 |
||
127 | public static function checkVerModule($helper, $source = 'github', $default = 'master') |
||
128 | { |
||
129 | $moduleDirName = \basename(\dirname(__DIR__, 2)); |
||
130 | $moduleDirNameUpper = mb_strtoupper($moduleDirName); |
||
131 | $update = ''; |
||
132 | $repository = 'XoopsModules25x/' . $moduleDirName; |
||
133 | // $repository = 'XoopsModules25x/publisher'; //for testing only |
||
134 | $ret = ''; |
||
135 | $infoReleasesUrl = "https://api.github.com/repos/$repository/releases"; |
||
136 | if ('github' === $source) { |
||
137 | if (\function_exists('curl_init') && false !== ($curlHandle = \curl_init())) { |
||
138 | \curl_setopt($curlHandle, \CURLOPT_URL, $infoReleasesUrl); |
||
139 | \curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true); |
||
140 | \curl_setopt($curlHandle, \CURLOPT_SSL_VERIFYPEER, true); //TODO: how to avoid an error when 'Peer's Certificate issuer is not recognized' |
||
141 | \curl_setopt($curlHandle, \CURLOPT_HTTPHEADER, ["User-Agent:Publisher\r\n"]); |
||
142 | $curlReturn = \curl_exec($curlHandle); |
||
143 | if (false === $curlReturn) { |
||
144 | \trigger_error(\curl_error($curlHandle)); |
||
145 | } elseif (false !== \strpos($curlReturn, 'Not Found')) { |
||
146 | \trigger_error('Repository Not Found: ' . $infoReleasesUrl); |
||
147 | } else { |
||
148 | $file = json_decode($curlReturn, false); |
||
149 | $latestVersionLink = \sprintf("https://github.com/$repository/archive/%s.zip", $file ? \reset($file)->tag_name : $default); |
||
150 | $latestVersion = $file[0]->tag_name; |
||
151 | $prerelease = $file[0]->prerelease; |
||
152 | if ('master' !== $latestVersionLink) { |
||
153 | $update = \constant('CO_' . $moduleDirNameUpper . '_' . 'NEW_VERSION') . $latestVersion; |
||
154 | } |
||
155 | //"PHP-standardized" version |
||
156 | $latestVersion = mb_strtolower($latestVersion); |
||
157 | if (false !== mb_strpos($latestVersion, 'final')) { |
||
158 | $latestVersion = \str_replace('_', '', mb_strtolower($latestVersion)); |
||
159 | $latestVersion = \str_replace('final', '', mb_strtolower($latestVersion)); |
||
160 | } |
||
161 | $moduleVersion = ($helper->getModule()->getInfo('version') . '_' . $helper->getModule()->getInfo('module_status')); |
||
162 | //"PHP-standardized" version |
||
163 | $moduleVersion = \str_replace(' ', '', mb_strtolower($moduleVersion)); |
||
164 | // $moduleVersion = '1.0'; //for testing only |
||
165 | // $moduleDirName = 'publisher'; //for testing only |
||
166 | if (!$prerelease && \version_compare($moduleVersion, $latestVersion, '<')) { |
||
167 | $ret = []; |
||
168 | $ret[] = $update; |
||
169 | $ret[] = $latestVersionLink; |
||
170 | } |
||
171 | } |
||
172 | \curl_close($curlHandle); |
||
173 | } |
||
174 | } |
||
175 | return $ret; |
||
176 | } |
||
178 |