Conditions | 17 |
Paths | 16 |
Total Lines | 86 |
Code Lines | 45 |
Lines | 28 |
Ratio | 32.56 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php namespace Anomaly\Streams\Platform\Support; |
||
143 | protected function checkPermission($permission, UserInterface $user) |
||
144 | { |
||
145 | /* |
||
146 | * No permission, let it proceed. |
||
147 | */ |
||
148 | if (!$permission) { |
||
149 | return true; |
||
150 | } |
||
151 | |||
152 | /* |
||
153 | * If the permission does not actually exist |
||
154 | * then we cant really do anything with it. |
||
155 | */ |
||
156 | if (str_is('*::*.*', $permission) && !ends_with($permission, '*')) { |
||
157 | $parts = explode('.', str_replace('::', '.', $permission)); |
||
158 | $end = array_pop($parts); |
||
159 | $group = array_pop($parts); |
||
160 | $parts = explode('::', $permission); |
||
161 | |||
162 | // If it does not exist, we are authorized. |
||
163 | if (!in_array($end, (array)$this->config->get($parts[0] . '::permissions.' . $group))) { |
||
164 | return true; |
||
165 | } |
||
166 | } elseif (ends_with($permission, '*')) { |
||
167 | $parts = explode('::', $permission); |
||
168 | |||
169 | $addon = array_shift($parts); |
||
170 | |||
171 | /* |
||
172 | * Check vendor.module.slug::group.* |
||
173 | * then check vendor.module.slug::* |
||
174 | */ |
||
175 | if (str_is('*.*.*::*.*.*', $permission)) { |
||
176 | $end = trim(substr($permission, strpos($permission, '::') + 2), '.*'); |
||
177 | |||
178 | if (!$permissions = $this->config->get($addon . '::permissions.' . $end)) { |
||
179 | return true; |
||
180 | } else { |
||
181 | return $user->hasAnyPermission($permissions); |
||
182 | } |
||
183 | } elseif (str_is('*.*.*::*.*', $permission)) { |
||
184 | $end = trim(substr($permission, strpos($permission, '::') + 2), '.*'); |
||
185 | |||
186 | View Code Duplication | if (!$permissions = $this->config->get($addon . '::permissions.' . $end)) { |
|
187 | return true; |
||
188 | } else { |
||
189 | $check = []; |
||
190 | |||
191 | foreach ($permissions as &$permission) { |
||
192 | $check[] = $addon . '::' . $end . '.' . $permission; |
||
193 | } |
||
194 | |||
195 | return $user->hasAnyPermission($check); |
||
196 | } |
||
197 | View Code Duplication | } else { |
|
198 | if (!$permissions = $this->config->get($addon . '::permissions')) { |
||
199 | return true; |
||
200 | } else { |
||
201 | $check = []; |
||
202 | |||
203 | foreach ($permissions as $group => &$permission) { |
||
204 | foreach ($permission as $access) { |
||
205 | $check[] = $addon . '::' . $group . '.' . $access; |
||
206 | } |
||
207 | } |
||
208 | |||
209 | return $user->hasAnyPermission($check); |
||
210 | } |
||
211 | } |
||
212 | } else { |
||
213 | $parts = explode('::', $permission); |
||
214 | |||
215 | $end = array_pop($parts); |
||
216 | |||
217 | if (!in_array($end, (array)$this->config->get($parts[0] . '::permissions'))) { |
||
218 | return true; |
||
219 | } |
||
220 | } |
||
221 | |||
222 | // Check if the user actually has permission. |
||
223 | if (!$user || !$user->hasPermission($permission)) { |
||
224 | return false; |
||
225 | } |
||
226 | |||
227 | return true; |
||
228 | } |
||
229 | |||
253 |