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
Pull Request — develop (#401)
by Dane
03:08
created

ServersController   D

Complexity

Total Complexity 59

Size/Duplication

Total Lines 556
Duplicated Lines 19.24 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
wmc 59
lcom 1
cbo 15
dl 107
loc 556
rs 4.5454
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 12 2
A create() 0 16 1
B store() 0 21 5
A nodes() 0 23 1
A viewIndex() 0 4 1
A viewDetails() 0 6 1
A viewBuild() 0 10 1
B viewStartup() 0 28 1
A viewDatabase() 0 9 1
A viewManage() 0 4 1
A viewDelete() 0 4 1
A setDetails() 20 20 4
A setContainer() 20 20 4
A toggleInstall() 16 16 3
A reinstallServer() 0 16 3
A rebuildContainer() 0 18 2
B manageSuspension() 0 25 4
B updateBuild() 0 25 5
A delete() 21 21 4
B saveStartup() 0 26 6
A newDatabase() 0 19 4
A resetDatabasePassword() 15 15 2
A deleteDatabase() 15 15 2

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ServersController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ServersController, and based on these observations, apply Extract Interface, too.

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 Javascript;
30
use Pterodactyl\Models;
31
use Illuminate\Http\Request;
32
use GuzzleHttp\Exception\TransferException;
33
use Pterodactyl\Exceptions\DisplayException;
34
use Pterodactyl\Http\Controllers\Controller;
35
use Pterodactyl\Repositories\ServerRepository;
36
use Pterodactyl\Repositories\DatabaseRepository;
37
use Pterodactyl\Exceptions\DisplayValidationException;
38
39
class ServersController extends Controller
40
{
41
    /**
42
     * Display the index page with all servers currently on the system.
43
     *
44
     * @param  \Illuminate\Http\Request  $request
45
     * @return \Illuminate\View\View
46
     */
47
    public function index(Request $request)
48
    {
49
        $servers = Models\Server::with('node', 'user', 'allocation');
50
51
        if (! is_null($request->input('query'))) {
52
            $servers->search($request->input('query'));
53
        }
54
55
        return view('admin.servers.index', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.servers.inde...ervers->paginate(25))); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 55 which is incompatible with the return type documented by Pterodactyl\Http\Control...erversController::index of type Illuminate\View\View.
Loading history...
56
            'servers' => $servers->paginate(25),
0 ignored issues
show
Bug introduced by
The method paginate 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...
57
        ]);
58
    }
59
60
    /**
61
     * Display create new server page.
62
     *
63
     * @param  \Illuminate\Http\Request  $request
64
     * @return \Illuminate\View\View
65
     */
66
    public function create(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...
67
    {
68
        $services = Models\Service::with('options.packs', 'options.variables')->get();
0 ignored issues
show
Bug introduced by
The method get 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...
69
        Javascript::put([
70
            'services' => $services->map(function ($item) {
71
                return array_merge($item->toArray(), [
72
                    'options' => $item->options->keyBy('id')->toArray(),
73
                ]);
74
            })->keyBy('id'),
75
        ]);
76
77
        return view('admin.servers.new', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.servers.new'...rvices' => $services)); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 77 which is incompatible with the return type documented by Pterodactyl\Http\Control...rversController::create of type Illuminate\View\View.
Loading history...
78
            'locations' => Models\Location::all(),
79
            'services' => $services,
80
        ]);
81
    }
82
83
    /**
84
     * Create server controller method.
85
     *
86
     * @param  \Illuminate\Http\Request  $request
87
     * @return \Illuminate\Response\RedirectResponse
88
     */
89
    public function store(Request $request)
90
    {
91
        try {
92
            $repo = new ServerRepository;
93
            $server = $repo->create($request->except('_token'));
94
95
            return redirect()->route('admin.servers.view', $server->id);
96
        } catch (DisplayValidationException $ex) {
97
            return redirect()->route('admin.servers.new')->withErrors(json_decode($ex->getMessage()))->withInput();
98
        } catch (DisplayException $ex) {
99
            Alert::danger($ex->getMessage())->flash();
100
        } catch (TransferException $ex) {
101
            Log::warning($ex);
102
            Alert::danger('A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.')->flash();
103
        } catch (\Exception $ex) {
104
            Log::error($ex);
105
            Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
106
        }
107
108
        return redirect()->route('admin.servers.new')->withInput();
109
    }
110
111
    /**
112
     * Returns a tree of all avaliable nodes in a given location.
113
     *
114
     * @param  \Illuminate\Http\Request  $request
115
     * @return array
116
     */
117
    public function nodes(Request $request)
118
    {
119
        $nodes = Models\Node::with('allocations')->where('location_id', $request->input('location'))->get();
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...
120
121
        return $nodes->map(function ($item) {
122
            $filtered = $item->allocations->where('server_id', null)->map(function ($map) {
123
                return collect($map)->only(['id', 'ip', 'port']);
124
            });
125
126
            $item->ports = $filtered->map(function ($map) use ($item) {
127
                return [
128
                    'id' => $map['id'],
129
                    'text' => $map['ip'] . ':' . $map['port'],
130
                ];
131
            })->values();
132
133
            return [
134
                'id' => $item->id,
135
                'text' => $item->name,
136
                'allocations' => $item->ports,
137
            ];
138
        })->values();
139
    }
140
141
    /**
142
     * Display the index when viewing a specific server.
143
     *
144
     * @param  \Illuminate\Http\Request  $request
145
     * @param  int                       $id
146
     * @return \Illuminate\View\View
147
     */
148
    public function viewIndex(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...
149
    {
150
        return view('admin.servers.view.index', ['server' => Models\Server::findOrFail($id)]);
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.servers.view...ver::findOrFail($id))); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 150 which is incompatible with the return type documented by Pterodactyl\Http\Control...rsController::viewIndex of type Illuminate\View\View.
Loading history...
151
    }
152
153
    /**
154
     * Display the details page when viewing a specific server.
155
     *
156
     * @param  \Illuminate\Http\Request  $request
157
     * @param  int                       $id
158
     * @return \Illuminate\View\View
159
     */
160
    public function viewDetails(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...
161
    {
162
        $server = Models\Server::where('installed', 1)->findOrFail($id);
163
164
        return view('admin.servers.view.details', ['server' => $server]);
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.servers.view...('server' => $server)); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 164 which is incompatible with the return type documented by Pterodactyl\Http\Control...Controller::viewDetails of type Illuminate\View\View.
Loading history...
165
    }
166
167
    /**
168
     * Display the build details page when viewing a specific server.
169
     *
170
     * @param  \Illuminate\Http\Request  $request
171
     * @param  int                       $id
172
     * @return \Illuminate\View\View
173
     */
174
    public function viewBuild(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...
175
    {
176
        $server = Models\Server::where('installed', 1)->with('node.allocations')->findOrFail($id);
177
178
        return view('admin.servers.view.build', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.servers.view...port')->sortBy('ip'))); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 178 which is incompatible with the return type documented by Pterodactyl\Http\Control...rsController::viewBuild of type Illuminate\View\View.
Loading history...
179
            'server' => $server,
180
            'assigned' => $server->node->allocations->where('server_id', $server->id)->sortBy('port')->sortBy('ip'),
181
            'unassigned' => $server->node->allocations->where('server_id', null)->sortBy('port')->sortBy('ip'),
182
        ]);
183
    }
184
185
    /**
186
     * Display startup configuration page for a server.
187
     *
188
     * @param  \Illuminate\Http\Request  $request
189
     * @param  int                       $id
190
     * @return \Illuminate\View\View
191
     */
192
    public function viewStartup(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...
193
    {
194
        $server = Models\Server::where('installed', 1)->with('option.variables', 'variables')->findOrFail($id);
195
        $server->option->variables->transform(function ($item, $key) use ($server) {
0 ignored issues
show
Unused Code introduced by
The parameter $key 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...
196
            $item->server_value = $server->variables->where('variable_id', $item->id)->pluck('variable_value')->first();
197
198
            return $item;
199
        });
200
201
        $services = Models\Service::with('options.packs', 'options.variables')->get();
0 ignored issues
show
Bug introduced by
The method get 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...
202
        Javascript::put([
203
            'services' => $services->map(function ($item) {
204
                return array_merge($item->toArray(), [
205
                    'options' => $item->options->keyBy('id')->toArray(),
206
                ]);
207
            })->keyBy('id'),
208
            'server_variables' => $server->variables->mapWithKeys(function ($item) {
209
                return ['env_' . $item->variable_id => [
210
                    'value' => $item->variable_value,
211
                ]];
212
            })->toArray(),
213
        ]);
214
215
        return view('admin.servers.view.startup', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.servers.view...rvices' => $services)); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 215 which is incompatible with the return type documented by Pterodactyl\Http\Control...Controller::viewStartup of type Illuminate\View\View.
Loading history...
216
            'server' => $server,
217
            'services' => $services,
218
        ]);
219
    }
220
221
    /**
222
     * Display the database management page for a specific server.
223
     *
224
     * @param  \Illuminate\Http\Request $request
225
     * @param  int                      $id
226
     * @return \Illuminate\View\View
227
     */
228
    public function viewDatabase(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...
229
    {
230
        $server = Models\Server::where('installed', 1)->with('databases.host')->findOrFail($id);
231
232
        return view('admin.servers.view.database', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.servers.view... 'server' => $server)); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 232 which is incompatible with the return type documented by Pterodactyl\Http\Control...ontroller::viewDatabase of type Illuminate\View\View.
Loading history...
233
            'hosts' => Models\DatabaseHost::all(),
234
            'server' => $server,
235
        ]);
236
    }
237
238
    /**
239
     * Display the management page when viewing a specific server.
240
     *
241
     * @param  \Illuminate\Http\Request  $request
242
     * @param  int                       $id
243
     * @return \Illuminate\View\View
244
     */
245
    public function viewManage(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...
246
    {
247
        return view('admin.servers.view.manage', ['server' => Models\Server::findOrFail($id)]);
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.servers.view...ver::findOrFail($id))); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 247 which is incompatible with the return type documented by Pterodactyl\Http\Control...sController::viewManage of type Illuminate\View\View.
Loading history...
248
    }
249
250
    /**
251
     * Display the deletion page for a server.
252
     *
253
     * @param  \Illuminate\Http\Request  $request
254
     * @param  int                       $id
255
     * @return \Illuminate\View\View
256
     */
257
    public function viewDelete(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...
258
    {
259
        return view('admin.servers.view.delete', ['server' => Models\Server::findOrFail($id)]);
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.servers.view...ver::findOrFail($id))); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 259 which is incompatible with the return type documented by Pterodactyl\Http\Control...sController::viewDelete of type Illuminate\View\View.
Loading history...
260
    }
261
262
    /**
263
     * Update the details for a server.
264
     *
265
     * @param  \Illuminate\Http\Request  $request
266
     * @param  int                       $id
267
     * @return \Illuminate\Http\RedirectResponse
268
     */
269 View Code Duplication
    public function setDetails(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...
270
    {
271
        $repo = new ServerRepository;
272
        try {
273
            $repo->updateDetails($id, $request->intersect([
274
                'owner_id', 'name', 'description', 'reset_token',
275
            ]));
276
277
            Alert::success('Server details were successfully updated.')->flash();
278
        } catch (DisplayValidationException $ex) {
279
            return redirect()->route('admin.servers.view.details', $id)->withErrors(json_decode($ex->getMessage()))->withInput();
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...
280
        } catch (DisplayException $ex) {
281
            Alert::danger($ex->getMessage())->flash();
282
        } catch (\Exception $ex) {
283
            Log::error($ex);
284
            Alert::danger('An unhandled exception occured while attemping to update this server. This error has been logged.')->flash();
285
        }
286
287
        return redirect()->route('admin.servers.view.details', $id)->withInput();
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...
288
    }
289
290
    /**
291
     * Set the new docker container for a server.
292
     *
293
     * @param  \Illuminate\Http\Request  $request
294
     * @param  int                       $id
295
     * @return \Illuminate\Http\RedirectResponse
296
     */
297 View Code Duplication
    public function setContainer(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...
298
    {
299
        $repo = new ServerRepository;
300
301
        try {
302
            $repo->updateContainer($id, $request->intersect('docker_image'));
303
304
            Alert::success('Successfully updated this server\'s docker image.')->flash();
305
        } catch (DisplayValidationException $ex) {
306
            return redirect()->route('admin.servers.view.details', $id)->withErrors(json_decode($ex->getMessage()))->withInput();
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...
307
        } catch (TransferException $ex) {
308
            Log::warning($ex);
309
            Alert::danger('A TransferException occured while attempting to update the container image. Is the daemon online? This error has been logged.');
310
        } catch (\Exception $ex) {
311
            Log::error($ex);
312
            Alert::danger('An unhandled exception occured while attemping to update this server\'s docker image. This error has been logged.')->flash();
313
        }
314
315
        return redirect()->route('admin.servers.view.details', $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...
316
    }
317
318
    /**
319
     * Toggles the install status for a server.
320
     *
321
     * @param  \Illuminate\Http\Request  $request
322
     * @param  int                       $id
323
     * @return \Illuminate\Http\RedirectResponse
324
     */
325 View Code Duplication
    public function toggleInstall(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...
326
    {
327
        $repo = new ServerRepository;
328
        try {
329
            $repo->toggleInstall($id);
330
331
            Alert::success('Server install status was successfully toggled.')->flash();
332
        } catch (DisplayException $ex) {
333
            Alert::danger($ex->getMessage())->flash();
334
        } catch (\Exception $ex) {
335
            Log::error($ex);
336
            Alert::danger('An unhandled exception occured while attemping to toggle this servers status. This error has been logged.')->flash();
337
        }
338
339
        return redirect()->route('admin.servers.view.manage', $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...
340
    }
341
342
    /**
343
     * Reinstalls the server with the currently assigned pack and service.
344
     *
345
     * @param  \Illuminate\Http\Request  $request
346
     * @param  int                       $id
347
     * @return \Illuminate\Http\RedirectResponse
348
     */
349
    public function reinstallServer(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...
350
    {
351
        $repo = new ServerRepository;
352
        try {
353
            $repo->reinstall($id);
354
355
            Alert::success('Server successfully marked for reinstallation.')->flash();
356
        } catch (DisplayException $ex) {
357
            Alert::danger($ex->getMessage())->flash();
358
        } catch (\Exception $ex) {
359
            Log::error($ex);
360
            Alert::danger('An unhandled exception occured while attemping to perform this reinstallation. This error has been logged.')->flash();
361
        }
362
363
        return redirect()->route('admin.servers.view.manage', $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...
364
    }
365
366
    /**
367
     * Setup a server to have a container rebuild.
368
     *
369
     * @param  \Illuminate\Http\Request  $request
370
     * @param  int                       $id
371
     * @return \Illuminate\Http\RedirectResponse
372
     */
373
    public function rebuildContainer(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...
374
    {
375
        $server = Models\Server::with('node')->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...
376
377
        try {
378
            $server->node->guzzleClient([
379
                'X-Access-Server' => $server->uuid,
380
                'X-Access-Token' => $server->node->daemonSecret,
381
            ])->request('POST', '/server/rebuild');
382
383
            Alert::success('A rebuild has been queued successfully. It will run the next time this server is booted.')->flash();
384
        } catch (TransferException $ex) {
385
            Log::warning($ex);
386
            Alert::danger('A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.')->flash();
387
        }
388
389
        return redirect()->route('admin.servers.view.manage', $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...
390
    }
391
392
    /**
393
     * Manage the suspension status for a server.
394
     *
395
     * @param  \Illuminate\Http\Request  $request
396
     * @param  int                       $id
397
     * @return \Illuminate\Http\RedirectResponse
398
     */
399
    public function manageSuspension(Request $request, $id)
400
    {
401
        $repo = new ServerRepository;
402
        $action = $request->input('action');
403
404
        if (! in_array($action, ['suspend', 'unsuspend'])) {
405
            Alert::danger('Invalid action was passed to function.')->flash();
406
407
            return redirect()->route('admin.servers.view.manage', $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...
408
        }
409
410
        try {
411
            $repo->toggleAccess($id, ($action === 'unsuspend'));
412
413
            Alert::success('Server has been ' . $action . 'ed.');
414
        } catch (TransferException $ex) {
415
            Log::warning($ex);
416
            Alert::danger('A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.')->flash();
417
        } catch (\Exception $ex) {
418
            Log::error($ex);
419
            Alert::danger('An unhandled exception occured while attemping to ' . $action . ' this server. This error has been logged.')->flash();
420
        }
421
422
        return redirect()->route('admin.servers.view.manage', $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...
423
    }
424
425
    /**
426
     * Update the build configuration for a server.
427
     *
428
     * @param  \Illuminate\Http\Request  $request
429
     * @param  int                       $id
430
     * @return \Illuminate\Http\RedirectResponse
431
     */
432
    public function updateBuild(Request $request, $id)
433
    {
434
        $repo = new ServerRepository;
435
436
        try {
437
            $repo->changeBuild($id, $request->intersect([
438
                'allocation_id', 'add_allocations', 'remove_allocations',
439
                'memory', 'swap', 'io', 'cpu',
440
            ]));
441
442
            Alert::success('Server details were successfully updated.')->flash();
443
        } catch (DisplayValidationException $ex) {
444
            return redirect()->route('admin.servers.view.build', $id)->withErrors(json_decode($ex->getMessage()))->withInput();
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...
445
        } catch (DisplayException $ex) {
446
            Alert::danger($ex->getMessage())->flash();
447
        } catch (TransferException $ex) {
448
            Log::warning($ex);
449
            Alert::danger('A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.')->flash();
450
        } catch (\Exception $ex) {
451
            Log::error($ex);
452
            Alert::danger('An unhandled exception occured while attemping to add this server. This error has been logged.')->flash();
453
        }
454
455
        return redirect()->route('admin.servers.view.build', $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...
456
    }
457
458
    /**
459
     * Start the server deletion process.
460
     *
461
     * @param  \Illuminate\Http\Request  $request
462
     * @param  int                       $id
463
     * @return \Illuminate\Http\RedirectResponse
464
     */
465 View Code Duplication
    public function delete(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...
466
    {
467
        $repo = new ServerRepository;
468
469
        try {
470
            $repo->delete($id, $request->has('force_delete'));
471
            Alert::success('Server was successfully deleted from the system.')->flash();
472
473
            return redirect()->route('admin.servers');
474
        } catch (DisplayException $ex) {
475
            Alert::danger($ex->getMessage())->flash();
476
        } catch (TransferException $ex) {
477
            Log::warning($ex);
478
            Alert::danger('A TransferException occurred while attempting to delete this server from the daemon, please ensure it is running. This error has been logged.')->flash();
479
        } catch (\Exception $ex) {
480
            Log::error($ex);
481
            Alert::danger('An unhandled exception occured while attemping to delete this server. This error has been logged.')->flash();
482
        }
483
484
        return redirect()->route('admin.servers.view.delete', $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...
485
    }
486
487
    /**
488
     * Update the startup command as well as variables.
489
     *
490
     * @param  \Illuminate\Http\Request  $request
491
     * @param  int                       $id
492
     * @return \Illuminate\Http\RedirectResponse
493
     */
494
    public function saveStartup(Request $request, $id)
495
    {
496
        $repo = new ServerRepository;
497
498
        try {
499
            if ($repo->updateStartup($id, $request->except('_token'), true)) {
500
                Alert::success('Service configuration successfully modfied for this server, reinstalling now.')->flash();
501
502
                return redirect()->route('admin.servers.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...
503
            } else {
504
                Alert::success('Startup variables were successfully modified and assigned for this server.')->flash();
505
            }
506
        } catch (DisplayValidationException $ex) {
507
            return redirect()->route('admin.servers.view.startup', $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...
508
        } catch (DisplayException $ex) {
509
            Alert::danger($ex->getMessage())->flash();
510
        } catch (TransferException $ex) {
511
            Log::warning($ex);
512
            Alert::danger('A TransferException occurred while attempting to update the startup for this server, please ensure the daemon is running. This error has been logged.')->flash();
513
        } catch (\Exception $ex) {
514
            Log::error($ex);
515
            Alert::danger('An unhandled exception occured while attemping to update startup variables for this server. This error has been logged.')->flash();
516
        }
517
518
        return redirect()->route('admin.servers.view.startup', $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...
519
    }
520
521
    /**
522
     * Creates a new database assigned to a specific server.
523
     *
524
     * @param  \Illuminate\Http\Request  $request
525
     * @param  int                       $id
526
     * @return \Illuminate\Http\RedirectResponse
527
     */
528
    public function newDatabase(Request $request, $id)
529
    {
530
        $repo = new DatabaseRepository;
531
532
        try {
533
            $repo->create($id, $request->only(['host', 'database', 'connection']));
534
535
            Alert::success('A new database was assigned to this server successfully.')->flash();
536
        } catch (DisplayValidationException $ex) {
537
            return redirect()->route('admin.servers.view.database', $id)->withInput()->withErrors(json_decode($ex->getMessage()))->withInput();
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...
538
        } catch (DisplayException $ex) {
539
            Alert::danger($ex->getMessage())->flash();
540
        } catch (\Exception $ex) {
541
            Log::error($ex);
542
            Alert::danger('An exception occured while attempting to add a new database for this server. This error has been logged.')->flash();
543
        }
544
545
        return redirect()->route('admin.servers.view.database', $id)->withInput();
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...
546
    }
547
548
    /**
549
     * Resets the database password for a specific database on this server.
550
     *
551
     * @param  \Illuminate\Http\Request  $request
552
     * @param  int                       $id
553
     * @return \Illuminate\Http\RedirectResponse
554
     */
555 View Code Duplication
    public function resetDatabasePassword(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...
556
    {
557
        $database = Models\Database::where('server_id', $id)->findOrFail($request->input('database'));
558
        $repo = new DatabaseRepository;
559
560
        try {
561
            $repo->password($database->id, str_random(20));
562
563
            return response('', 204);
564
        } catch (\Exception $ex) {
565
            Log::error($ex);
566
567
            return response()->json(['error' => 'A unhandled exception occurred while attempting to reset this password. This error has been logged.'], 503);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return response()->json(...s been logged.'), 503); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Pterodactyl\Http\Control...::resetDatabasePassword of type Illuminate\Http\RedirectResponse.

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...
568
        }
569
    }
570
571
    /**
572
     * Deletes a database from a server.
573
     *
574
     * @param  \Illuminate\Http\Request  $request
575
     * @param  int                       $id
576
     * @param  int                       $database
577
     * @return \Illuminate\Http\RedirectResponse
578
     */
579 View Code Duplication
    public function deleteDatabase(Request $request, $id, $database)
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...
580
    {
581
        $database = Models\Database::where('server_id', $id)->findOrFail($database);
582
        $repo = new DatabaseRepository;
583
584
        try {
585
            $repo->drop($database->id);
586
587
            return response('', 204);
588
        } catch (\Exception $ex) {
589
            Log::error($ex);
590
591
            return response()->json(['error' => 'A unhandled exception occurred while attempting to drop this database. This error has been logged.'], 503);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return response()->json(...s been logged.'), 503); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Pterodactyl\Http\Control...troller::deleteDatabase of type Illuminate\Http\RedirectResponse.

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...
592
        }
593
    }
594
}
595