| Conditions | 9 |
| Paths | 4 |
| Total Lines | 51 |
| Code Lines | 27 |
| 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 |
||
| 125 | protected function voteOnAttribute($attribute, $group, TokenInterface $token): bool |
||
| 126 | { |
||
| 127 | $user = $token->getUser(); |
||
| 128 | |||
| 129 | // make sure there is a user object (i.e. that the user is logged in) |
||
| 130 | if (!$user instanceof UserInterface) { |
||
| 131 | return false; |
||
| 132 | } |
||
| 133 | |||
| 134 | if (false == $group) { |
||
| 135 | return false; |
||
| 136 | } |
||
| 137 | |||
| 138 | $authChecker = $this->getAuthorizationChecker(); |
||
| 139 | |||
| 140 | // Admins have access to everything |
||
| 141 | if ($authChecker->isGranted('ROLE_ADMIN')) { |
||
| 142 | return true; |
||
| 143 | } |
||
| 144 | |||
| 145 | $groupInfo = [ |
||
| 146 | 'id' => $group->getId(), |
||
| 147 | 'session_id' => 0, |
||
| 148 | 'status' => $group->getStatus(), |
||
| 149 | ]; |
||
| 150 | |||
| 151 | // Legacy |
||
| 152 | return \GroupManager::userHasAccessToBrowse($user->getId(), $groupInfo); |
||
| 153 | |||
| 154 | switch ($attribute) { |
||
|
|
|||
| 155 | case self::VIEW: |
||
| 156 | if (!$group->hasUserInCourse($user, $course)) { |
||
| 157 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_SESSION_COURSE_STUDENT); |
||
| 158 | |||
| 159 | return true; |
||
| 160 | } |
||
| 161 | |||
| 162 | break; |
||
| 163 | case self::EDIT: |
||
| 164 | case self::DELETE: |
||
| 165 | if (!$session->hasCoachInCourseWithStatus($user, $course)) { |
||
| 166 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_SESSION_COURSE_TEACHER); |
||
| 167 | |||
| 168 | return true; |
||
| 169 | } |
||
| 170 | |||
| 171 | break; |
||
| 172 | } |
||
| 173 | dump("You don't have access to this group!!"); |
||
| 174 | |||
| 175 | return false; |
||
| 176 | } |
||
| 178 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.