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 declare(strict_types=1); |
||
25 | class Rss extends XmlAbstract |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * Format version |
||
30 | */ |
||
31 | const VERSION = '2.0'; |
||
32 | |||
33 | /** |
||
34 | * RSS document must have a <rss> root node |
||
35 | */ |
||
36 | const ROOT_NODE_TAGNAME = 'rss'; |
||
37 | |||
38 | /** |
||
39 | * <channel> node contains feed's metadata |
||
40 | */ |
||
41 | const CHANNEL_NODE_TAGNAME = 'channel'; |
||
42 | |||
43 | /** |
||
44 | * publication date |
||
45 | */ |
||
46 | const DATE_NODE_TAGNAME = 'pubDate'; |
||
47 | |||
48 | protected $mandatoryFields = ['channel']; |
||
49 | |||
50 | /** |
||
51 | * Formats the document according to the standard's specification |
||
52 | * @param \DOMDocument $document |
||
53 | * @return \DOMDocument |
||
54 | */ |
||
55 | 3 | public function format(\DOMDocument $document) : \DOMDocument |
|
66 | |||
67 | /** |
||
68 | * Tells if the parser can handle the feed or not |
||
69 | * @param Document $document |
||
70 | * @return boolean |
||
71 | */ |
||
72 | 53 | View Code Duplication | public function canHandle(Document $document) : bool |
79 | |||
80 | /** |
||
81 | * @param DOMDocument $document |
||
82 | * @return \DomElement |
||
83 | */ |
||
84 | 53 | public function getMainElement(\DOMDocument $document) : \DOMElement |
|
88 | |||
89 | /** |
||
90 | * @return \FeedIo\RuleSet |
||
91 | */ |
||
92 | 53 | public function buildFeedRuleSet() : RuleSet |
|
100 | |||
101 | /** |
||
102 | * @return \FeedIo\RuleSet |
||
103 | */ |
||
104 | 56 | public function buildItemRuleSet() : RuleSet |
|
117 | |||
118 | /** |
||
119 | * @return \FeedIo\RuleSet |
||
120 | */ |
||
121 | 57 | protected function buildBaseRuleSet() : RuleSet |
|
132 | } |
||
133 |
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.