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 |
||
9 | class Feed implements FeedInterface |
||
10 | { |
||
11 | |||
12 | /** @var ChannelInterface[] */ |
||
13 | protected $channels = []; |
||
14 | |||
15 | /** |
||
16 | * Add channel |
||
17 | * @param ChannelInterface $channel |
||
18 | * @return $this |
||
19 | */ |
||
20 | public function addChannel(ChannelInterface $channel) |
||
26 | |||
27 | /** |
||
28 | * Render XML |
||
29 | * @return string |
||
30 | */ |
||
31 | public function render() |
||
48 | |||
49 | /** |
||
50 | * Render XML |
||
51 | * @return string |
||
52 | */ |
||
53 | public function __toString() |
||
57 | } |
||
58 |
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.