Conditions | 11 |
Paths | 8 |
Total Lines | 48 |
Lines | 0 |
Ratio | 0 % |
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 |
||
79 | public function check(ForumsUserInterface $user, string $resource, IdentifierInterface ...$identifiers): bool |
||
80 | { |
||
81 | /// Force allow all check |
||
82 | $force = $this->PermissionConfig->getForce($resource); |
||
83 | if ($force) { |
||
84 | return $force->check($resource); |
||
85 | } |
||
86 | |||
87 | $roleObject = null; |
||
88 | |||
89 | if (!empty($identifiers)) { |
||
90 | foreach ($identifiers as $identifier) { |
||
91 | $type = $identifier->type(); |
||
92 | switch ($type) { |
||
93 | case ('owner'): |
||
94 | /// Owner check |
||
95 | foreach ($this->PermissionConfig->getOwner($resource) as $allowance) { |
||
96 | if ($allowance->check($resource, $user, $identifier->get())) { |
||
97 | return true; |
||
98 | } |
||
99 | } |
||
100 | break; |
||
101 | case ('role'): |
||
102 | // Just remember if there's a role object. Performed below. |
||
103 | $roleObject = $identifier->get(); |
||
104 | break; |
||
105 | default: |
||
106 | new InvalidArgumentException( |
||
107 | sprintf('Unknown identifier type "%s" in permissin check.', $type) |
||
108 | ); |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | |||
113 | /// Role check |
||
114 | $roleAllowances = $this->PermissionConfig->getRole($resource); |
||
115 | if (!empty($roleAllowances)) { |
||
116 | foreach ($roleAllowances as $allowance) { |
||
117 | $role = $user->getRole(); |
||
118 | $roles = $this->roles->get($role); |
||
119 | if ($allowance->check($resource, $roles, $roleObject)) { |
||
120 | return true; |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | |||
125 | return false; |
||
126 | } |
||
127 | |||
171 |