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:
1 | <?php |
||
15 | class MethodListenerDiscovery |
||
16 | { |
||
17 | use FilesInDirectoryExtracter; |
||
18 | |||
19 | protected $eventToListenerMap = []; |
||
20 | /** |
||
21 | * @var MessageClassDetector |
||
22 | */ |
||
23 | private $messageClassDetector; |
||
24 | |||
25 | /** @var ListenerClassValidator */ |
||
26 | private $classValidator; |
||
27 | |||
28 | /** @var ListenerMethod[] */ |
||
29 | private $allEventsListeners = []; |
||
30 | /** |
||
31 | * @var ClassSorter |
||
32 | */ |
||
33 | private $classSorter; |
||
34 | /** |
||
35 | * @var PhpClassInFileInspector |
||
36 | */ |
||
37 | private $phpClassInFileInspector; |
||
38 | |||
39 | 1 | View Code Duplication | public function __construct( |
51 | |||
52 | |||
53 | 1 | public function discoverListeners($directory) |
|
77 | |||
78 | /** |
||
79 | * @param string $filePath |
||
80 | * @return bool |
||
81 | */ |
||
82 | 1 | protected function isListenerFileName($filePath) |
|
86 | |||
87 | /** |
||
88 | * @param $fullFilePath |
||
89 | * @return bool|\Gica\CodeAnalysis\MethodListenerDiscovery\ListenerMethod[] |
||
90 | */ |
||
91 | 1 | protected function extractListenerMethodsFromFile($fullFilePath) |
|
101 | |||
102 | 1 | protected function addListenerToEvents(ListenerMethod $listener) |
|
107 | |||
108 | /** |
||
109 | * @param ListenerMethod[] $listeners |
||
110 | * @return ListenerMethod[] |
||
111 | */ |
||
112 | 1 | private function sortListeners($listeners) |
|
120 | |||
121 | 1 | public function getEventToListenerMap() |
|
125 | |||
126 | /** |
||
127 | * @return ListenerMethod[] |
||
128 | */ |
||
129 | 1 | public function getAllEventsListeners(): array |
|
133 | |||
134 | protected function filterFiles(array $files) |
||
140 | |||
141 | /** |
||
142 | * @param $className |
||
143 | * @return ListenerMethod[] |
||
144 | */ |
||
145 | 1 | private function findListenerMethodsInClass($className) |
|
170 | |||
171 | 1 | private function getMessageClassFromMethod(\ReflectionMethod $reflectionMethod) |
|
189 | |||
190 | 1 | private function isValidListenerMethod(\ReflectionMethod $reflectionMethod) |
|
197 | |||
198 | 1 | private function isOurMessageClass(\ReflectionClass $typeHintedClass) |
|
202 | |||
203 | 1 | private function isMethodAcccepted(\ReflectionMethod $reflectionMethod) |
|
207 | } |
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.