| Conditions | 16 | 
| Paths | 35 | 
| Total Lines | 80 | 
| Code Lines | 39 | 
| 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  | 
            ||
| 59 | public function getPermissionsCriterion($module = 'content', $function = 'read')  | 
            ||
| 60 |     { | 
            ||
| 61 | $permissionSets = $this->permissionResolver->hasAccess($module, $function);  | 
            ||
| 62 |         if (is_bool($permissionSets)) { | 
            ||
| 63 | return $permissionSets;  | 
            ||
| 64 | }  | 
            ||
| 65 | |||
| 66 |         if (empty($permissionSets)) { | 
            ||
| 67 |             throw new RuntimeException("Got an empty array of limitations from hasAccess( '{$module}', '{$function}' )"); | 
            ||
| 68 | }  | 
            ||
| 69 | |||
| 70 | /*  | 
            ||
| 71 | * RoleAssignment is a OR condition, so is policy, while limitations is a AND condition  | 
            ||
| 72 | *  | 
            ||
| 73 | * If RoleAssignment has limitation then policy OR conditions are wrapped in a AND condition with the  | 
            ||
| 74 | * role limitation, otherwise it will be merged into RoleAssignment's OR condition.  | 
            ||
| 75 | */  | 
            ||
| 76 | $currentUserRef = $this->permissionResolver->getCurrentUserReference();  | 
            ||
| 77 | $roleAssignmentOrCriteria = [];  | 
            ||
| 78 |         foreach ($permissionSets as $permissionSet) { | 
            ||
| 79 | // $permissionSet is a RoleAssignment, but in the form of role limitation & role policies hash  | 
            ||
| 80 | $policyOrCriteria = [];  | 
            ||
| 81 | /**  | 
            ||
| 82 | * @var \eZ\Publish\API\Repository\Values\User\Policy  | 
            ||
| 83 | */  | 
            ||
| 84 |             foreach ($permissionSet['policies'] as $policy) { | 
            ||
| 85 | $limitations = $policy->getLimitations();  | 
            ||
| 86 |                 if ($limitations === '*' || empty($limitations)) { | 
            ||
| 87 | // Given policy gives full access, optimize away all role policies (but not role limitation if any)  | 
            ||
| 88 | // This should be optimized on create/update of Roles, however we keep this here for bc with older data  | 
            ||
| 89 | $policyOrCriteria = [];  | 
            ||
| 90 | break;  | 
            ||
| 91 | }  | 
            ||
| 92 | |||
| 93 | $limitationsAndCriteria = [];  | 
            ||
| 94 |                 foreach ($limitations as $limitation) { | 
            ||
| 95 | $type = $this->limitationService->getLimitationType($limitation->getIdentifier());  | 
            ||
| 96 | $limitationsAndCriteria[] = $type->getCriterion($limitation, $currentUserRef);  | 
            ||
| 97 | }  | 
            ||
| 98 | |||
| 99 | $policyOrCriteria[] = isset($limitationsAndCriteria[1]) ?  | 
            ||
| 100 | new LogicalAnd($limitationsAndCriteria) :  | 
            ||
| 101 | $limitationsAndCriteria[0];  | 
            ||
| 102 | }  | 
            ||
| 103 | |||
| 104 | /**  | 
            ||
| 105 | * Apply role limitations if there is one.  | 
            ||
| 106 | *  | 
            ||
| 107 | * @var \eZ\Publish\API\Repository\Values\User\Limitation[]  | 
            ||
| 108 | */  | 
            ||
| 109 |             if ($permissionSet['limitation'] instanceof Limitation) { | 
            ||
| 110 | // We need to match both the limitation AND *one* of the policies, aka; roleLimit AND policies(OR)  | 
            ||
| 111 | $type = $this->limitationService->getLimitationType($permissionSet['limitation']->getIdentifier());  | 
            ||
| 112 |                 if (!empty($policyOrCriteria)) { | 
            ||
| 113 | $roleAssignmentOrCriteria[] = new LogicalAnd(  | 
            ||
| 114 | [  | 
            ||
| 115 | $type->getCriterion($permissionSet['limitation'], $currentUserRef),  | 
            ||
| 116 | isset($policyOrCriteria[1]) ? new LogicalOr($policyOrCriteria) : $policyOrCriteria[0],  | 
            ||
| 117 | ]  | 
            ||
| 118 | );  | 
            ||
| 119 |                 } else { | 
            ||
| 120 | $roleAssignmentOrCriteria[] = $type->getCriterion($permissionSet['limitation'], $currentUserRef);  | 
            ||
| 121 | }  | 
            ||
| 122 |             } elseif (!empty($policyOrCriteria)) { | 
            ||
| 123 | // Otherwise merge $policyOrCriteria into $roleAssignmentOrCriteria  | 
            ||
| 124 | // There is no role limitation, so any of the policies can globally match in the returned OR criteria  | 
            ||
| 125 | $roleAssignmentOrCriteria = empty($roleAssignmentOrCriteria) ?  | 
            ||
| 126 | $policyOrCriteria :  | 
            ||
| 127 | array_merge($roleAssignmentOrCriteria, $policyOrCriteria);  | 
            ||
| 128 | }  | 
            ||
| 129 | }  | 
            ||
| 130 | |||
| 131 |         if (empty($roleAssignmentOrCriteria)) { | 
            ||
| 132 | return false;  | 
            ||
| 133 | }  | 
            ||
| 134 | |||
| 135 | return isset($roleAssignmentOrCriteria[1]) ?  | 
            ||
| 136 | new LogicalOr($roleAssignmentOrCriteria) :  | 
            ||
| 137 | $roleAssignmentOrCriteria[0];  | 
            ||
| 138 | }  | 
            ||
| 139 | }  | 
            ||
| 140 |