Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AnnotationChecker often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AnnotationChecker, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | 1 | final class AnnotationChecker implements IChecker, ICheckRequirements |
|
36 | { |
||
37 | /** |
||
38 | * Implement nette smart magic |
||
39 | */ |
||
40 | 1 | use Nette\SmartObject; |
|
41 | |||
42 | /** |
||
43 | * @var NS\User |
||
44 | */ |
||
45 | private $user; |
||
46 | |||
47 | /** |
||
48 | * @param NS\User $user |
||
49 | */ |
||
50 | public function __construct(NS\User $user) |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | View Code Duplication | public function isAllowed($element) : bool |
|
75 | |||
76 | /** |
||
77 | * @param UI\ComponentReflection|UI\MethodReflection|Nette\Reflection\ClassType|Nette\Reflection\Method|\Reflector $element |
||
78 | * |
||
79 | * @return bool |
||
80 | * |
||
81 | * @throws Exceptions\InvalidArgumentException |
||
82 | */ |
||
83 | View Code Duplication | private function checkUser($element) : bool |
|
111 | |||
112 | /** |
||
113 | * @param UI\ComponentReflection|UI\MethodReflection|Nette\Reflection\ClassType|Nette\Reflection\Method|\Reflector $element |
||
114 | * |
||
115 | * @return bool |
||
116 | * |
||
117 | * @throws Exceptions\InvalidStateException |
||
118 | */ |
||
119 | private function checkResources($element) : bool |
||
151 | |||
152 | /** |
||
153 | * @param UI\ComponentReflection|UI\MethodReflection|Nette\Reflection\ClassType|Nette\Reflection\Method|\Reflector $element |
||
154 | * |
||
155 | * @return bool |
||
156 | * |
||
157 | * @throws Exceptions\InvalidStateException |
||
158 | */ |
||
159 | View Code Duplication | private function checkPrivileges($element) : bool |
|
185 | |||
186 | /** |
||
187 | * @param UI\ComponentReflection|UI\MethodReflection|Nette\Reflection\ClassType|Nette\Reflection\Method|\Reflector $element |
||
188 | * |
||
189 | * @return bool |
||
190 | */ |
||
191 | View Code Duplication | private function checkPermission($element) : bool |
|
220 | |||
221 | /** |
||
222 | * @param UI\ComponentReflection|UI\MethodReflection|Nette\Reflection\ClassType|Nette\Reflection\Method|\Reflector $element |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | private function checkRoles($element) : bool |
||
248 | |||
249 | /** |
||
250 | * @param UI\ComponentReflection|UI\MethodReflection|Nette\Reflection\ClassType|Nette\Reflection\Method|\Reflector $element |
||
251 | * @param string $attribute |
||
252 | * |
||
253 | * @return array|FALSE |
||
254 | */ |
||
255 | private function getElementAttribute($element, string $attribute) |
||
265 | } |
||
266 |
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.