Conditions | 12 |
Paths | 12 |
Total Lines | 59 |
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 |
||
52 | public function vote(TokenInterface $token, $subject, array $attributes) |
||
53 | { |
||
54 | // Check if the class of this object is supported by this voter |
||
55 | if (!$this->supportsClass(get_class($subject))) { |
||
56 | return VoterInterface::ACCESS_ABSTAIN; |
||
57 | } |
||
58 | |||
59 | // This voter allows one attribute to vote on. |
||
60 | if (count($attributes) > 1) { |
||
61 | throw new InvalidArgumentException('Only one attribute is allowed'); |
||
62 | } |
||
63 | |||
64 | $attribute = $attributes[0]; |
||
65 | |||
66 | // Check if the given attribute is covered by this voter |
||
67 | if (!$this->supportsAttribute($attribute)) { |
||
68 | return VoterInterface::ACCESS_ABSTAIN; |
||
69 | } |
||
70 | |||
71 | $actorRoles = $token->getRoles(); |
||
72 | |||
73 | // Does the actor have one of the required roles? |
||
74 | if (!$this->authorizedByRole($actorRoles)) { |
||
75 | return VoterInterface::ACCESS_DENIED; |
||
76 | } |
||
77 | |||
78 | $institutionConfig = $this->service->getInstitutionConfigurationOptionsFor($subject->getActorInstitution()); |
||
79 | |||
80 | if (!$institutionConfig) { |
||
81 | return VoterInterface::ACCESS_ABSTAIN; |
||
82 | } |
||
83 | |||
84 | $raInstitutions = $institutionConfig->useRa; |
||
85 | $raaInstitutions = $institutionConfig->useRaa; |
||
86 | |||
87 | // Now test if any of the roles allow the user to perform the requested task |
||
88 | foreach ($actorRoles as $role) { |
||
89 | switch ($role->getRole()) { |
||
90 | // The SRAA role is always allowed to perform the VIEW_AUDITLOG action |
||
91 | case "ROLE_SRAA": |
||
92 | return VoterInterface::ACCESS_GRANTED; |
||
93 | break; |
||
|
|||
94 | case "ROLE_RA": |
||
95 | // RA roles are allowed if the target institution is in the useRa options. |
||
96 | if (in_array($subject->getTargetInstitution(), $raInstitutions)) { |
||
97 | return VoterInterface::ACCESS_GRANTED; |
||
98 | } |
||
99 | break; |
||
100 | case "ROLE_RAA": |
||
101 | // (S)RAA roles are allowed if either the target institution is in the useRa or useRaa options. |
||
102 | if (in_array($subject->getTargetInstitution(), array_merge($raInstitutions, $raaInstitutions))) { |
||
103 | return VoterInterface::ACCESS_GRANTED; |
||
104 | } |
||
105 | break; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | return VoterInterface::ACCESS_DENIED; |
||
110 | } |
||
111 | |||
141 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.