| Conditions | 20 |
| Paths | 23 |
| Total Lines | 116 |
| Lines | 9 |
| Ratio | 7.76 % |
| 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 |
||
| 101 | public static function get_repos_with_auth($username = '', $getNamesWithPrefix = false) |
||
| 102 | { |
||
| 103 | $preSelected = Config::inst()->get('UpdateModules', 'modules_to_update'); |
||
| 104 | if (is_array($preSelected) && count($preSelected)) { |
||
| 105 | self::$_modules = $preSelected; |
||
| 106 | } else { |
||
| 107 | if ($username) { |
||
| 108 | $gitUserName = $username; |
||
| 109 | } else { |
||
| 110 | $gitUserName = Config::inst()->get('GitHubModule', "github_user_name"); |
||
| 111 | } |
||
| 112 | print "<li>Retrieving List of modules from GitHub for user $username ... </li>"; |
||
| 113 | if (! count(self::$_modules)) { |
||
| 114 | $url = 'https://api.github.com/users/' . trim($gitUserName) . '/repos'; |
||
| 115 | $array = array(); |
||
| 116 | for ($page = 0; $page < 10; $page++) { |
||
| 117 | $data = array( |
||
| 118 | 'per_page' => 100, |
||
| 119 | 'page'=>$page |
||
| 120 | ); |
||
| 121 | |||
| 122 | $method = 'GET'; |
||
| 123 | $ch = curl_init($url); |
||
| 124 | $header = "Content-Type: application/json"; |
||
| 125 | |||
| 126 | if ($method == 'GET') { |
||
| 127 | $url .= '?'.http_build_query($data); |
||
| 128 | } |
||
| 129 | |||
| 130 | $gitApiUserName = trim(GitHubModule::Config()->get('git_api_login_username')); |
||
| 131 | $gitUserName = trim(GitHubModule::Config()->get('github_user_name')); |
||
| 132 | $gitApiUserPassword = trim(GitHubModule::Config()->get('git_api_login_password')); |
||
| 133 | |||
| 134 | $gitApiAccessToken = trim(GitHubModule::Config()->get('git_personal_access_token')); |
||
| 135 | if (trim($gitApiAccessToken)) { |
||
| 136 | $gitApiUserPassword = $gitApiAccessToken; |
||
| 137 | } |
||
| 138 | |||
| 139 | |||
| 140 | curl_setopt($ch, CURLOPT_VERBOSE, 1); |
||
| 141 | curl_setopt($ch, CURLOPT_URL, $url); |
||
| 142 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); |
||
| 143 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
| 144 | curl_setopt($ch, CURLOPT_HTTPHEADER, array($header)); |
||
| 145 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
||
| 146 | curl_setopt( |
||
| 147 | $ch, |
||
| 148 | CURLOPT_USERAGENT, |
||
| 149 | 'sunnysideupdevs' |
||
| 150 | ); |
||
| 151 | |||
| 152 | |||
| 153 | if (isset($gitApiUserName) && isset($gitApiUserPassword)) { |
||
| 154 | curl_setopt($ch, CURLOPT_USERPWD, $gitApiUserName . ':' . $gitApiUserPassword); |
||
| 155 | curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
||
| 156 | } |
||
| 157 | |||
| 158 | |||
| 159 | $curlResult = curl_exec($ch); |
||
| 160 | |||
| 161 | if (! $curlResult) { |
||
| 162 | GeneralMethods::output_to_screen('Could not retrieve list of modules from GitHub'); |
||
| 163 | |||
| 164 | UpdateModules::$unsolvedItems["all"] = ('Could not retrieve list of modules from GitHub'); |
||
| 165 | die(''); |
||
| 166 | } |
||
| 167 | |||
| 168 | $array = array_merge($array, json_decode($curlResult)); |
||
| 169 | } |
||
| 170 | |||
| 171 | |||
| 172 | $modules = array(); |
||
| 173 | |||
| 174 | if (count($array) > 0) { |
||
| 175 | foreach ($array as $repo) { |
||
| 176 | |||
| 177 | // see http://stackoverflow.com/questions/4345554/convert-php-object-to-associative-array |
||
| 178 | $repo = json_decode(json_encode($repo), true); |
||
| 179 | |||
| 180 | |||
| 181 | //dont bother about forks |
||
| 182 | if (isset($repo["fork"]) && !$repo["fork"]) { |
||
| 183 | //make sure we are the owners |
||
| 184 | |||
| 185 | if ($repo["owner"]["login"] == $gitUserName) { |
||
| 186 | $isSSModule = (stripos($repo["name"], 'silverstripe-') !== false); |
||
| 187 | //check it is silverstripe module |
||
| 188 | |||
| 189 | if (!$getNamesWithPrefix) { |
||
| 190 | $name = $repo["name"]; |
||
| 191 | } else { |
||
| 192 | $name = preg_replace('/silverstripe/', "", $repo["name"], $limit = 1); |
||
| 193 | } |
||
| 194 | |||
| 195 | //if(strlen($name) < strlen($repo["name"])) { |
||
| 196 | if ($isSSModule) { |
||
| 197 | //is it listed yet? |
||
| 198 | if (!in_array($name, $modules)) { |
||
| 199 | array_push($modules, $name); |
||
| 200 | } |
||
| 201 | } else { |
||
| 202 | GeneralMethods::output_to_screen("skipping ".$repo["name"]." as it does not appear to me a silverstripe module"); |
||
| 203 | } |
||
| 204 | } else { |
||
| 205 | GeneralMethods::output_to_screen("skipping ".$repo["name"]." as it has a different owner"); |
||
| 206 | } |
||
| 207 | } elseif (isset($repo["name"])) { |
||
| 208 | DB::alteration_message("skipping ".$repo["name"]." as it is a fork"); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | self::$_modules = $modules; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | return self::$_modules; |
||
| 216 | } |
||
| 217 | } |
||
| 218 |