| Conditions | 21 |
| Paths | 1472 |
| Total Lines | 149 |
| Code Lines | 70 |
| Lines | 29 |
| Ratio | 19.46 % |
| 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 |
||
| 268 | public function lookup( |
||
| 269 | $module, |
||
| 270 | $function, |
||
| 271 | array $limitations = [], |
||
| 272 | APIUserReference $userReference = null |
||
| 273 | ) { |
||
| 274 | $limitationSets = []; |
||
| 275 | $permissionSets = $this->hasAccess($module, $function, $userReference); |
||
| 276 | |||
| 277 | if ($permissionSets === true) { |
||
| 278 | return new PermissionInfo(['access' => PermissionInfo::ACCESS_GRANTED]); |
||
| 279 | } |
||
| 280 | |||
| 281 | if ($permissionSets === false) { |
||
| 282 | return new PermissionInfo(['access' => PermissionInfo::ACCESS_DENIED]); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** @var \eZ\Publish\API\Repository\Values\User\Limitation[] $queryLimitationMap */ |
||
| 286 | $queryLimitationMap = []; |
||
| 287 | foreach ($limitations as $limitation) { |
||
| 288 | $queryLimitationMap[$limitation->getIdentifier()] = $limitation; |
||
| 289 | } |
||
| 290 | |||
| 291 | $setPasses = false; |
||
| 292 | |||
| 293 | foreach ($permissionSets as $permissionSet) { |
||
| 294 | $roleLimitations = []; |
||
| 295 | |||
| 296 | /** |
||
| 297 | * First deal with Role limitation if any. |
||
| 298 | * |
||
| 299 | * Here we accept ACCESS_GRANTED and ACCESS_ABSTAIN, the latter in cases where $object and $targets |
||
| 300 | * are not supported by limitation. |
||
| 301 | * |
||
| 302 | * @var \eZ\Publish\API\Repository\Values\User\Limitation[] |
||
| 303 | */ |
||
| 304 | if ($permissionSet['limitation'] instanceof Limitation) { |
||
| 305 | $limitation = $permissionSet['limitation']; |
||
| 306 | $identifier = $limitation->getIdentifier(); |
||
| 307 | |||
| 308 | View Code Duplication | if (isset($queryLimitationMap[$identifier])) { |
|
| 309 | $value = reset($queryLimitationMap[$identifier]->limitationValues); |
||
| 310 | $type = $this->limitationService->getLimitationType($identifier); |
||
| 311 | |||
| 312 | // Try with next role permission set |
||
| 313 | if (!$type->evaluateSingle($limitation, $value)) { |
||
| 314 | continue; |
||
| 315 | } |
||
| 316 | } else { |
||
| 317 | // todo How to decide if this is at all relevant for module/function? |
||
| 318 | // ACCESS_ABSTAIN is returned by evaluate(). |
||
| 319 | // Maybe it could be modelled on the permission map instead? |
||
| 320 | $roleLimitations[] = $limitation; |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | $policyLimitationSet = []; |
||
| 325 | |||
| 326 | $policiesPass = false; |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Loop over all policies. |
||
| 330 | * |
||
| 331 | * These are already filtered by hasAccess and given hasAccess did not return boolean |
||
| 332 | * there must be some, so only return true if one of them says yes. |
||
| 333 | * |
||
| 334 | * @var \eZ\Publish\API\Repository\Values\User\Policy $policy |
||
| 335 | */ |
||
| 336 | foreach ($permissionSet['policies'] as $policy) { |
||
| 337 | $policyLimitations = []; |
||
| 338 | |||
| 339 | $limitations = $policy->getLimitations(); |
||
| 340 | if ($limitations === '*') { |
||
| 341 | $limitations = []; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** @var \eZ\Publish\API\Repository\Values\User\Limitation[] $limitations */ |
||
| 345 | foreach ($limitations as $limitation) { |
||
| 346 | $identifier = $limitation->getIdentifier(); |
||
| 347 | |||
| 348 | View Code Duplication | if (isset($queryLimitationMap[$identifier])) { |
|
| 349 | $value = reset($queryLimitationMap[$identifier]->limitationValues); |
||
| 350 | $type = $this->limitationService->getLimitationType($identifier); |
||
| 351 | |||
| 352 | if ($type->evaluateSingle($limitation, $value)) { |
||
| 353 | // Continue evaluating |
||
| 354 | continue; |
||
| 355 | } else { |
||
| 356 | // Break to next policy, all limitations must either pass or record |
||
| 357 | break 2; |
||
| 358 | } |
||
| 359 | } else { |
||
| 360 | // Record limitation for return |
||
| 361 | $policyLimitations[] = $limitation; |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | $policiesPass = true; |
||
| 366 | |||
| 367 | // Break if a policy allows access without limitation |
||
| 368 | if (empty($policyLimitations)) { |
||
| 369 | $policyLimitationSet = []; |
||
| 370 | break; |
||
| 371 | } |
||
| 372 | |||
| 373 | // Else, add limitation to policy set |
||
| 374 | $policyLimitationSet[] = $policyLimitations; |
||
| 375 | } |
||
| 376 | |||
| 377 | // Try with next role permission set |
||
| 378 | if (!$policiesPass) { |
||
| 379 | continue; |
||
| 380 | } |
||
| 381 | |||
| 382 | $setPasses = true; |
||
| 383 | $setLimitations = []; |
||
| 384 | |||
| 385 | foreach ($policyLimitationSet as $policyLimitations) { |
||
| 386 | $setLimitations = array_merge($policyLimitations, $roleLimitations); |
||
| 387 | } |
||
| 388 | |||
| 389 | if (empty($setLimitations) && !empty($roleLimitations)) { |
||
| 390 | $setLimitations = $roleLimitations; |
||
| 391 | } |
||
| 392 | |||
| 393 | // Break if a set allows access without limitation |
||
| 394 | if (empty($setLimitations)) { |
||
| 395 | $limitationSets = []; |
||
| 396 | break; |
||
| 397 | } |
||
| 398 | |||
| 399 | $limitationSets = array_merge($limitationSets, [$setLimitations]); |
||
| 400 | } |
||
| 401 | |||
| 402 | $access = PermissionInfo::ACCESS_LIMITED; |
||
| 403 | |||
| 404 | if (!$setPasses) { |
||
| 405 | $access = PermissionInfo::ACCESS_DENIED; |
||
| 406 | } else if (empty($limitationSets)) { |
||
| 407 | $access = PermissionInfo::ACCESS_GRANTED; |
||
| 408 | } |
||
| 409 | |||
| 410 | return new PermissionInfo( |
||
| 411 | [ |
||
| 412 | 'access' => $access, |
||
| 413 | 'limitationSets' => $limitationSets, |
||
| 414 | ] |
||
| 415 | ); |
||
| 416 | } |
||
| 417 | } |
||
| 418 |
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.