Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
36 | class APIRepository |
||
37 | { |
||
38 | /** |
||
39 | * Valid API permissions. |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $permissions = [ |
||
43 | 'admin' => [ |
||
44 | '*', |
||
45 | |||
46 | // User Management Routes |
||
47 | 'users.list', |
||
48 | 'users.create', |
||
49 | 'users.view', |
||
50 | 'users.update', |
||
51 | 'users.delete', |
||
52 | |||
53 | // Server Manaement Routes |
||
54 | 'servers.list', |
||
55 | 'servers.create', |
||
56 | 'servers.view', |
||
57 | 'servers.config', |
||
58 | 'servers.build', |
||
59 | 'servers.suspend', |
||
60 | 'servers.unsuspend', |
||
61 | 'servers.delete', |
||
62 | |||
63 | // Node Management Routes |
||
64 | 'nodes.list', |
||
65 | 'nodes.create', |
||
66 | 'nodes.list', |
||
67 | 'nodes.allocations', |
||
68 | 'nodes.delete', |
||
69 | |||
70 | // Service Routes |
||
71 | 'services.list', |
||
72 | 'services.view', |
||
73 | |||
74 | // Location Routes |
||
75 | 'locations.list', |
||
76 | |||
77 | ], |
||
78 | 'user' => [ |
||
79 | '*', |
||
80 | |||
81 | // Informational |
||
82 | 'me', |
||
83 | |||
84 | // Server Control |
||
85 | 'server', |
||
86 | 'server.power', |
||
87 | ], |
||
88 | ]; |
||
89 | |||
90 | /** |
||
91 | * Holder for listing of allowed IPs when creating a new key. |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $allowed = []; |
||
95 | |||
96 | protected $user; |
||
97 | |||
98 | /** |
||
99 | * Constructor. |
||
100 | */ |
||
101 | public function __construct(Models\User $user = null) |
||
108 | |||
109 | /** |
||
110 | * Create a New API Keypair on the system. |
||
111 | * |
||
112 | * @param array $data An array with a permissions and allowed_ips key. |
||
113 | * |
||
114 | * @throws Pterodactyl\Exceptions\DisplayException if there was an error that can be safely displayed to end-users. |
||
115 | * @throws Pterodactyl\Exceptions\DisplayValidationException if there was a validation error. |
||
116 | * |
||
117 | * @return string Returns the generated secret token. |
||
118 | */ |
||
119 | public function create(array $data) |
||
120 | { |
||
121 | $validator = Validator::make($data, [ |
||
122 | 'memo' => 'string|max:500', |
||
123 | 'permissions' => 'sometimes|required|array', |
||
124 | 'adminPermissions' => 'sometimes|required|array', |
||
125 | ]); |
||
126 | |||
127 | $validator->after(function ($validator) use ($data) { |
||
128 | if (array_key_exists('allowed_ips', $data) && ! empty($data['allowed_ips'])) { |
||
129 | foreach (explode("\n", $data['allowed_ips']) as $ip) { |
||
130 | $ip = trim($ip); |
||
131 | try { |
||
132 | Network::parse($ip); |
||
133 | array_push($this->allowed, $ip); |
||
134 | } catch (\Exception $ex) { |
||
135 | $validator->errors()->add('allowed_ips', 'Could not parse IP <' . $ip . '> because it is in an invalid format.'); |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 | }); |
||
140 | |||
141 | // Run validator, throw catchable and displayable exception if it fails. |
||
142 | // Exception includes a JSON result of failed validation rules. |
||
143 | if ($validator->fails()) { |
||
144 | throw new DisplayValidationException($validator->errors()); |
||
145 | } |
||
146 | |||
147 | DB::beginTransaction(); |
||
148 | try { |
||
149 | $secretKey = str_random(16) . '.' . str_random(7) . '.' . str_random(7); |
||
150 | $key = new Models\APIKey; |
||
151 | $key->fill([ |
||
152 | 'user' => $this->user->id, |
||
153 | 'public' => str_random(16), |
||
154 | 'secret' => Crypt::encrypt($secretKey), |
||
155 | 'allowed_ips' => empty($this->allowed) ? null : json_encode($this->allowed), |
||
156 | 'memo' => $data['memo'], |
||
157 | 'expires_at' => null, |
||
158 | ]); |
||
159 | $key->save(); |
||
160 | |||
161 | $totalPermissions = 0; |
||
162 | View Code Duplication | if (isset($data['permissions'])) { |
|
|
|||
163 | foreach ($data['permissions'] as $permNode) { |
||
164 | if (! strpos($permNode, ':')) { |
||
165 | continue; |
||
166 | } |
||
167 | |||
168 | list($toss, $permission) = explode(':', $permNode); |
||
169 | if (in_array($permission, $this->permissions['user'])) { |
||
170 | $totalPermissions++; |
||
171 | $model = new Models\APIPermission; |
||
172 | $model->fill([ |
||
173 | 'key_id' => $key->id, |
||
174 | 'permission' => 'api.user.' . $permission, |
||
175 | ]); |
||
176 | $model->save(); |
||
177 | } |
||
178 | } |
||
179 | } |
||
180 | |||
181 | View Code Duplication | if ($this->user->root_admin === 1 && isset($data['adminPermissions'])) { |
|
182 | foreach ($data['adminPermissions'] as $permNode) { |
||
183 | if (! strpos($permNode, ':')) { |
||
184 | continue; |
||
185 | } |
||
186 | |||
187 | list($toss, $permission) = explode(':', $permNode); |
||
188 | if (in_array($permission, $this->permissions['admin'])) { |
||
189 | $totalPermissions++; |
||
190 | $model = new Models\APIPermission; |
||
191 | $model->fill([ |
||
192 | 'key_id' => $key->id, |
||
193 | 'permission' => 'api.admin.' . $permission, |
||
194 | ]); |
||
195 | $model->save(); |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 | |||
200 | if ($totalPermissions < 1) { |
||
201 | throw new DisplayException('No valid permissions were passed.'); |
||
202 | } |
||
203 | |||
204 | DB::commit(); |
||
205 | |||
206 | return $secretKey; |
||
207 | } catch (\Exception $ex) { |
||
208 | DB::rollBack(); |
||
209 | throw $ex; |
||
210 | } |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Revokes an API key and associated permissions. |
||
215 | * |
||
216 | * @param string $key The public key. |
||
217 | * |
||
218 | * @throws Illuminate\Database\Eloquent\ModelNotFoundException |
||
219 | * |
||
220 | * @return void |
||
221 | */ |
||
222 | public function revoke($key) |
||
237 | } |
||
238 |
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.