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.

ServersController::deleteDatabase()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Importance

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