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 |
||
19 | class ContentExtractor extends AbstractModule implements ModuleInterface { |
||
20 | use ArticleMutatorTrait, NodeGravityTrait, NodeCommonTrait; |
||
21 | |||
22 | /** |
||
23 | * @param Article $article |
||
24 | */ |
||
25 | public function run(Article $article) { |
||
30 | |||
31 | /** |
||
32 | * @param Article $article |
||
33 | * |
||
34 | * @return array |
||
35 | */ |
||
36 | private function getTopNodeCandidatesByContents(Article $article) { |
||
52 | |||
53 | /** |
||
54 | * @param Element $node |
||
55 | * @param int $i |
||
56 | * @param int $totalNodes |
||
57 | * |
||
58 | * @return double |
||
59 | */ |
||
60 | private function getTopNodeCandidateScore(Element $node, $i, $totalNodes) { |
||
80 | |||
81 | /** |
||
82 | * @param array $nodes |
||
83 | * |
||
84 | * @return Element|null |
||
85 | */ |
||
86 | private function getTopNodeByScore($nodes) { |
||
109 | |||
110 | /** |
||
111 | * @param Element $node |
||
112 | * @param double $upscore |
||
113 | */ |
||
114 | private function calculateBestNodeCandidateScores(Element $node, $upscore) { |
||
125 | |||
126 | /** |
||
127 | * @param Element $node |
||
128 | * @param array $nodeCandidates |
||
129 | */ |
||
130 | private function updateBestNodeCandidates(Element $node, $nodeCandidates) { |
||
147 | |||
148 | /** |
||
149 | * We're going to start looking for where the clusters of paragraphs are. We'll score a cluster based on the number of stopwords |
||
150 | * and the number of consecutive paragraphs together, which should form the cluster of text that this node is around |
||
151 | * also store on how high up the paragraphs are, comments are usually at the bottom and should get a lower score |
||
152 | * |
||
153 | * @return Element|null |
||
154 | */ |
||
155 | public function getTopNode() { |
||
174 | |||
175 | /** |
||
176 | * A lot of times the first paragraph might be the caption under an image so we'll want to make sure if we're going to |
||
177 | * boost a parent node that it should be connected to other paragraphs, at least for the first n paragraphs |
||
178 | * so we'll want to make sure that the next sibling is a paragraph and has at least some substantial weight to it |
||
179 | * |
||
180 | * @param Element $node |
||
181 | * |
||
182 | * @return bool |
||
183 | */ |
||
184 | private function isOkToBoost(Element $node) { |
||
212 | } |
||
213 |
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.