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