Conditions | 18 |
Paths | 257 |
Total Lines | 70 |
Code Lines | 45 |
Lines | 39 |
Ratio | 55.71 % |
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 |
||
41 | private function init() |
||
42 | { |
||
43 | $self = $this; |
||
44 | $hasUserEntity = $this->container->has(UserEntityInterface::class); |
||
45 | $hasUserRepository = $this->container->has(UserRepositoryInterface::class); |
||
46 | $hasSessionEntity = $this->container->has(SessionEntityInterface::class); |
||
47 | $hasSessionRepository = $this->container->has(SessionRepositoryInterface::class); |
||
48 | $hasGroupEntity = $this->container->has(GroupEntityInterface::class); |
||
49 | $hasGroupRepository = $this->container->has(GroupRepositoryInterface::class); |
||
50 | $hasUserValidator = $this->container->has(UserValidatorInterface::class); |
||
51 | $hasGroupValidator = $this->container->has(GroupValidatorInterface::class); |
||
52 | |||
53 | if (!$hasUserEntity || !$hasSessionEntity || !$hasGroupEntity) { |
||
54 | throw new \Exception('Container passed to Sinergi\\Users must contain user, group and session\'s entities'); |
||
55 | } |
||
56 | |||
57 | View Code Duplication | if ($hasUserEntity && !$hasUserRepository) { |
|
|
|||
58 | $userEntity = $this->container->get(UserEntityInterface::class); |
||
59 | if ($userEntity instanceof DoctrineUserEntity) { |
||
60 | $em = $this->container->get(EntityManager::class); |
||
61 | $this->items[UserRepositoryInterface::class] = function () use ($em, $userEntity, $self) { |
||
62 | return new DoctrineUserRepository($em, $em->getClassMetadata(get_class($userEntity)), $self); |
||
63 | }; |
||
64 | } elseif ($userEntity instanceof EloquentUserEntity) { |
||
65 | $this->items[UserRepositoryInterface::class] = function () { |
||
66 | return new EloquentUserRepository(); |
||
67 | }; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | View Code Duplication | if ($hasSessionEntity && !$hasSessionRepository) { |
|
72 | $sessionEntity = $this->container->get(SessionEntityInterface::class); |
||
73 | if ($sessionEntity instanceof DoctrineSessionEntity) { |
||
74 | $em = $this->container->get(EntityManager::class); |
||
75 | $this->items[SessionRepositoryInterface::class] = function () use ($em, $sessionEntity, $self) { |
||
76 | return new DoctrineSessionRepository($em, $em->getClassMetadata(get_class($sessionEntity)), $self); |
||
77 | }; |
||
78 | } elseif ($sessionEntity instanceof EloquentSessionEntity) { |
||
79 | $this->items[SessionRepositoryInterface::class] = function () { |
||
80 | return new EloquentSessionRepository(); |
||
81 | }; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | View Code Duplication | if ($hasGroupEntity && !$hasGroupRepository) { |
|
86 | $groupEntity = $this->container->get(GroupEntityInterface::class); |
||
87 | if ($groupEntity instanceof DoctrineGroupEntity) { |
||
88 | $em = $this->container->get(EntityManager::class); |
||
89 | $this->items[GroupRepositoryInterface::class] = function () use ($em, $groupEntity, $self) { |
||
90 | return new DoctrineGroupRepository($em, $em->getClassMetadata(get_class($groupEntity)), $self); |
||
91 | }; |
||
92 | } elseif ($groupEntity instanceof EloquentGroupEntity) { |
||
93 | $this->items[GroupRepositoryInterface::class] = function () { |
||
94 | return new EloquentGroupRepository(); |
||
95 | }; |
||
96 | } |
||
97 | } |
||
98 | |||
99 | if (!$hasUserValidator) { |
||
100 | $this->items[UserValidatorInterface::class] = function () use ($self) { |
||
101 | return new UserValidator($self); |
||
102 | }; |
||
103 | } |
||
104 | |||
105 | if (!$hasGroupValidator) { |
||
106 | $this->items[GroupValidatorInterface::class] = function () use ($self) { |
||
107 | return new GroupValidator($self); |
||
108 | }; |
||
109 | } |
||
110 | } |
||
111 | |||
133 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.