| Conditions | 14 |
| Paths | 26 |
| Total Lines | 84 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 158 | public function canUser($module, $function, ValueObject $object, $targets = null) |
||
| 159 | { |
||
| 160 | $permissionSets = $this->hasAccess($module, $function); |
||
| 161 | if ($permissionSets === false || $permissionSets === true) { |
||
| 162 | return $permissionSets; |
||
| 163 | } |
||
| 164 | |||
| 165 | if ($targets instanceof ValueObject) { |
||
| 166 | $targets = array($targets); |
||
| 167 | } elseif ($targets !== null && !is_array($targets)) { |
||
| 168 | throw new InvalidArgumentType( |
||
| 169 | '$targets', |
||
| 170 | 'null|\\eZ\\Publish\\API\\Repository\\Values\\ValueObject|\\eZ\\Publish\\API\\Repository\\Values\\ValueObject[]', |
||
| 171 | $targets |
||
| 172 | ); |
||
| 173 | } |
||
| 174 | |||
| 175 | $currentUserRef = $this->getCurrentUserReference(); |
||
| 176 | foreach ($permissionSets as $permissionSet) { |
||
| 177 | /** |
||
| 178 | * First deal with Role limitation if any. |
||
| 179 | * |
||
| 180 | * Here we accept ACCESS_GRANTED and ACCESS_ABSTAIN, the latter in cases where $object and $targets |
||
| 181 | * are not supported by limitation. |
||
| 182 | * |
||
| 183 | * @var \eZ\Publish\API\Repository\Values\User\Limitation[] |
||
| 184 | */ |
||
| 185 | if ($permissionSet['limitation'] instanceof Limitation) { |
||
| 186 | $type = $this->limitationService->getLimitationType($permissionSet['limitation']->getIdentifier()); |
||
| 187 | $accessVote = $type->evaluate($permissionSet['limitation'], $currentUserRef, $object, $targets); |
||
| 188 | if ($accessVote === LimitationType::ACCESS_DENIED) { |
||
| 189 | continue; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Loop over all policies. |
||
| 195 | * |
||
| 196 | * These are already filtered by hasAccess and given hasAccess did not return boolean |
||
| 197 | * there must be some, so only return true if one of them says yes. |
||
| 198 | * |
||
| 199 | * @var \eZ\Publish\API\Repository\Values\User\Policy |
||
| 200 | */ |
||
| 201 | foreach ($permissionSet['policies'] as $policy) { |
||
| 202 | $limitations = $policy->getLimitations(); |
||
| 203 | |||
| 204 | /* |
||
| 205 | * Return true if policy gives full access (aka no limitations) |
||
| 206 | */ |
||
| 207 | if ($limitations === '*') { |
||
| 208 | return true; |
||
| 209 | } |
||
| 210 | |||
| 211 | /* |
||
| 212 | * Loop over limitations, all must return ACCESS_GRANTED for policy to pass. |
||
| 213 | * If limitations was empty array this means same as '*' |
||
| 214 | */ |
||
| 215 | $limitationsPass = true; |
||
| 216 | foreach ($limitations as $limitation) { |
||
| 217 | $type = $this->limitationService->getLimitationType($limitation->getIdentifier()); |
||
| 218 | $accessVote = $type->evaluate($limitation, $currentUserRef, $object, $targets); |
||
| 219 | /* |
||
| 220 | * For policy limitation atm only support ACCESS_GRANTED |
||
| 221 | * |
||
| 222 | * Reasoning: Right now, use of a policy limitation not valid for a policy is per definition a |
||
| 223 | * BadState. To reach this you would have to configure the "policyMap" wrongly, like using |
||
| 224 | * Node (Location) limitation on state/assign. So in this case Role Limitations will return |
||
| 225 | * ACCESS_ABSTAIN (== no access here), and other limitations will throw InvalidArgument above, |
||
| 226 | * both cases forcing dev to investigate to find miss configuration. This might be relaxed in |
||
| 227 | * the future if valid use cases for ACCESS_ABSTAIN on policy limitations becomes known. |
||
| 228 | */ |
||
| 229 | if ($accessVote !== LimitationType::ACCESS_GRANTED) { |
||
| 230 | $limitationsPass = false; |
||
| 231 | break;// Break to next policy, all limitations must pass |
||
| 232 | } |
||
| 233 | } |
||
| 234 | if ($limitationsPass) { |
||
| 235 | return true; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | return false;// None of the limitation sets wanted to let you in, sorry! |
||
| 241 | } |
||
| 242 | |||
| 281 |
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.