GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — develop (#401)
by Dane
03:08
created

OptionController::viewScripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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 Log;
28
use Alert;
29
use Javascript;
30
use Illuminate\Http\Request;
31
use Pterodactyl\Models\Service;
32
use Pterodactyl\Models\ServiceOption;
33
use Pterodactyl\Exceptions\DisplayException;
34
use Pterodactyl\Http\Controllers\Controller;
35
use Pterodactyl\Repositories\OptionRepository;
36
use Pterodactyl\Repositories\VariableRepository;
37
use Pterodactyl\Exceptions\DisplayValidationException;
38
39
class OptionController extends Controller
40
{
41
    /**
42
     * Handles request to view page for adding new option.
43
     *
44
     * @param  \Illuminate\Http\Request  $request
45
     * @return \Illuminate\View\View
46
     */
47
    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...
48
    {
49
        $services = Service::with('options')->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...
50
        Javascript::put(['services' => $services->keyBy('id')]);
51
52
        return view('admin.services.options.new', ['services' => $services]);
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.services.opt...rvices' => $services)); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 52 which is incompatible with the return type documented by Pterodactyl\Http\Control...ptionController::create of type Illuminate\View\View.
Loading history...
53
    }
54
55
    /**
56
     * Handles POST request to create a new option.
57
     *
58
     * @param  \Illuminate\Http\Request  $request
59
     * @return \Illuminate\Response\RedirectResponse
60
     */
61 View Code Duplication
    public function store(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...
62
    {
63
        $repo = new OptionRepository;
64
65
        try {
66
            $option = $repo->create($request->intersect([
67
                'service_id', 'name', 'description', 'tag',
68
                'docker_image', 'startup', 'config_from', 'config_startup',
69
                'config_logs', 'config_files', 'config_stop',
70
            ]));
71
            Alert::success('Successfully created new service option.')->flash();
72
73
            return redirect()->route('admin.services.option.view', $option->id);
74
        } catch (DisplayValidationException $ex) {
75
            return redirect()->route('admin.services.option.new')->withErrors(json_decode($ex->getMessage()))->withInput();
76
        } catch (DisplayException $ex) {
77
            Alert::danger($ex->getMessage())->flash();
78
        } catch (\Exception $ex) {
79
            Log::error($ex);
80
            Alert::danger('An unhandled exception occurred while attempting to create this service. This error has been logged.')->flash();
81
        }
82
83
        return redirect()->route('admin.services.option.new')->withInput();
84
    }
85
86
    /**
87
     * Handles POST request to create a new option variable.
88
     *
89
     * @param  \Illuminate\Http\Request  $request
90
     * @param  int                       $id
91
     * @return \Illuminate\Http\RedirectResponse
92
     */
93
    public function createVariable(Request $request, $id)
94
    {
95
        $repo = new VariableRepository;
96
97
        try {
98
            $variable = $repo->create($id, $request->intersect([
0 ignored issues
show
Unused Code introduced by
$variable 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...
99
                'name', 'description', 'env_variable',
100
                'default_value', 'options', 'rules',
101
            ]));
102
103
            Alert::success('New variable successfully assigned to this service option.')->flash();
104
        } catch (DisplayValidationException $ex) {
105
            return redirect()->route('admin.services.option.variables', $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...
106
        } catch (DisplayException $ex) {
107
            Alert::danger($ex->getMessage())->flash();
108
        } catch (\Exception $ex) {
109
            Log::error($ex);
110
            Alert::danger('An unhandled exception was encountered while attempting to process that request. This error has been logged.')->flash();
111
        }
112
113
        return redirect()->route('admin.services.option.variables', $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...
114
    }
115
116
    /**
117
     * Display option overview page.
118
     *
119
     * @param  \Illuminate\Http\Request  $request
120
     * @param  int                       $id
121
     * @return \Illuminate\View\View
122
     */
123
    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...
124
    {
125
        return view('admin.services.options.view', ['option' => ServiceOption::findOrFail($id)]);
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.services.opt...ion::findOrFail($id))); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 125 which is incompatible with the return type documented by Pterodactyl\Http\Control...ller::viewConfiguration of type Illuminate\View\View.
Loading history...
126
    }
127
128
    /**
129
     * Display variable overview page for a service option.
130
     *
131
     * @param  \Illuminate\Http\Request  $request
132
     * @param  int                       $id
133
     * @return \Illuminate\View\View
134
     */
135
    public function viewVariables(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...
136
    {
137
        return view('admin.services.options.variables', ['option' => ServiceOption::with('variables')->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...
Bug Compatibility introduced by
The expression view('admin.services.opt...s')->findOrFail($id))); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 137 which is incompatible with the return type documented by Pterodactyl\Http\Control...ntroller::viewVariables of type Illuminate\View\View.
Loading history...
138
    }
139
140
    /**
141
     * Display script management page for an option.
142
     *
143
     * @param  Request $request
144
     * @param  int     $id
145
     * @return \Illuminate\View\View
146
     */
147
    public function viewScripts(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...
148
    {
149
        return view('admin.services.options.scripts', ['option' => ServiceOption::findOrFail($id)]);
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.services.opt...ion::findOrFail($id))); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 149 which is incompatible with the return type documented by Pterodactyl\Http\Control...Controller::viewScripts of type Illuminate\View\View.
Loading history...
150
    }
151
152
    /**
153
     * Handles POST when editing a configration for a service option.
154
     *
155
     * @param  \Illuminate\Http\Request  $request
156
     * @param  int                       $id
157
     * @return \Illuminate\Http\RedirectResponse
158
     */
159
    public function editConfiguration(Request $request, $id)
160
    {
161
        $repo = new OptionRepository;
162
163
        try {
164
            if ($request->input('action') !== 'delete') {
165
                $repo->update($id, $request->intersect([
166
                    'name', 'description', 'tag', 'docker_image', 'startup',
167
                    'config_from', 'config_stop', 'config_logs', 'config_files', 'config_startup',
168
                ]));
169
                Alert::success('Service option configuration has been successfully updated.')->flash();
170
            } else {
171
                $option = ServiceOption::with('service')->where('id', $id)->first();
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...
172
                $repo->delete($id);
173
                Alert::success('Successfully deleted service option from the system.')->flash();
174
175
                return redirect()->route('admin.services.view', $option->service_id);
176
            }
177
        } catch (DisplayValidationException $ex) {
178
            return redirect()->route('admin.services.option.view', $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...
179
        } catch (DisplayException $ex) {
180
            Alert::danger($ex->getMessage())->flash();
181
        } catch (\Exception $ex) {
182
            Log::error($ex);
183
            Alert::danger('An unhandled exception occurred while attempting to perform that action. This error has been logged.')->flash();
184
        }
185
186
        return redirect()->route('admin.services.option.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...
187
    }
188
189
    /**
190
     * Handles POST when editing a configration for a service option.
191
     *
192
     * @param  \Illuminate\Http\Request  $request
193
     * @param  int                       $option
194
     * @param  int                       $variable
195
     * @return \Illuminate\Http\RedirectResponse
196
     */
197
    public function editVariable(Request $request, $option, $variable)
198
    {
199
        $repo = new VariableRepository;
200
201
        try {
202
            if ($request->input('action') !== 'delete') {
203
                $variable = $repo->update($variable, $request->intersect([
204
                    'name', 'description', 'env_variable',
205
                    'default_value', 'options', 'rules',
206
                ]));
207
                Alert::success("The service variable '{$variable->name}' has been updated.")->flash();
208
            } else {
209
                $repo->delete($variable);
210
                Alert::success('That service variable has been deleted.')->flash();
211
            }
212
        } catch (DisplayValidationException $ex) {
213
            return redirect()->route('admin.services.option.variables', $option)->withErrors(json_decode($ex->getMessage()));
0 ignored issues
show
Documentation introduced by
$option 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...
214
        } catch (DisplayException $ex) {
215
            Alert::danger($ex->getMessage())->flash();
216
        } catch (\Exception $ex) {
217
            Log::error($ex);
218
            Alert::danger('An unhandled exception was encountered while attempting to process that request. This error has been logged.')->flash();
219
        }
220
221
        return redirect()->route('admin.services.option.variables', $option);
0 ignored issues
show
Documentation introduced by
$option 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...
222
    }
223
224
    /**
225
     * Handles POST when updating scripts for a service option.
226
     *
227
     * @param  Request $request
228
     * @param  int     $id
229
     * @return \Illuminate\Response\RedirectResponse
230
     */
231 View Code Duplication
    public function updateScripts(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...
232
    {
233
        $repo = new OptionRepository;
234
235
        try {
236
            $repo->scripts($id, $request->only([
237
                'script_install', 'script_entry', 'script_container',
238
            ]));
239
            Alert::success('Successfully updated option scripts to be run when servers are installed.')->flash();
240
        } catch (DisplayValidationException $ex) {
241
            return redirect()->route('admin.services.option.scripts', $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...
242
        } catch (\Exception $ex) {
243
            Log::error($ex);
244
            Alert::danger('An unhandled exception was encountered while attempting to process that request. This error has been logged.')->flash();
245
        }
246
247
        return redirect()->route('admin.services.option.scripts', $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...
248
    }
249
}
250