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 |
||
49 | class Roster extends AbstractEventListener implements BlockingEventListenerInterface |
||
50 | { |
||
51 | |||
52 | /** |
||
53 | * Blocking. |
||
54 | * |
||
55 | * @var boolean |
||
56 | */ |
||
57 | protected $blocking = false; |
||
58 | |||
59 | /** |
||
60 | * user object. |
||
61 | * |
||
62 | * @var User |
||
63 | */ |
||
64 | protected $userObject; |
||
65 | |||
66 | /** |
||
67 | * {@inheritDoc} |
||
68 | */ |
||
69 | 3 | View Code Duplication | public function attachEvents() |
|
|||
70 | { |
||
71 | 3 | $this->getOutputEventManager() |
|
72 | 3 | ->attach('{jabber:iq:roster}query', [$this, 'query']); |
|
73 | 3 | $this->getInputEventManager() |
|
74 | 3 | ->attach('{jabber:iq:roster}query', [$this, 'result']); |
|
75 | 3 | } |
|
76 | |||
77 | /** |
||
78 | * Sending a query request for roster sets listener to blocking mode. |
||
79 | * |
||
80 | * @return void |
||
81 | */ |
||
82 | 3 | public function query() |
|
86 | |||
87 | /** |
||
88 | * Result received. |
||
89 | * |
||
90 | * @param \Fabiang\Xmpp\Event\XMLEvent $event |
||
91 | * @return void |
||
92 | */ |
||
93 | 3 | public function result(XMLEvent $event) |
|
120 | |||
121 | /** |
||
122 | * Get user object. |
||
123 | * |
||
124 | * @return User |
||
125 | */ |
||
126 | 3 | public function getUserObject() |
|
134 | |||
135 | /** |
||
136 | * Set user object. |
||
137 | * |
||
138 | * @param User $userObject |
||
139 | * @return $this |
||
140 | */ |
||
141 | 3 | public function setUserObject(User $userObject) |
|
146 | |||
147 | /** |
||
148 | * {@inheritDoc} |
||
149 | */ |
||
150 | 3 | public function isBlocking() |
|
154 | } |
||
155 |
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.