Completed
Branch master (bb48cc)
by vijay
148:50 queued 92:39
created

GithubController   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 184
Duplicated Lines 15.22 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
c 1
b 0
f 0
lcom 2
cbo 6
dl 28
loc 184
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A authenticate() 0 16 2
A authForSpecificApp() 0 13 2
A listRepositories() 10 10 2
B getReleaseByTag() 0 27 6
A getReleaseById() 9 9 2
A getDownloadCount() 9 9 2
A download() 0 14 3
A getSettings() 0 8 2
A postSettings() 0 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\Http\Controllers\Github;
4
5
use Illuminate\Http\Request;
6
use App\Http\Requests;
7
use App\Http\Controllers\Controller;
8
use App\Http\Controllers\Github\GithubApiController;
9
use Exception;
10
use App\Model\Github\Github;
11
12
class GithubController extends Controller {
13
14
    public $github_api;
15
    public $client_id;
16
    public $client_secret;
17
    public $github;
18
19
    public function __construct() {
20
21
        $this->middleware('auth');
22
23
        $github_controller = new GithubApiController();
24
        $this->github_api = $github_controller;
25
26
        $model = new Github();
27
        $this->github = $model->firstOrFail();
28
   
29
        $this->client_id = $this->github->client_id;
30
        $this->client_secret = $this->github->client_secret;
31
    }
32
33
    /**
34
     * Authenticate a user entirly
35
     * @return type
36
     */
37
    public function authenticate() {
38
        try {
39
            $url = "https://api.github.com/user";
40
            $data = array("bio" => "This is my bio");
41
            $data_string = json_encode($data);
42
            $auth = $this->github_api->postCurl($url, $data_string);
43
            return $auth;
44
//            if($auth!='true'){
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
45
//                throw new Exception('can not authenticate with github', 401);
46
//            }
47
            //$authenticated = json_decode($auth);
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
48
            //dd($authenticated);
49
        } catch (Exception $ex) {
50
            return redirect('/')->with('fails', $ex->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return redirect('/')->wi...s', $ex->getMessage()); (Illuminate\Http\RedirectResponse) is incompatible with the return type documented by App\Http\Controllers\Git...ontroller::authenticate of type App\Http\Controllers\Github\type.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
51
        }
52
    }
53
54
    /**
55
     * Authenticate a user for a perticular application
56
     * @return type
57
     */
58
    public function authForSpecificApp() {
59
        try {
60
            $url = "https://api.github.com/authorizations/clients/$this->client_id";
61
            $data = array("client_secret" => "$this->client_secret");
62
            $data_string = json_encode($data);
63
            $method = "PUT";
64
            $auth = $this->github_api->postCurl($url, $data_string, $method);
65
            return $auth;
66
            //dd($auth);
67
        } catch (Exception $ex) {
68
            return redirect('/')->with('fails', $ex->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return redirect('/')->wi...s', $ex->getMessage()); (Illuminate\Http\RedirectResponse) is incompatible with the return type documented by App\Http\Controllers\Git...ler::authForSpecificApp of type App\Http\Controllers\Github\type.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
69
        } 
70
    }
71
72
    /**
73
     * List all release
74
     * @return type
75
     */
76 View Code Duplication
    public function listRepositories($owner,$repo) {
77
        try {
78
            $url = "https://api.github.com/repos/$owner/$repo/releases";
79
            $releases = $this->github_api->getCurl($url);
80
            //dd($releases);
81
            return $releases;
82
        } catch (Exception $ex) {
83
            return redirect('/')->with('fails', $ex->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return redirect('/')->wi...s', $ex->getMessage()); (Illuminate\Http\RedirectResponse) is incompatible with the return type documented by App\Http\Controllers\Git...oller::listRepositories of type App\Http\Controllers\Github\type.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
84
        }
85
    }
86
87
    /**
88
     * List only one release by tag
89
     * @param Request $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
90
     * @return type
91
     */
92
    public function getReleaseByTag($owner,$repo) {
93
        try {
94
            $tag = \Input::get('tag');
95
            $all_releases = $this->listRepositories($owner,$repo);
96
            //dd($all_releases);
97
            if ($tag) {
98
                foreach ($all_releases as $key => $release) {
99
                    //dd($release);
100
                    if (in_array($tag, $release)) {
101
102
                        $version[$tag] = $this->getReleaseById($release["id"]);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$version was never initialized. Although not strictly required by PHP, it is generally a good practice to add $version = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
103
                    }
104
                }
105
            } else {
106
                $version[0] = $all_releases[0];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$version was never initialized. Although not strictly required by PHP, it is generally a good practice to add $version = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
107
            }
108
            //dd($version);
109
            //execute download
110
            
111
            if($this->download($version)=="success"){
0 ignored issues
show
Bug introduced by
The variable $version does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Documentation introduced by
$version is of type array, but the function expects a object<App\Http\Controllers\Github\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
112
                return "success";
113
            }
114
            //return redirect()->back()->with('success', \Lang::get('message.downloaded-successfully'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
115
        } catch (Exception $ex) {
116
            return redirect('/')->with('fails', $ex->getMessage());
117
        }
118
    }
119
120
    /**
121
     * List only one release by id
122
     * @param type $id
123
     * @return type
124
     */
125 View Code Duplication
    public function getReleaseById($id) {
126
        try {
127
            $url = "https://api.github.com/repos/ladybirdweb/faveo-helpdesk/releases/$id";
128
            $releaseid = $this->github_api->getCurl($url);
129
            return $releaseid;
130
        } catch (Exception $ex) {
131
            return redirect('/')->with('fails', $ex->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return redirect('/')->wi...s', $ex->getMessage()); (Illuminate\Http\RedirectResponse) is incompatible with the return type documented by App\Http\Controllers\Git...troller::getReleaseById of type App\Http\Controllers\Github\type.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
132
        }
133
    }
134
135
    /**
136
     * Get the count of download of the release
137
     * @return array||redirect
0 ignored issues
show
Documentation introduced by
The doc-type array||redirect could not be parsed: Unknown type name "|" at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
138
     */
139 View Code Duplication
    public function getDownloadCount() {
140
        try {
141
            $url = "https://api.github.com/repos/ladybirdweb/faveo-helpdesk/downloads";
142
            $downloads = $this->github_api->getCurl($url);
143
            return $downloads;
144
        } catch (Exception $ex) {
145
            return redirect('/')->with('fails', $ex->getMessage());
146
        }
147
    }
148
149
    /**
150
     * 
151
     * @param type $release
152
     * @return type .zip file
153
     */
154
    public function download($release) {
155
        try {
156
            foreach ($release as $url) {
157
                $download_link = $url["zipball_url"];
158
            }
159
            echo "<form action=$download_link method=get name=download>";
0 ignored issues
show
Bug introduced by
The variable $download_link does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
160
            echo '</form>';
161
            echo"<script language='javascript'>document.download.submit();</script>";
162
            
163
            //return "success";
164
        } catch (Exception $ex) {
165
            return redirect('/')->with('fails', $ex->getMessage());
166
        }
167
    }
168
169
    /**
170
     * get the settings page for github 
171
     * @return view
172
     */
173
    public function getSettings() {
174
        try {
175
            $model = $this->github;
176
            return view('themes.default1.github.settings',  compact('model'));
177
        } catch (Exception $ex) {
178
            return redirect('/')->with('fails', $ex->getMessage());
179
        }
180
    }
181
    
182
    public function postSettings(Request $request){
183
         $this->validate($request, [
184
                "username"=>"required",
185
                "password"=>"required"
186
            ]);
187
        try{
188
            $this->github->fill($request->input())->save();
189
            return redirect()->back()->with('success',\Lang::get('message.updated-successfully'));
190
        } catch (Exception $ex) {
191
            return redirect()->back()->with('fails', $ex->getMessage());
192
        }   
193
    }
194
195
}
196