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 ( 4e916c...5e2777 )
by Dane
02:47
created

UserController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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\API;
26
27
use Illuminate\Http\Request;
28
use Pterodactyl\Models\User;
29
use Dingo\Api\Exception\ResourceException;
30
use Pterodactyl\Exceptions\DisplayException;
31
use Pterodactyl\Repositories\UserRepository;
32
use Pterodactyl\Exceptions\DisplayValidationException;
33
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
34
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
35
36
class UserController extends BaseController
37
{
38
    /**
39
     * Lists all users currently on the system.
40
     *
41
     * @param  Request  $request
42
     * @return array
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 User::all()->toArray();
47
    }
48
49
    /**
50
     * Lists specific fields about a user or all fields pertaining to that user.
51
     *
52
     * @param  Request  $request
53
     * @param  int      $id
54
     * @return array
55
     */
56
    public function view(Request $request, $id)
57
    {
58
        $user = User::with('servers')->where((is_numeric($id) ? 'id' : 'email'), $id)->firstOrFail();
0 ignored issues
show
Bug introduced by
The method where 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...
59
60
        $user->servers->transform(function ($item) {
61
            return collect($item)->only([
62
                'id', 'node_id', 'uuidShort',
63
                'uuid', 'name', 'suspended',
64
                'owner_id',
65
            ]);
66
        });
67
68 View Code Duplication
        if (! is_null($request->input('fields'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
69
            $fields = explode(',', $request->input('fields'));
70
            if (! empty($fields) && is_array($fields)) {
71
                return collect($user)->only($fields);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return collect($user)->only($fields); (Illuminate\Support\Collection) is incompatible with the return type documented by Pterodactyl\Http\Control...PI\UserController::view of type array.

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...
72
            }
73
        }
74
75
        return $user->toArray();
76
    }
77
78
    /**
79
     * Create a New User.
80
     *
81
     * @param  Request  $request
82
     * @return array
83
     */
84 View Code Duplication
    public function create(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...
85
    {
86
        $repo = new UserRepository;
0 ignored issues
show
Unused Code introduced by
$repo 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...
87
88
        try {
89
            $user = $user->create($request->only([
0 ignored issues
show
Bug introduced by
The variable $user seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
90
                'email', 'password', 'name_first',
91
                'name_last', 'username', 'root_admin',
92
            ]));
93
94
            return ['id' => $user->id];
95
        } catch (DisplayValidationException $ex) {
96
            throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
97
        } catch (DisplayException $ex) {
98
            throw new ResourceException($ex->getMessage());
99
        } catch (\Exception $ex) {
100
            throw new ServiceUnavailableHttpException('Unable to create a user on the system due to an error.');
101
        }
102
    }
103
104
    /**
105
     * Update an Existing User.
106
     *
107
     * @param  Request  $request
108
     * @param  int      $id
109
     * @return array
110
     */
111 View Code Duplication
    public function update(Request $request, $id)
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...
112
    {
113
        $repo = new UserRepository;
114
115
        try {
116
            $user = $repo->update($id, $request->only([
0 ignored issues
show
Unused Code introduced by
$user 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...
117
                'email', 'password', 'name_first',
118
                'name_last', 'username', 'root_admin',
119
            ]));
120
121
            return ['id' => $id];
122
        } catch (DisplayValidationException $ex) {
123
            throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
124
        } catch (DisplayException $ex) {
125
            throw new ResourceException($ex->getMessage());
126
        } catch (\Exception $ex) {
127
            throw new ServiceUnavailableHttpException('Unable to update a user on the system due to an error.');
128
        }
129
    }
130
131
    /**
132
     * Delete a User.
133
     *
134
     * @param  Request  $request
135
     * @param  int      $id
136
     * @return void
137
     */
138 View Code Duplication
    public function delete(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...
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...
139
    {
140
        $repo = new UserRepository;
141
142
        try {
143
            $repo->delete($id);
144
145
            return $this->response->noContent();
146
        } catch (DisplayException $ex) {
147
            throw new ResourceException($ex->getMessage());
148
        } catch (\Exception $ex) {
149
            throw new ServiceUnavailableHttpException('Unable to delete this user due to an error.');
150
        }
151
    }
152
}
153