| Conditions | 10 |
| Paths | 11 |
| Total Lines | 57 |
| Code Lines | 24 |
| 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 |
||
| 87 | protected function voteOnAttribute($attribute, $course, TokenInterface $token) |
||
| 88 | { |
||
| 89 | $user = $token->getUser(); |
||
| 90 | // Anons can enter a course depending of the course visibility |
||
| 91 | /*if (!$user instanceof UserInterface) { |
||
| 92 | return false; |
||
| 93 | }*/ |
||
| 94 | |||
| 95 | $authChecker = $this->container->get('security.authorization_checker'); |
||
| 96 | |||
| 97 | // Admins have access to everything |
||
| 98 | if ($authChecker->isGranted('ROLE_ADMIN')) { |
||
| 99 | |||
| 100 | return true; |
||
| 101 | } |
||
| 102 | |||
| 103 | // Course is active? |
||
| 104 | if (!$course->isActive()) { |
||
| 105 | |||
| 106 | return false; |
||
| 107 | } |
||
| 108 | |||
| 109 | switch ($attribute) { |
||
| 110 | case self::VIEW: |
||
| 111 | // "Open to the world" no need to check if user is registered |
||
| 112 | if ($course->isPublic()) { |
||
| 113 | |||
| 114 | return true; |
||
| 115 | } |
||
| 116 | |||
| 117 | // Other course visibility need to have a user set |
||
| 118 | if (!$user instanceof UserInterface) { |
||
| 119 | return false; |
||
| 120 | } |
||
| 121 | |||
| 122 | // User is subscribed in the course no matter if is teacher/student |
||
| 123 | if ($course->hasUser($user)) { |
||
| 124 | |||
| 125 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_STUDENT); |
||
| 126 | |||
| 127 | return true; |
||
| 128 | } |
||
| 129 | |||
| 130 | break; |
||
| 131 | case self::EDIT: |
||
| 132 | case self::DELETE: |
||
| 133 | // Only teacher can edit/delete stuff |
||
| 134 | if ($course->hasTeacher($user)) { |
||
| 135 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER); |
||
| 136 | |||
| 137 | return true; |
||
| 138 | } |
||
| 139 | break; |
||
| 140 | } |
||
| 141 | |||
| 142 | return false; |
||
| 143 | } |
||
| 144 | } |
||
| 145 |
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.