| Conditions | 12 |
| Paths | 9 |
| Total Lines | 53 |
| Code Lines | 36 |
| 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 |
||
| 96 | public function createAction(Request $request, GroupEntity $group) |
||
| 97 | { |
||
| 98 | if (!$this->hasPermission('ZikulaGroupsModule::', '::', ACCESS_OVERVIEW)) { |
||
| 99 | throw new AccessDeniedException(); |
||
| 100 | } |
||
| 101 | $currentUserApi = $this->get('zikula_users_module.current_user'); |
||
| 102 | if (!$currentUserApi->isLoggedIn()) { |
||
| 103 | throw new AccessDeniedException($this->__('Error! You must register for a user account on this site before you can apply for membership of a group.')); |
||
| 104 | } |
||
| 105 | $userEntity = $this->get('zikula_users_module.user_repository')->find($currentUserApi->get('uid')); |
||
| 106 | if (($group->getGtype() == CommonHelper::GTYPE_CORE) |
||
| 107 | || ($group->getState() == CommonHelper::STATE_CLOSED) |
||
| 108 | || ($group->getNbumax() > 0 && $group->getUsers()->count() > $group->getNbumax()) |
||
| 109 | || ($group->getUsers()->contains($userEntity))) { |
||
| 110 | $this->addFlash('error', $this->__('Sorry!, You cannot apply to join the requested group')); // @todo more specific info would be better |
||
| 111 | |||
| 112 | return $this->redirectToRoute('zikulagroupsmodule_group_list'); |
||
| 113 | } |
||
| 114 | $existingApplication = $this->get('zikula_groups_module.group_application_repository')->findOneBy(['group' => $group, 'user' => $userEntity]); |
||
| 115 | if ($existingApplication) { |
||
| 116 | $this->addFlash('info', $this->__('You already have a pending application. Please wait until the administrator notifies you.')); |
||
| 117 | |||
| 118 | return $this->redirectToRoute('zikulagroupsmodule_group_list'); |
||
| 119 | } |
||
| 120 | |||
| 121 | $groupApplicationEntity = new GroupApplicationEntity(); |
||
| 122 | $groupApplicationEntity->setGroup($group); |
||
| 123 | $groupApplicationEntity->setUser($userEntity); |
||
| 124 | $form = $this->createForm('Zikula\GroupsModule\Form\Type\MembershipApplicationType', $groupApplicationEntity, [ |
||
| 125 | 'translator' => $this->get('translator.default'), |
||
| 126 | ] |
||
| 127 | ); |
||
| 128 | if ($form->handleRequest($request)->isValid()) { |
||
| 129 | if ($form->get('apply')->isClicked()) { |
||
| 130 | $groupApplicationEntity = $form->getData(); |
||
| 131 | $this->get('doctrine')->getManager()->persist($groupApplicationEntity); |
||
| 132 | $this->get('doctrine')->getManager()->flush(); |
||
| 133 | $newApplicationEvent = new GenericEvent($groupApplicationEntity); |
||
| 134 | $this->get('event_dispatcher')->dispatch(GroupEvents::GROUP_NEW_APPLICATION, $newApplicationEvent); |
||
| 135 | $this->addFlash('status', $this->__('Done! The application has been sent. You will be notified by email when the application is processed.')); |
||
| 136 | } |
||
| 137 | if ($form->get('cancel')->isClicked()) { |
||
| 138 | $this->addFlash('status', $this->__('Application cancelled.')); |
||
| 139 | } |
||
| 140 | |||
| 141 | return $this->redirectToRoute('zikulagroupsmodule_group_list'); |
||
| 142 | } |
||
| 143 | |||
| 144 | return [ |
||
| 145 | 'form' => $form->createView(), |
||
| 146 | 'group' => $group, |
||
| 147 | ]; |
||
| 148 | } |
||
| 149 | } |
||
| 150 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: