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 |
||
8 | trait MatcherTrait |
||
9 | { |
||
10 | private $name; |
||
11 | private $isBot; |
||
12 | private $channels; |
||
13 | private $users; |
||
14 | private $patterns; |
||
15 | |||
16 | abstract protected function debug($message, array $context = array()); |
||
17 | |||
18 | public function matches(Message $message) |
||
42 | |||
43 | protected function matchesIsHandled(Message $message) : bool |
||
52 | |||
53 | protected function matchesIsBot(Message $message) : bool |
||
62 | |||
63 | View Code Duplication | protected function matchesChannels(Message $message) : bool |
|
72 | |||
73 | View Code Duplication | protected function matchesUsers(Message $message) : bool |
|
82 | |||
83 | protected function matchPatterns(Message $message): array |
||
95 | |||
96 | private function setName($name) |
||
100 | |||
101 | private function setIsBot($isBot) |
||
105 | |||
106 | private function setChannels(array $channels) |
||
110 | |||
111 | private function setUsers(array $users) |
||
115 | |||
116 | private function setPatterns(array $patterns) |
||
120 | } |
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.