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 |
||
| 21 | class AtomParser extends Parser |
||
| 22 | { |
||
| 23 | protected $mandatoryFields = array( |
||
| 24 | 'id', |
||
| 25 | 'updated', |
||
| 26 | 'title', |
||
| 27 | 'link', |
||
| 28 | 'entry', |
||
| 29 | ); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * |
||
| 33 | */ |
||
| 34 | 7 | public function __construct() |
|
| 38 | |||
| 39 | /** |
||
| 40 | * @param SimpleXMLElement $xmlBody |
||
| 41 | * |
||
| 42 | * @return bool |
||
| 43 | */ |
||
| 44 | 5 | public function canHandle(SimpleXMLElement $xmlBody) |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @param SimpleXMLElement $xmlBody |
||
| 51 | * @param FeedInterface $feed |
||
| 52 | * @param array $filters |
||
| 53 | * |
||
| 54 | * @return FeedInterface |
||
| 55 | * |
||
| 56 | * @throws ParserException |
||
| 57 | */ |
||
| 58 | 4 | protected function parseBody(SimpleXMLElement $xmlBody, FeedInterface $feed, array $filters) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @param SimpleXMLElement $xmlBody |
||
| 93 | * @param FeedInterface $feed |
||
| 94 | * |
||
| 95 | * @throws ParserException |
||
| 96 | */ |
||
| 97 | 3 | protected function parseHeaders(SimpleXMLElement $xmlBody, FeedInterface $feed) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @param SimpleXMLElement $xmlElement |
||
| 112 | * @param string $type |
||
| 113 | */ |
||
| 114 | 4 | protected function detectLink(SimpleXMLElement $xmlElement, $type) |
|
| 125 | |||
| 126 | 4 | protected function parseContent(SimpleXMLElement $content) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Handles enclosures if any. |
||
| 142 | * |
||
| 143 | * @param SimpleXMLElement $element |
||
| 144 | * @param ItemInInterface $item |
||
| 145 | * |
||
| 146 | * @return $this |
||
| 147 | */ |
||
| 148 | 3 | protected function handleEnclosure(SimpleXMLElement $element, ItemInInterface $item) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Parse category elements. |
||
| 162 | * We may have more than one. |
||
| 163 | * |
||
| 164 | * @param SimpleXMLElement $element |
||
| 165 | * @param ItemInInterface $item |
||
| 166 | */ |
||
| 167 | 3 | View Code Duplication | protected function parseCategories(SimpleXMLElement $element, ItemInInterface $item) |
| 176 | } |
||
| 177 |
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.