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 Atom extends XmlAbstract |
||
26 | { |
||
27 | /** |
||
28 | * Atom document must have a <feed> root node |
||
29 | */ |
||
30 | const ROOT_NODE_TAGNAME = 'feed'; |
||
31 | |||
32 | const ITEM_NODE = 'entry'; |
||
33 | |||
34 | const DATETIME_FORMAT = \DateTime::ATOM; |
||
35 | |||
36 | /** |
||
37 | * Formats the document according to the standard's specification |
||
38 | * @param \DOMDocument $document |
||
39 | * @return \DOMDocument |
||
40 | */ |
||
41 | 5 | public function format(\DOMDocument $document) : \DOMDocument |
|
49 | |||
50 | /** |
||
51 | * Tells if the parser can handle the feed or not |
||
52 | * @param Document $document |
||
53 | * @return mixed |
||
54 | */ |
||
55 | 5 | View Code Duplication | public function canHandle(Document $document) : bool |
62 | |||
63 | /** |
||
64 | * @param DOMDocument $document |
||
65 | * @return \DomElement |
||
66 | */ |
||
67 | 9 | public function getMainElement(\DOMDocument $document) : \DOMElement |
|
71 | |||
72 | /** |
||
73 | * Builds and returns a rule set to parse the root node |
||
74 | * @return \FeedIo\RuleSet |
||
75 | */ |
||
76 | 10 | public function buildFeedRuleSet() : RuleSet |
|
88 | |||
89 | /** |
||
90 | * Builds and returns a rule set to parse an item |
||
91 | * @return \FeedIo\RuleSet |
||
92 | */ |
||
93 | 8 | public function buildItemRuleSet() : RuleSet |
|
105 | |||
106 | /** |
||
107 | * @return \FeedIo\RuleSet |
||
108 | */ |
||
109 | 10 | protected function buildBaseRuleSet() : RuleSet |
|
117 | } |
||
118 |
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.