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 |
||
12 | class ActionDispatcher implements ActionDispatcherInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var VoterInterface |
||
16 | */ |
||
17 | private $voter; |
||
18 | |||
19 | /** |
||
20 | * @var \SplObjectStorage|ActionInterface[] |
||
21 | */ |
||
22 | private $actions; |
||
23 | |||
24 | /** |
||
25 | * @var LoggerInterface |
||
26 | */ |
||
27 | private $logger; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $name; |
||
33 | |||
34 | public function __construct($name, VoterInterface $voter, array $actions = [], LoggerInterface $logger = null) |
||
48 | |||
49 | public function performFromEvent(Event $event, $eventName) { |
||
59 | |||
60 | public function perform($entity) |
||
87 | |||
88 | /** |
||
89 | * @param array $actions |
||
90 | * @return $this |
||
91 | */ |
||
92 | public function addActions(array $actions) |
||
100 | |||
101 | /** |
||
102 | * @param ActionInterface $action |
||
103 | * @return $this |
||
104 | */ |
||
105 | public function addAction(ActionInterface $action) |
||
111 | |||
112 | /** |
||
113 | * @return \SplObjectStorage|ActionInterface[] |
||
114 | */ |
||
115 | public function getActions() |
||
119 | |||
120 | /** |
||
121 | * @param ActionInterface $action |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function removeAction(ActionInterface $action) |
||
130 | |||
131 | /** |
||
132 | * @param $entity |
||
133 | * @return bool |
||
134 | */ |
||
135 | View Code Duplication | private function performActions($entity) |
|
151 | |||
152 | /** |
||
153 | * Gets voter |
||
154 | * @return VoterInterface |
||
155 | */ |
||
156 | public function getVoter() |
||
160 | |||
161 | /** |
||
162 | * Sets voter |
||
163 | * @param \Vivait\Voter\Model\VoterInterface $voter |
||
164 | * @return $this |
||
165 | */ |
||
166 | public function setVoter($voter) |
||
172 | |||
173 | /** |
||
174 | * Gets logger |
||
175 | * @return LoggerInterface |
||
176 | */ |
||
177 | public function getLogger() |
||
181 | |||
182 | /** |
||
183 | * Sets logger |
||
184 | * @param LoggerInterface $logger |
||
185 | * @return $this |
||
186 | */ |
||
187 | public function setLogger($logger) |
||
193 | |||
194 | /** |
||
195 | * Gets name |
||
196 | * @return string |
||
197 | */ |
||
198 | public function getName() |
||
202 | |||
203 | /** |
||
204 | * Sets name |
||
205 | * @param string $name |
||
206 | * @return $this |
||
207 | */ |
||
208 | public function setName($name) |
||
214 | } |
||
215 |
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.