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 ( db0720...05d2a6 )
by Dane
03:11
created

LocationController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 0
cbo 8
dl 0
loc 84
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 6 1
A view() 0 4 1
A create() 0 18 3
B update() 0 24 5
1
<?php
2
/**
3
 * Pterodactyl - Panel
4
 * Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>.
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in all
14
 * copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24
25
namespace Pterodactyl\Http\Controllers\Admin;
26
27
use Log;
28
use Alert;
29
use Illuminate\Http\Request;
30
use Pterodactyl\Models\Location;
31
use Pterodactyl\Exceptions\DisplayException;
32
use Pterodactyl\Http\Controllers\Controller;
33
use Pterodactyl\Repositories\LocationRepository;
34
use Pterodactyl\Exceptions\DisplayValidationException;
35
36
class LocationController extends Controller
37
{
38
    /**
39
     * Return the location overview page.
40
     *
41
     * @param  Request $request
42
     * @return \Illuminate\View\View
43
     */
44
    public function index(Request $request)
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...
45
    {
46
        return view('admin.locations.index', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.locations.in...', 'servers')->get())); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 46 which is incompatible with the return type documented by Pterodactyl\Http\Control...cationController::index of type Illuminate\View\View.
Loading history...
47
            'locations' => Location::withCount('nodes', 'servers')->get(),
48
        ]);
49
    }
50
51
    /**
52
     * Return the location view page.
53
     *
54
     * @param  Request $request
55
     * @param  int     $id
56
     * @return \Illuminate\View\View
57
     */
58
    public function view(Request $request, $id)
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...
59
    {
60
        return view('admin.locations.view', ['location' => Location::with('nodes.servers')->findOrFail($id)]);
0 ignored issues
show
Bug introduced by
The method findOrFail does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Bug Compatibility introduced by
The expression view('admin.locations.vi...s')->findOrFail($id))); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 60 which is incompatible with the return type documented by Pterodactyl\Http\Control...ocationController::view of type Illuminate\View\View.
Loading history...
61
    }
62
63
    /**
64
     * Handle request to create new location.
65
     *
66
     * @param  Request $request
67
     * @return \Illuminate\Response\RedirectResponse
68
     */
69
    public function create(Request $request)
70
    {
71
        $repo = new LocationRepository;
72
73
        try {
74
            $location = $repo->create($request->intersect(['short', 'long']));
75
            Alert::success('Location was created successfully.')->flash();
76
77
            return redirect()->route('admin.locations.view', $location->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Pterodactyl\Models\Location>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
78
        } catch (DisplayValidationException $ex) {
79
            return redirect()->route('admin.locations')->withErrors(json_decode($ex->getMessage()));
80
        } catch (\Exception $ex) {
81
            Log::error($ex);
82
            Alert::error('An unhandled exception occurred while processing this request. This error has been logged.')->flash();
83
        }
84
85
        return redirect()->route('admin.locations');
86
    }
87
88
    /**
89
     * Handle request to update or delete location.
90
     *
91
     * @param  Request $request
92
     * @param  int     $id
93
     * @return \Illuminate\Response\RedirectResponse
94
     */
95
    public function update(Request $request, $id)
96
    {
97
        $repo = new LocationRepository;
98
99
        try {
100
            if ($request->input('action') !== 'delete') {
101
                $location = $repo->update($id, $request->intersect(['short', 'long']));
0 ignored issues
show
Unused Code introduced by
$location is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
102
                Alert::success('Location was updated successfully.')->flash();
103
            } else {
104
                $repo->delete($id);
105
106
                return redirect()->route('admin.locations');
107
            }
108
        } catch (DisplayValidationException $ex) {
109
            return redirect()->route('admin.locations.view', $id)->withErrors(json_decode($ex->getMessage()));
0 ignored issues
show
Documentation introduced by
$id is of type integer, but the function expects a array.

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...
110
        } catch (DisplayException $ex) {
111
            Alert::danger($ex->getMessage())->flash();
112
        } catch (\Exception $ex) {
113
            Log::error($ex);
114
            Alert::error('An unhandled exception occurred while processing this request. This error has been logged.')->flash();
115
        }
116
117
        return redirect()->route('admin.locations.view', $id);
0 ignored issues
show
Documentation introduced by
$id is of type integer, but the function expects a array.

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...
118
    }
119
}
120