Completed
Push — master ( 1385c4...94a8d3 )
by Nicolaas
01:48
created

GitRepoFinder::get_repos_with_auth()   D

Complexity

Conditions 20
Paths 23

Size

Total Lines 116
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 116
rs 4.7294
c 0
b 0
f 0
cc 20
eloc 69
nc 23
nop 2

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
4
5
class GitRepoFinder extends Object
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
8
    /**
9
     *
10
     *
11
     *
12
     * @var string
13
     */
14
    private static $_modules = array();
15
16
17
18
    /**
19
     * takes the preloaded modules and
20
     * adds any other ones you have listed on github
21
     */
22
23
    public static function get_all_repos($username = '', $getNamesWithPrefix = false)
24
    {
25
        # $oauth_token = GitRepoFinder::Config()->get('github_oauth_token');
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
27
        self::get_all_repos_no_oauth($username, $getNamesWithPrefix);
28
        if (! self::$_modules) {
29
            self::get_repos_with_auth($username, $getNamesWithPrefix);
30
        }
31
        return self::$_modules;
32
    }
33
34
    public static function get_all_repos_no_oauth($username = '', $getNamesWithPrefix = false)
35
    {
36
        $preSelected = Config::inst()->get('UpdateModules', 'modules_to_update');
37
        if (is_array($preSelected) && count($preSelected)) {
38
            return $preSelected;
39
        } else {
40
            if (!$username) {
41
                $username = Config::inst()->get('GitHubModule', "github_user_name");
42
            }
43
            print "<li>Retrieving List of modules from GitHub for user $username ... </li>";
44
            if (! count(self::$_modules)) {
45
                for ($page = 0; $page < 10; $page++) {
46
                    $ch = curl_init();
47
                    curl_setopt($ch, CURLOPT_URL, "https://api.github.com/users/".$username."/repos?per_page=100&page=$page");
48
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
49
                    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, true);
50
                    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
51
52
53
                    $string = curl_exec($ch);
54
                    // close curl resource to free up system resources
55
                    curl_close($ch);
56
                    $array = json_decode($string, true);
57
                    $count = count($array);
58
                    if ($count > 0) {
59
                        foreach ($array as $repo) {
60
                            //dont bother about forks
61
                            if (isset($repo["fork"]) && !$repo["fork"]) {
62
                                //make sure we are the owners
63
                                if ($repo["owner"]["login"] == $username) {
64
                                    $isSSModule =  (stripos($repo["name"], 'silverstripe-')  !== false);
65
                                    //check it is silverstripe module
66
                                    if (!$getNamesWithPrefix) {
67
                                        $name = $repo["name"];
68
                                    } else {
69
                                        $name = preg_replace('/silverstripe/', "", $repo["name"], $limit = 1);
70
                                    }
71
72
                                    //if(strlen($name) < strlen($repo["name"])) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73
                                    if ($isSSModule) {
74
                                        //is it listed yet?
75
                                        if (!in_array($name, self::$_modules)) {
76
                                            self::$_modules[] = $name;
77
                                        }
78
                                    } else {
79
                                        DB::alteration_message("skipping ".$repo["name"]." as it does not appear to me a silverstripe module, you can add it manually to this task, using the configs ... ");
80
                                    }
81
                                } else {
82
                                    DB::alteration_message("skipping ".$repo["name"]." as it has a different owner");
83
                                }
84
                            } elseif (isset($repo["name"])) {
85
                                DB::alteration_message("skipping ".$repo["name"]." as it is a fork");
86
                            }
87
                        }
88
                    } else {
89
                        $page = 11;
90
                    }
91
                }
92
            }
93
            print "<li>Found ".count(self::$_modules)." modules on GitHub ... </li>";
94
            if (count(self::$_modules)==0) {
95
                user_error("No modules found on GitHub. This is possibly because the limit of 60 requests an hour has been exceeded.");
96
            }
97
        }
98
        return self::$_modules;
99
    }
100
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $preSelected of type array is incompatible with the declared type string of property $_modules.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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::OutputToScreen('Could not retrieve list of modules from GitHub');
163
164
                        UpdateModules::$unsolvedItems["all"] =  ('Could not retrieve list of modules from GitHub');
165
                        die('');
0 ignored issues
show
Coding Style Compatibility introduced by
The method get_repos_with_auth() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
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"])) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
196
                                if ($isSSModule) {
197
                                    //is it listed yet?
198
                                    if (!in_array($name, $modules)) {
199
                                        array_push($modules, $name);
200
                                    }
201
                                } else {
202
                                    GeneralMethods::OutputToScreen("skipping ".$repo["name"]." as it does not appear to me a silverstripe module");
203
                                }
204
                            } else {
205
                                GeneralMethods::OutputToScreen("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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $modules of type array is incompatible with the declared type string of property $_modules.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
213
            }
214
        }
215
        return self::$_modules;
216
    }
217
}
218