| Conditions | 13 |
| Paths | 13 |
| Total Lines | 54 |
| Code Lines | 40 |
| 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 declare(strict_types=1); |
||
| 90 | public static function checkVerModule(Helper $helper, ?string $source = null, ?string $default = null): ?array |
||
| 91 | {
|
||
| 92 | $source ??= 'github'; |
||
| 93 | $default ??= 'master'; |
||
| 94 | $moduleDirName = \basename(\dirname(__DIR__, 2)); |
||
| 95 | $moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
||
| 96 | $update = ''; |
||
| 97 | $repository = 'XoopsModules25x/' . $moduleDirName; |
||
| 98 | // $repository = 'XoopsModules25x/publisher'; //for testing only |
||
| 99 | $ret = null; |
||
| 100 | $infoReleasesUrl = "https://api.github.com/repos/$repository/releases"; |
||
| 101 | if ('github' === $source) {
|
||
| 102 | if (\function_exists('curl_init') && false !== ($curlHandle = \curl_init())) {
|
||
| 103 | \curl_setopt($curlHandle, \CURLOPT_URL, $infoReleasesUrl); |
||
| 104 | \curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true); |
||
| 105 | \curl_setopt($curlHandle, \CURLOPT_SSL_VERIFYPEER, true); //TODO: how to avoid an error when 'Peer's Certificate issuer is not recognized' |
||
| 106 | \curl_setopt($curlHandle, \CURLOPT_HTTPHEADER, ["User-Agent:Publisher\r\n"]); |
||
| 107 | $curlReturn = \curl_exec($curlHandle); |
||
| 108 | if (false === $curlReturn) {
|
||
| 109 | \trigger_error(\curl_error($curlHandle)); |
||
| 110 | } elseif (\is_string($curlReturn) && false !== \mb_strpos($curlReturn, 'Not Found')) {
|
||
| 111 | \trigger_error('Repository Not Found: ' . $infoReleasesUrl);
|
||
| 112 | } elseif (\is_string($curlReturn)) {
|
||
| 113 | $file = \json_decode($curlReturn, false, 512, JSON_THROW_ON_ERROR); |
||
| 114 | $latestVersionLink = \sprintf("https://github.com/$repository/archive/%s.zip", $file ? \reset($file)->tag_name : $default);
|
||
| 115 | $latestVersion = $file[0]->tag_name; |
||
| 116 | $prerelease = $file[0]->prerelease; |
||
| 117 | if ('master' !== $latestVersionLink) {
|
||
| 118 | $update = \constant('CO_' . $moduleDirNameUpper . '_' . 'NEW_VERSION') . $latestVersion;
|
||
| 119 | } |
||
| 120 | //"PHP-standardized" version |
||
| 121 | $latestVersion = \mb_strtolower((string)$latestVersion); |
||
| 122 | if (false !== mb_strpos($latestVersion, 'final')) {
|
||
| 123 | $latestVersion = \str_replace('_', '', \mb_strtolower($latestVersion));
|
||
| 124 | $latestVersion = \str_replace('final', '', \mb_strtolower($latestVersion));
|
||
| 125 | } |
||
| 126 | $moduleVersion = ($helper->getModule() |
||
|
|
|||
| 127 | ->getInfo('version') . '_' . $helper->getModule()
|
||
| 128 | ->getInfo('module_status'));
|
||
| 129 | //"PHP-standardized" version |
||
| 130 | $moduleVersion = \str_replace(' ', '', \mb_strtolower($moduleVersion));
|
||
| 131 | // $moduleVersion = '1.0'; //for testing only |
||
| 132 | // $moduleDirName = 'publisher'; //for testing only |
||
| 133 | if (!$prerelease && \version_compare($moduleVersion, $latestVersion, '<')) {
|
||
| 134 | $ret = []; |
||
| 135 | $ret[] = $update; |
||
| 136 | $ret[] = $latestVersionLink; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | \curl_close($curlHandle); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | return $ret; |
||
| 144 | } |
||
| 146 |