Conditions | 10 |
Paths | 21 |
Total Lines | 39 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
54 | protected function allowed(): bool |
||
55 | { |
||
56 | // block requests from browser address bar |
||
57 | $sameOrigin = (string)Hash::get((array)$this->request->getHeader('Sec-Fetch-Site'), 0) === 'same-origin'; |
||
58 | $noReferer = empty((array)$this->request->getHeader('Referer')); |
||
59 | $isNavigate = in_array('navigate', (array)$this->request->getHeader('Sec-Fetch-Mode')); |
||
60 | if (!$sameOrigin || $noReferer || $isNavigate) { |
||
61 | return false; |
||
62 | } |
||
63 | /** @var \Authentication\Identity|null $user */ |
||
64 | $user = $this->Authentication->getIdentity(); |
||
65 | $roles = empty($user) ? [] : (array)$user->get('roles'); |
||
66 | if (empty($roles)) { |
||
67 | return false; |
||
68 | } |
||
69 | if (in_array('admin', $roles)) { |
||
70 | return true; |
||
71 | } |
||
72 | $method = $this->request->getMethod(); |
||
73 | $action = $this->request->getParam('pass')[0] ?? null; |
||
74 | $action = $action != null && strpos($action, '/') > 0 ? explode('/', $action)[0] : $action; |
||
75 | $blockedMethods = (array)Configure::read('ApiProxy.blocked', [ |
||
76 | 'objects' => ['GET', 'POST', 'PATCH', 'DELETE'], |
||
77 | 'users' => ['GET', 'POST', 'PATCH', 'DELETE'], |
||
78 | ]); |
||
79 | $blocked = in_array($method, $blockedMethods[$action] ?? []); |
||
80 | $modules = $this->viewBuilder()->getVar('modules'); |
||
81 | $modules = array_values($modules); |
||
82 | $modules = array_merge( |
||
83 | (array)Hash::combine($modules, '{n}.name', '{n}.hints.allow'), |
||
84 | ['history' => ['GET'], 'model' => ['GET']], |
||
85 | ); |
||
86 | $allowedMethods = array_merge( |
||
87 | (array)Hash::get($modules, $action, []), |
||
88 | (array)Hash::get((array)Configure::read('ApiProxy.allowed'), $action, []), |
||
89 | ); |
||
90 | $allowed = in_array($method, $allowedMethods); |
||
91 | |||
92 | return $allowed && !$blocked; |
||
93 | } |
||
95 |