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
Push — develop ( 57a888...65f9e3 )
by Dane
16:31 queued 13:22
created

VariableRepository::update()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 46
Code Lines 27

Duplication

Lines 46
Ratio 100 %

Importance

Changes 0
Metric Value
dl 46
loc 46
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 27
nc 12
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\Repositories;
26
27
use DB;
28
use Validator;
29
use Pterodactyl\Models\ServiceOption;
30
use Pterodactyl\Models\ServiceVariable;
31
use Pterodactyl\Exceptions\DisplayException;
32
use Pterodactyl\Exceptions\DisplayValidationException;
33
34
class VariableRepository
35
{
36
    /**
37
     * Create a new service variable.
38
     *
39
     * @param  int    $option
40
     * @param  array  $data
41
     * @return \Pterodactyl\Models\ServiceVariable
42
     *
43
     * @throws \Pterodactyl\Exceptions\DisplayException
44
     * @throws \Pterodactyl\Exceptions\DisplayValidationException
45
     */
46 View Code Duplication
    public function create($option, array $data)
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
        $option = ServiceOption::select('id')->findOrFail($option);
49
50
        $validator = Validator::make($data, [
51
            'name' => 'required|string|min:1|max:255',
52
            'description' => 'sometimes|nullable|string',
53
            'env_variable' => 'required|regex:/^[\w]{1,255}$/',
54
            'default_value' => 'string',
55
            'options' => 'sometimes|required|array',
56
            'rules' => 'bail|required|string|min:1',
57
        ]);
58
59
        // Ensure the default value is allowed by the rules provided.
60
        $rules = (isset($data['rules'])) ? $data['rules'] : $variable->rules;
0 ignored issues
show
Bug introduced by
The variable $variable does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
61
        $validator->sometimes('default_value', $rules, function ($input) {
62
            return $input->default_value;
63
        });
64
65
        if ($validator->fails()) {
66
            throw new DisplayValidationException($validator->errors());
67
        }
68
69
        if (isset($data['env_variable'])) {
70
            $search = ServiceVariable::where('env_variable', $data['env_variable'])->where('option_id', $option->id);
71
            if ($search->first()) {
72
                throw new DisplayException('The envionment variable name assigned to this variable must be unique for this service option.');
73
            }
74
        }
75
76
        if (! isset($data['options']) || ! is_array($data['options'])) {
77
            $data['options'] = [];
78
        }
79
80
        $data['option_id'] = $option->id;
81
        $data['user_viewable'] = (in_array('user_viewable', $data['options']));
82
        $data['user_editable'] = (in_array('user_editable', $data['options']));
83
84
        // Remove field that isn't used.
85
        unset($data['options']);
86
87
        return ServiceVariable::create($data);
88
    }
89
90
    /**
91
     * Deletes a specified option variable as well as all server
92
     * variables currently assigned.
93
     *
94
     * @param  int    $id
95
     * @return void
96
     */
97
    public function delete($id)
98
    {
99
        $variable = ServiceVariable::with('serverVariable')->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...
100
101
        DB::transaction(function () use ($variable) {
102
            foreach ($variable->serverVariable as $v) {
103
                $v->delete();
104
            }
105
106
            $variable->delete();
107
        });
108
    }
109
110
    /**
111
     * Updates a given service variable.
112
     *
113
     * @param  int    $id
114
     * @param  array  $data
115
     * @return \Pterodactyl\Models\ServiceVariable
116
     *
117
     * @throws \Pterodactyl\Exceptions\DisplayException
118
     * @throws \Pterodactyl\Exceptions\DisplayValidationException
119
     */
120 View Code Duplication
    public function update($id, array $data)
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...
121
    {
122
        $variable = ServiceVariable::findOrFail($id);
123
124
        $validator = Validator::make($data, [
125
            'name' => 'sometimes|required|string|min:1|max:255',
126
            'description' => 'sometimes|nullable|string',
127
            'env_variable' => 'sometimes|required|regex:/^[\w]{1,255}$/',
128
            'default_value' => 'string',
129
            'options' => 'sometimes|required|array',
130
            'rules' => 'bail|sometimes|required|string|min:1',
131
        ]);
132
133
        // Ensure the default value is allowed by the rules provided.
134
        $rules = (isset($data['rules'])) ? $data['rules'] : $variable->rules;
135
        $validator->sometimes('default_value', $rules, function ($input) {
136
            return $input->default_value;
137
        });
138
139
        if ($validator->fails()) {
140
            throw new DisplayValidationException($validator->errors());
141
        }
142
143
        if (isset($data['env_variable'])) {
144
            $search = ServiceVariable::where('env_variable', $data['env_variable'])
145
                ->where('option_id', $variable->option_id)
146
                ->where('id', '!=', $variable->id);
147
            if ($search->first()) {
148
                throw new DisplayException('The envionment variable name assigned to this variable must be unique for this service option.');
149
            }
150
        }
151
152
        if (! isset($data['options']) || ! is_array($data['options'])) {
153
            $data['options'] = [];
154
        }
155
156
        $data['user_viewable'] = (in_array('user_viewable', $data['options']));
157
        $data['user_editable'] = (in_array('user_editable', $data['options']));
158
159
        // Remove field that isn't used.
160
        unset($data['options']);
161
162
        $variable->fill($data)->save();
163
164
        return $variable;
165
    }
166
}
167