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 (#371)
by Dane
05:00 queued 02:14
created

ServersController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
/**
3
 * Pterodactyl - Panel
4
 * Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>.
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in all
14
 * copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24
25
namespace Pterodactyl\Http\Controllers\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
        return view('admin.servers.view.startup', ['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 201 which is incompatible with the return type documented by Pterodactyl\Http\Control...Controller::viewStartup of type Illuminate\View\View.
Loading history...
202
    }
203
204
    /**
205
     * Display the database management page for a specific server.
206
     *
207
     * @param  \Illuminate\Http\Request $request
208
     * @param  int                      $id
209
     * @return \Illuminate\View\View
210
     */
211
    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...
212
    {
213
        $server = Models\Server::where('installed', 1)->with('databases.host')->findOrFail($id);
214
215
        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 215 which is incompatible with the return type documented by Pterodactyl\Http\Control...ontroller::viewDatabase of type Illuminate\View\View.
Loading history...
216
            'hosts' => Models\DatabaseHost::all(),
217
            'server' => $server,
218
        ]);
219
    }
220
221
    /**
222
     * Display the management page when viewing a specific server.
223
     *
224
     * @param  \Illuminate\Http\Request  $request
225
     * @param  int                       $id
226
     * @return \Illuminate\View\View
227
     */
228
    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...
229
    {
230
        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 230 which is incompatible with the return type documented by Pterodactyl\Http\Control...sController::viewManage of type Illuminate\View\View.
Loading history...
231
    }
232
233
    /**
234
     * Display the deletion page for a server.
235
     *
236
     * @param  \Illuminate\Http\Request  $request
237
     * @param  int                       $id
238
     * @return \Illuminate\View\View
239
     */
240
    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...
241
    {
242
        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 242 which is incompatible with the return type documented by Pterodactyl\Http\Control...sController::viewDelete of type Illuminate\View\View.
Loading history...
243
    }
244
245
    /**
246
     * Update the details for a server.
247
     *
248
     * @param  \Illuminate\Http\Request  $request
249
     * @param  int                       $id
250
     * @return \Illuminate\Http\RedirectResponse
251
     */
252 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...
253
    {
254
        $repo = new ServerRepository;
255
        try {
256
            $repo->updateDetails($id, $request->intersect([
257
                'owner_id', 'name', 'description', 'reset_token',
258
            ]));
259
260
            Alert::success('Server details were successfully updated.')->flash();
261
        } catch (DisplayValidationException $ex) {
262
            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...
263
        } catch (DisplayException $ex) {
264
            Alert::danger($ex->getMessage())->flash();
265
        } catch (\Exception $ex) {
266
            Log::error($ex);
267
            Alert::danger('An unhandled exception occured while attemping to update this server. This error has been logged.')->flash();
268
        }
269
270
        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...
271
    }
272
273
    /**
274
     * Set the new docker container for a server.
275
     *
276
     * @param  \Illuminate\Http\Request  $request
277
     * @param  int                       $id
278
     * @return \Illuminate\Http\RedirectResponse
279
     */
280 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...
281
    {
282
        $repo = new ServerRepository;
283
284
        try {
285
            $repo->updateContainer($id, $request->intersect('docker_image'));
286
287
            Alert::success('Successfully updated this server\'s docker image.')->flash();
288
        } catch (DisplayValidationException $ex) {
289
            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...
290
        } catch (TransferException $ex) {
291
            Log::warning($ex);
292
            Alert::danger('A TransferException occured while attempting to update the container image. Is the daemon online? This error has been logged.');
293
        } catch (\Exception $ex) {
294
            Log::error($ex);
295
            Alert::danger('An unhandled exception occured while attemping to update this server\'s docker image. This error has been logged.')->flash();
296
        }
297
298
        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...
299
    }
300
301
    /**
302
     * Toggles the install status for a server.
303
     *
304
     * @param  \Illuminate\Http\Request  $request
305
     * @param  int                       $id
306
     * @return \Illuminate\Http\RedirectResponse
307
     */
308 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...
309
    {
310
        $repo = new ServerRepository;
311
        try {
312
            $repo->toggleInstall($id);
313
314
            Alert::success('Server install status was successfully toggled.')->flash();
315
        } catch (DisplayException $ex) {
316
            Alert::danger($ex->getMessage())->flash();
317
        } catch (\Exception $ex) {
318
            Log::error($ex);
319
            Alert::danger('An unhandled exception occured while attemping to toggle this servers status. This error has been logged.')->flash();
320
        }
321
322
        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...
323
    }
324
325
    /**
326
     * Setup a server to have a container rebuild.
327
     *
328
     * @param  \Illuminate\Http\Request  $request
329
     * @param  int                       $id
330
     * @return \Illuminate\Http\RedirectResponse
331
     */
332
    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...
333
    {
334
        $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...
335
336
        try {
337
            $server->node->guzzleClient([
338
                'X-Access-Server' => $server->uuid,
339
                'X-Access-Token' => $server->node->daemonSecret,
340
            ])->request('POST', '/server/rebuild');
341
342
            Alert::success('A rebuild has been queued successfully. It will run the next time this server is booted.')->flash();
343
        } catch (TransferException $ex) {
344
            Log::warning($ex);
345
            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();
346
        }
347
348
        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...
349
    }
350
351
    /**
352
     * Manage the suspension status for a server.
353
     *
354
     * @param  \Illuminate\Http\Request  $request
355
     * @param  int                       $id
356
     * @return \Illuminate\Http\RedirectResponse
357
     */
358
    public function manageSuspension(Request $request, $id)
359
    {
360
        $repo = new ServerRepository;
361
        $action = $request->input('action');
362
363
        if (! in_array($action, ['suspend', 'unsuspend'])) {
364
            Alert::danger('Invalid action was passed to function.')->flash();
365
366
            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...
367
        }
368
369
        try {
370
            $repo->$action($id);
371
372
            Alert::success('Server has been ' . $action . 'ed.');
373
        } catch (TransferException $ex) {
374
            Log::warning($ex);
375
            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();
376
        } catch (\Exception $ex) {
377
            Log::error($ex);
378
            Alert::danger('An unhandled exception occured while attemping to ' . $action . ' this server. This error has been logged.')->flash();
379
        }
380
381
        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...
382
    }
383
384
    /**
385
     * Update the build configuration for a server.
386
     *
387
     * @param  \Illuminate\Http\Request  $request
388
     * @param  int                       $id
389
     * @return \Illuminate\Http\RedirectResponse
390
     */
391
    public function updateBuild(Request $request, $id)
392
    {
393
        $repo = new ServerRepository;
394
395
        try {
396
            $repo->changeBuild($id, $request->intersect([
397
                'allocation_id', 'add_allocations', 'remove_allocations',
398
                'memory', 'swap', 'io', 'cpu',
399
            ]));
400
401
            Alert::success('Server details were successfully updated.')->flash();
402
        } catch (DisplayValidationException $ex) {
403
            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...
404
        } catch (DisplayException $ex) {
405
            Alert::danger($ex->getMessage())->flash();
406
        } catch (TransferException $ex) {
407
            Log::warning($ex);
408
            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();
409
        } catch (\Exception $ex) {
410
            Log::error($ex);
411
            Alert::danger('An unhandled exception occured while attemping to add this server. This error has been logged.')->flash();
412
        }
413
414
        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...
415
    }
416
417
    /**
418
     * Start the server deletion process.
419
     *
420
     * @param  \Illuminate\Http\Request  $request
421
     * @param  int                       $id
422
     * @return \Illuminate\Http\RedirectResponse
423
     */
424 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...
425
    {
426
        $repo = new ServerRepository;
427
428
        try {
429
            $repo->delete($id, $request->has('force_delete'));
430
            Alert::success('Server was successfully deleted from the system.')->flash();
431
432
            return redirect()->route('admin.servers');
433
        } catch (DisplayException $ex) {
434
            Alert::danger($ex->getMessage())->flash();
435
        } catch (TransferException $ex) {
436
            Log::warning($ex);
437
            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();
438
        } catch (\Exception $ex) {
439
            Log::error($ex);
440
            Alert::danger('An unhandled exception occured while attemping to delete this server. This error has been logged.')->flash();
441
        }
442
443
        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...
444
    }
445
446
    /**
447
     * Update the startup command as well as variables.
448
     *
449
     * @param  \Illuminate\Http\Request  $request
450
     * @param  int                       $id
451
     * @return \Illuminate\Http\RedirectResponse
452
     */
453
    public function saveStartup(Request $request, $id)
454
    {
455
        $repo = new ServerRepository;
456
457
        try {
458
            $repo->updateStartup($id, $request->except('_token'), true);
459
460
            Alert::success('Startup variables were successfully modified and assigned for this server.')->flash();
461
        } catch (DisplayValidationException $ex) {
462
            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...
463
        } catch (DisplayException $ex) {
464
            Alert::danger($ex->getMessage())->flash();
465
        } catch (TransferException $ex) {
466
            Log::warning($ex);
467
            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();
468
        } catch (\Exception $ex) {
469
            Log::error($ex);
470
            Alert::danger('An unhandled exception occured while attemping to update startup variables for this server. This error has been logged.')->flash();
471
        }
472
473
        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...
474
    }
475
476
    /**
477
     * Creates a new database assigned to a specific server.
478
     *
479
     * @param  \Illuminate\Http\Request  $request
480
     * @param  int                       $id
481
     * @return \Illuminate\Http\RedirectResponse
482
     */
483
    public function newDatabase(Request $request, $id)
484
    {
485
        $repo = new DatabaseRepository;
486
487
        try {
488
            $repo->create($id, $request->only(['host', 'database', 'connection']));
489
490
            Alert::success('A new database was assigned to this server successfully.')->flash();
491
        } catch (DisplayValidationException $ex) {
492
            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...
493
        } catch (DisplayException $ex) {
494
            Alert::danger($ex->getMessage())->flash();
495
        } catch (\Exception $ex) {
496
            Log::error($ex);
497
            Alert::danger('An exception occured while attempting to add a new database for this server. This error has been logged.')->flash();
498
        }
499
500
        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...
501
    }
502
503
    /**
504
     * Resets the database password for a specific database on this server.
505
     *
506
     * @param  \Illuminate\Http\Request  $request
507
     * @param  int                       $id
508
     * @return \Illuminate\Http\RedirectResponse
509
     */
510 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...
511
    {
512
        $database = Models\Database::where('server_id', $id)->findOrFail($request->input('database'));
513
        $repo = new DatabaseRepository;
514
515
        try {
516
            $repo->password($database->id, str_random(20));
517
518
            return response('', 204);
519
        } catch (\Exception $ex) {
520
            Log::error($ex);
521
522
            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...
523
        }
524
    }
525
526
    /**
527
     * Deletes a database from a server.
528
     *
529
     * @param  \Illuminate\Http\Request  $request
530
     * @param  int                       $id
531
     * @param  int                       $database
532
     * @return \Illuminate\Http\RedirectResponse
533
     */
534 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...
535
    {
536
        $database = Models\Database::where('server_id', $id)->findOrFail($database);
537
        $repo = new DatabaseRepository;
538
539
        try {
540
            $repo->drop($database->id);
541
542
            return response('', 204);
543
        } catch (\Exception $ex) {
544
            Log::error($ex);
545
546
            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...
547
        }
548
    }
549
}
550