| Conditions | 10 |
| Paths | 12 |
| Total Lines | 47 |
| Code Lines | 25 |
| 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 |
||
| 25 | public function createGroup(GroupEntityInterface $group, array $users = null): GroupEntityInterface |
||
| 26 | { |
||
| 27 | $userErrors = []; |
||
| 28 | $hasErrors = false; |
||
| 29 | if (is_array($users) && count($users)) { |
||
| 30 | /** @var UserValidatorInterface $userValidator */ |
||
| 31 | $userValidator = $this->container->get(UserValidatorInterface::class); |
||
| 32 | foreach ($users as $user) { |
||
| 33 | $userErrors[] = $userValidator($user); |
||
| 34 | if (count(end($userErrors))) { |
||
| 35 | $hasErrors = true; |
||
| 36 | } |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | /** @var GroupValidatorInterface $groupValidator */ |
||
| 41 | $groupValidator = $this->container->get(GroupValidatorInterface::class); |
||
| 42 | $errors = $groupValidator($group); |
||
| 43 | |||
| 44 | if (count($errors)) { |
||
| 45 | $hasErrors = true; |
||
| 46 | } |
||
| 47 | |||
| 48 | if ($hasErrors) { |
||
| 49 | throw new InvalidGroupException(array_merge($errors, ['users' => $userErrors])); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** @var GroupRepositoryInterface $groupRepository */ |
||
| 53 | $groupRepository = $this->container->get(GroupRepositoryInterface::class); |
||
| 54 | $groupRepository->save($group); |
||
| 55 | |||
| 56 | /** @var UserRepositoryInterface $userRepository */ |
||
| 57 | $userRepository = $this->container->get(UserRepositoryInterface::class); |
||
| 58 | |||
| 59 | if (is_array($users) && count($users)) { |
||
| 60 | |||
| 61 | /** @var UserEntityInterface $user */ |
||
| 62 | foreach ($users as $user) { |
||
| 63 | $user->setGroupId($group->getId()); |
||
| 64 | $userRepository->save($user); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | $group->setUserRepository($userRepository); |
||
| 69 | $group->getUsers(); |
||
| 70 | return $group; |
||
| 71 | } |
||
| 72 | } |
||
| 73 |