GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 4f127b...a52d9e )
by Dane
02:59
created

IndexController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 79
Duplicated Lines 15.19 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 6
dl 12
loc 79
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPassword() 0 14 4
A getIndex() 12 12 2
B status() 0 27 6

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
 * Pterodactyl - Panel
4
 * Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>
5
 * Some Modifications (c) 2015 Dylan Seidt <[email protected]>.
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
namespace Pterodactyl\Http\Controllers\Base;
27
28
use Illuminate\Http\Request;
29
use Pterodactyl\Models\Server;
30
use Pterodactyl\Http\Controllers\Controller;
31
32
class IndexController extends Controller
33
{
34
    /**
35
     * Returns listing of user's servers.
36
     *
37
     * @param  \Illuminate\Http\Request  $request
38
     * @return \Illuminate\View\View
39
     */
40 View Code Duplication
    public function getIndex(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $servers = $request->user()->access()->with('user');
43
44
        if (! is_null($request->input('query'))) {
45
            $servers->search($request->input('query'));
46
        }
47
48
        return view('base.index', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('base.index', array....frontend.servers')))); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 48 which is incompatible with the return type documented by Pterodactyl\Http\Control...dexController::getIndex of type Illuminate\View\View.
Loading history...
49
            'servers' => $servers->paginate(config('pterodactyl.paginate.frontend.servers')),
50
        ]);
51
    }
52
53
    /**
54
     * Generate a random string.
55
     *
56
     * @param  \Illuminate\Http\Request  $request
57
     * @param  int                       $length
58
     * @return string
59
     * @deprecated
60
     */
61
    public function getPassword(Request $request, $length = 16)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        $length = ($length < 8) ? 8 : $length;
64
65
        $returnable = false;
66
        while (! $returnable) {
67
            $generated = str_random($length);
68
            if (preg_match('/[A-Z]+[a-z]+[0-9]+/', $generated)) {
69
                $returnable = true;
70
            }
71
        }
72
73
        return $generated;
0 ignored issues
show
Bug introduced by
The variable $generated 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...
74
    }
75
76
    /**
77
     * Returns status of the server in a JSON response used for populating active status list.
78
     *
79
     * @param  \Illuminate\Http\Request  $request
80
     * @param  string                    $uuid
81
     * @return \Illuminate\Http\JsonResponse
82
     */
83
    public function status(Request $request, $uuid)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
    {
85
        $server = Server::byUuid($uuid);
86
87
        if (! $server) {
88
            return response()->json([], 404);
89
        }
90
91
        if (! $server->installed) {
92
            return response()->json(['status' => 20]);
93
        }
94
95
        if ($server->suspended) {
96
            return response()->json(['status' => 30]);
97
        }
98
99
        try {
100
            $res = $server->guzzleClient()->request('GET', '/server');
101
            if ($res->getStatusCode() === 200) {
102
                return response()->json(json_decode($res->getBody()));
103
            }
104
        } catch (\Exception $e) {
105
            //
106
        }
107
108
        return response()->json([]);
109
    }
110
}
111