Conditions | 12 |
Paths | 25 |
Total Lines | 78 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
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 |
||
149 | public function canUser($module, $function, ValueObject $object, array $targets = []) |
||
150 | { |
||
151 | $permissionSets = $this->hasAccess($module, $function); |
||
152 | if ($permissionSets === false || $permissionSets === true) { |
||
153 | return $permissionSets; |
||
154 | } |
||
155 | |||
156 | if (empty($targets)) { |
||
157 | $targets = null; |
||
158 | } |
||
159 | |||
160 | $currentUserRef = $this->getCurrentUserReference(); |
||
161 | foreach ($permissionSets as $permissionSet) { |
||
162 | /** |
||
163 | * First deal with Role limitation if any. |
||
164 | * |
||
165 | * Here we accept ACCESS_GRANTED and ACCESS_ABSTAIN, the latter in cases where $object and $targets |
||
166 | * are not supported by limitation. |
||
167 | * |
||
168 | * @var \eZ\Publish\API\Repository\Values\User\Limitation[] |
||
169 | */ |
||
170 | if ($permissionSet['limitation'] instanceof Limitation) { |
||
171 | $type = $this->limitationService->getLimitationType($permissionSet['limitation']->getIdentifier()); |
||
172 | $accessVote = $type->evaluate($permissionSet['limitation'], $currentUserRef, $object, $targets); |
||
173 | if ($accessVote === LimitationType::ACCESS_DENIED) { |
||
174 | continue; |
||
175 | } |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Loop over all policies. |
||
180 | * |
||
181 | * These are already filtered by hasAccess and given hasAccess did not return boolean |
||
182 | * there must be some, so only return true if one of them says yes. |
||
183 | * |
||
184 | * @var \eZ\Publish\API\Repository\Values\User\Policy |
||
185 | */ |
||
186 | foreach ($permissionSet['policies'] as $policy) { |
||
187 | $limitations = $policy->getLimitations(); |
||
188 | |||
189 | /* |
||
190 | * Return true if policy gives full access (aka no limitations) |
||
191 | */ |
||
192 | if ($limitations === '*') { |
||
193 | return true; |
||
194 | } |
||
195 | |||
196 | /* |
||
197 | * Loop over limitations, all must return ACCESS_GRANTED for policy to pass. |
||
198 | * If limitations was empty array this means same as '*' |
||
199 | */ |
||
200 | $limitationsPass = true; |
||
201 | foreach ($limitations as $limitation) { |
||
202 | $type = $this->limitationService->getLimitationType($limitation->getIdentifier()); |
||
203 | $accessVote = $type->evaluate($limitation, $currentUserRef, $object, $targets); |
||
204 | /* |
||
205 | * For policy limitation atm only support ACCESS_GRANTED |
||
206 | * |
||
207 | * Reasoning: Right now, use of a policy limitation not valid for a policy is per definition a |
||
208 | * BadState. To reach this you would have to configure the "policyMap" wrongly, like using |
||
209 | * Node (Location) limitation on state/assign. So in this case Role Limitations will return |
||
210 | * ACCESS_ABSTAIN (== no access here), and other limitations will throw InvalidArgument above, |
||
211 | * both cases forcing dev to investigate to find miss configuration. This might be relaxed in |
||
212 | * the future if valid use cases for ACCESS_ABSTAIN on policy limitations becomes known. |
||
213 | */ |
||
214 | if ($accessVote !== LimitationType::ACCESS_GRANTED) { |
||
215 | $limitationsPass = false; |
||
216 | break;// Break to next policy, all limitations must pass |
||
217 | } |
||
218 | } |
||
219 | if ($limitationsPass) { |
||
220 | return true; |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | |||
225 | return false;// None of the limitation sets wanted to let you in, sorry! |
||
226 | } |
||
227 | |||
268 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.