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 (#334)
by Dane
05:31 queued 02:47
created

NodesController::createAllocation()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 13
nc 7
nop 2
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 DB;
28
use Log;
29
use Alert;
30
use Javascript;
31
use Pterodactyl\Models;
32
use Illuminate\Http\Request;
33
use Pterodactyl\Exceptions\DisplayException;
34
use Pterodactyl\Http\Controllers\Controller;
35
use Pterodactyl\Repositories\NodeRepository;
36
use Pterodactyl\Exceptions\DisplayValidationException;
37
38
class NodesController extends Controller
39
{
40
    /**
41
     * Displays the index page listing all nodes on the panel.
42
     *
43
     * @param  Request $request
44
     * @return \Illuminate\View\View
45
     */
46 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...
47
    {
48
        $nodes = Models\Node::with('location')->withCount('servers');
0 ignored issues
show
Bug introduced by
The method withCount 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...
49
50
        if (! is_null($request->input('query'))) {
51
            $nodes->search($request->input('query'));
52
        }
53
54
        return view('admin.nodes.index', ['nodes' => $nodes->paginate(25)]);
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.nodes.index'...$nodes->paginate(25))); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 54 which is incompatible with the return type documented by Pterodactyl\Http\Control...\NodesController::index of type Illuminate\View\View.
Loading history...
55
    }
56
57
    /**
58
     * Displays create new node page.
59
     *
60
     * @param  Request $request
61
     * @return \Illuminate\View\View|\Illuminate\Response\RedirectResponse
62
     */
63
    public function new(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...
64
    {
65
        $locations = Models\Location::all();
66
        if (! $locations->count()) {
67
            Alert::warning('You must add a location before you can add a new node.')->flash();
68
69
            return redirect()->route('admin.locations');
70
        }
71
72
        return view('admin.nodes.new', ['locations' => $locations]);
73
    }
74
75
    /**
76
     * Post controller to create a new node on the system.
77
     *
78
     * @param  Request $request
79
     * @return \Illuminate\Response\RedirectResponse
80
     */
81 View Code Duplication
    public function create(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        try {
84
            $repo = new NodeRepository;
85
            $node = $repo->create($request->intersect([
86
                'name', 'location_id', 'public', 'fqdn', 'scheme', 'memory',
87
                'memory_overallocate', 'disk', 'disk_overallocate',
88
                'daemonBase', 'daemonSFTP', 'daemonListen',
89
            ]));
90
            Alert::success('Successfully created new node that can be configured automatically on your remote machine by visiting the configuration tab. <strong>Before you can add any servers you need to first assign some IP addresses and ports.</strong>')->flash();
91
92
            return redirect()->route('admin.nodes.view', $node->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Pterodactyl\Models\Node>. Since you implemented __get, maybe consider adding a @property annotation.

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

<?php

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

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

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

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

}

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

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

See also the PhpDoc documentation for @property.

Loading history...
93
        } catch (DisplayValidationException $e) {
94
            return redirect()->route('admin.nodes.new')->withErrors(json_decode($e->getMessage()))->withInput();
95
        } catch (DisplayException $e) {
96
            Alert::danger($e->getMessage())->flash();
97
        } catch (\Exception $e) {
98
            Log::error($e);
99
            Alert::danger('An unhandled exception occured while attempting to add this node. Please try again.')->flash();
100
        }
101
102
        return redirect()->route('admin.nodes.new')->withInput();
103
    }
104
105
    /**
106
     * Shows the index overview page for a specific node.
107
     *
108
     * @param  Request $request
109
     * @param  int     $id      The ID of the node to display information for.
110
     *
111
     * @return \Illuminate\View\View
112
     */
113
    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...
114
    {
115
        $node = Models\Node::with('location')->withCount('servers')->findOrFail($id);
0 ignored issues
show
Bug introduced by
The method withCount 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...
116
        $stats = collect(
117
            Models\Server::select(
0 ignored issues
show
Bug introduced by
The method select() does not exist on Pterodactyl\Models\Server. Did you maybe mean addSelectsToQuery()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
118
                DB::raw('SUM(memory) as memory, SUM(disk) as disk')
119
            )->where('node_id', $node->id)->first()
120
        )->mapWithKeys(function ($item, $key) use ($node) {
121
            $percent = ($item / $node->{$key}) * 100;
122
123
            return [$key => [
124
                'value' => $item,
125
                'percent' => $percent,
126
                'css' => ($percent <= 75) ? 'green' : (($percent > 90) ? 'red' : 'yellow'),
127
            ]];
128
        })->toArray();
129
130
        return view('admin.nodes.view.index', ['node' => $node, 'stats' => $stats]);
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.nodes.view.i...e, 'stats' => $stats)); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 130 which is incompatible with the return type documented by Pterodactyl\Http\Control...esController::viewIndex of type Illuminate\View\View.
Loading history...
131
    }
132
133
    /**
134
     * Shows the settings page for a specific node.
135
     *
136
     * @param  Request $request
137
     * @param  int     $id      The ID of the node to display information for.
138
     *
139
     * @return \Illuminate\View\View
140
     */
141
    public function viewSettings(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...
142
    {
143
        return view('admin.nodes.view.settings', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.nodes.view.s...dels\Location::all())); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 143 which is incompatible with the return type documented by Pterodactyl\Http\Control...ontroller::viewSettings of type Illuminate\View\View.
Loading history...
144
            'node' => Models\Node::findOrFail($id),
145
            'locations' => Models\Location::all(),
146
        ]);
147
    }
148
149
    /**
150
     * Shows the configuration page for a specific node.
151
     *
152
     * @param  Request $request
153
     * @param  int     $id      The ID of the node to display information for.
154
     *
155
     * @return \Illuminate\View\View
156
     */
157
    public function viewConfiguration(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...
158
    {
159
        return view('admin.nodes.view.configuration', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.nodes.view.c...ode::findOrFail($id))); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 159 which is incompatible with the return type documented by Pterodactyl\Http\Control...ller::viewConfiguration of type Illuminate\View\View.
Loading history...
160
            'node' => Models\Node::findOrFail($id),
161
        ]);
162
    }
163
164
    /**
165
     * Shows the allocation page for a specific node.
166
     *
167
     * @param  Request $request
168
     * @param  int     $id      The ID of the node to display information for.
169
     *
170
     * @return \Illuminate\View\View
171
     */
172
    public function viewAllocation(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...
173
    {
174
        $node = Models\Node::findOrFail($id);
175
        $node->setRelation('allocations', $node->allocations()->orderBy('ip', 'asc')->orderBy('port', 'asc')->with('server')->paginate(50));
176
177
        Javascript::put([
178
            'node' => collect($node)->only(['id']),
179
        ]);
180
181
        return view('admin.nodes.view.allocation', ['node' => $node]);
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.nodes.view.a...rray('node' => $node)); 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...troller::viewAllocation of type Illuminate\View\View.
Loading history...
182
    }
183
184
    /**
185
     * Shows the server listing page for a specific node.
186
     *
187
     * @param  Request $request
188
     * @param  int     $id      The ID of the node to display information for.
189
     *
190
     * @return \Illuminate\View\View
191
     */
192
    public function viewServers(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
        $node = Models\Node::with('servers.user', 'servers.service', 'servers.option')->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...
195
        Javascript::put([
196
            'node' => collect($node->makeVisible('daemonSecret'))->only(['scheme', 'fqdn', 'daemonListen', 'daemonSecret']),
197
        ]);
198
199
        return view('admin.nodes.view.servers', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.nodes.view.s...rray('node' => $node)); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 199 which is incompatible with the return type documented by Pterodactyl\Http\Control...Controller::viewServers of type Illuminate\View\View.
Loading history...
200
            'node' => $node,
201
        ]);
202
    }
203
204
    /**
205
     * Updates settings for a node.
206
     *
207
     * @param  Request $request
208
     * @param  int $node
0 ignored issues
show
Bug introduced by
There is no parameter named $node. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
209
     * @return \Illuminate\Http\RedirectResponse
210
     */
211 View Code Duplication
    public function updateSettings(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...
212
    {
213
        $repo = new NodeRepository;
214
215
        try {
216
            $repo->update($id, $request->intersect([
217
                'name', 'location_id', 'public', 'fqdn', 'scheme', 'memory',
218
                'memory_overallocate', 'disk', 'disk_overallocate', 'upload_size',
219
                'daemonSFTP', 'daemonListen', 'reset_secret',
220
            ]));
221
222
            Alert::success('Successfully updated this node\'s information. If you changed any daemon settings you will need to restart it now.')->flash();
223
        } catch (DisplayValidationException $ex) {
224
            return redirect()->route('admin.nodes.view.settings', $id)->withErrors(json_decode($ex->getMessage()))->withInput();
225
        } catch (DisplayException $ex) {
226
            Alert::danger($ex->getMessage())->flash();
227
        } catch (\Exception $ex) {
228
            Log::error($ex);
229
            Alert::danger('An unhandled exception occured while attempting to edit this node. Please try again.')->flash();
230
        }
231
232
        return redirect()->route('admin.nodes.view.settings', $id)->withInput();
233
    }
234
235
    /**
236
     * Removes a single allocation from a node.
237
     *
238
     * @param  Request $request
239
     * @param  int $node
240
     * @param  int $allocation [description]
241
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
242
     */
243
    public function allocationRemoveSingle(Request $request, $node, $allocation)
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...
244
    {
245
        $query = Models\Allocation::where('node_id', $node)->whereNull('server_id')->where('id', $allocation)->delete();
246
        if ($query < 1) {
247
            return response()->json([
248
                'error' => 'Unable to find an allocation matching those details to delete.',
249
            ], 400);
250
        }
251
252
        return response('', 204);
253
    }
254
255
    /**
256
     * Remove all allocations for a specific IP at once on a node.
257
     *
258
     * @param  Request $request
259
     * @param  int  $node
260
     * @return \Illuminate\Http\RedirectResponse
261
     */
262
    public function allocationRemoveBlock(Request $request, $node)
263
    {
264
        $query = Models\Allocation::where('node_id', $node)->whereNull('server_id')->where('ip', $request->input('ip'))->delete();
265
        if ($query < 1) {
266
            Alert::danger('There was an error while attempting to delete allocations on that IP.')->flash();
267
        } else {
268
            Alert::success('Deleted all unallocated ports for <code>' . $request->input('ip') . '</code>.')->flash();
269
        }
270
271
        return redirect()->route('admin.nodes.view.allocation', $node);
0 ignored issues
show
Documentation introduced by
$node 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...
272
    }
273
274
    /**
275
     * Sets an alias for a specific allocation on a node.
276
     *
277
     * @param  Request $request
278
     * @param  int $node
279
     * @return \Illuminate\Http\Response
280
     * @throws \Exception
281
     */
282
    public function allocationSetAlias(Request $request, $node)
0 ignored issues
show
Unused Code introduced by
The parameter $node 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...
283
    {
284
        if (! $request->input('allocation_id')) {
285
            return response('Missing required parameters.', 422);
286
        }
287
288
        try {
289
            $update = Models\Allocation::findOrFail($request->input('allocation_id'));
290
            $update->ip_alias = (empty($request->input('alias'))) ? null : $request->input('alias');
291
            $update->save();
292
293
            return response('', 204);
294
        } catch (\Exception $ex) {
295
            throw $ex;
296
        }
297
    }
298
299
    /**
300
     * Creates new allocations on a node.
301
     *
302
     * @param  Request $request
303
     * @param  int  $node
304
     * @return \Illuminate\Http\RedirectResponse
305
     */
306
    public function createAllocation(Request $request, $node)
307
    {
308
        $repo = new NodeRepository;
309
310
        try {
311
            $repo->addAllocations($node, $request->intersect(['allocation_ip', 'allocation_alias', 'allocation_ports']));
312
            Alert::success('Successfully added new allocations!')->flash();
313
        } catch (DisplayValidationException $ex) {
314
            return redirect()->route('admin.nodes.view.allocation', $node)->withErrors(json_decode($ex->getMessage()))->withInput();
0 ignored issues
show
Documentation introduced by
$node 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...
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 attempting to add allocations this node. This error has been logged.')->flash();
320
        }
321
322
        return redirect()->route('admin.nodes.view.allocation', $node);
0 ignored issues
show
Documentation introduced by
$node 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
     * Deletes a node from the system.
327
     *
328
     * @param  Request  $request
329
     * @param  int  $id
330
     * @return \Illuminate\Http\RedirectResponse
331
     */
332 View Code Duplication
    public function delete(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
333
    {
334
        $repo = new NodeRepository;
335
336
        try {
337
            $repo->delete($id);
338
            Alert::success('Successfully deleted the requested node from the panel.')->flash();
339
340
            return redirect()->route('admin.nodes');
341
        } catch (DisplayException $ex) {
342
            Alert::danger($ex->getMessage())->flash();
343
        } catch (\Exception $ex) {
344
            Log::error($ex);
345
            Alert::danger('An unhandled exception occured while attempting to delete this node. Please try again.')->flash();
346
        }
347
348
        return redirect()->route('admin.nodes.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...
349
    }
350
351
    /**
352
     * Returns the configuration token to auto-deploy a node.
353
     *
354
     * @param  Request  $request
355
     * @param  int  $id
356
     * @return \Illuminate\Http\JsonResponse
357
     */
358
    public function setToken(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...
359
    {
360
        $node = Models\Node::findOrFail($id);
0 ignored issues
show
Unused Code introduced by
$node is not used, you could remove the assignment.

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

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

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

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

Loading history...
361
362
        $t = Models\NodeConfigurationToken::create([
363
            'node_id' => $id,
364
            'token' => str_random(32),
365
        ]);
366
367
        return response()->json(['token' => $t->token]);
0 ignored issues
show
Documentation introduced by
The property token does not exist on object<Pterodactyl\Models\NodeConfigurationToken>. Since you implemented __get, maybe consider adding a @property annotation.

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

<?php

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

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

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

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

}

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

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

See also the PhpDoc documentation for @property.

Loading history...
368
    }
369
}
370