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 |
||
5 | class Homepage { |
||
6 | |||
7 | private $db; |
||
8 | |||
9 | protected $mp_house = 1; |
||
10 | protected $cons_type = 'WMC'; |
||
11 | protected $mp_url = 'yourmp'; |
||
12 | protected $page = 'overview'; |
||
13 | |||
14 | protected $recent_types = array( |
||
15 | 'DEBATELIST' => array('recent_debates', 'debatesfront', 'Commons debates'), |
||
16 | 'LORDSDEBATELIST' => array('recent_debates', 'lordsdebatesfront', 'Lords debates'), |
||
17 | 'WHALLLIST' => array('recent_debates', 'whallfront', 'Westminster Hall debates'), |
||
18 | 'WMSLIST' => array('recent_wms', 'wmsfront', 'Written ministerial statements'), |
||
19 | 'WRANSLIST' => array('recent_wrans', 'wransfront', 'Written answers'), |
||
20 | 'StandingCommittee' => array('recent_pbc_debates', 'pbcfront', 'Public Bill committees') |
||
21 | ); |
||
22 | |||
23 | public function __construct() { |
||
26 | |||
27 | public function display() { |
||
49 | |||
50 | protected function getRegionalList() { |
||
53 | |||
54 | protected function getEditorialContent() { |
||
81 | |||
82 | public function getFeaturedDebate($gid, $title, $context, $related) { |
||
83 | View Code Duplication | if (strpos($gid, 'lords') !== false) { |
|
|
|||
84 | $debatelist = new \LORDSDEBATELIST; |
||
85 | } elseif (strpos($gid, 'westminhall') !== false) { |
||
86 | $debatelist = new \WHALLLIST; |
||
87 | } else { |
||
88 | $debatelist = new \DEBATELIST; |
||
89 | } |
||
90 | |||
91 | $item = $debatelist->display('featured_gid', array('gid' => $gid), 'none'); |
||
92 | $item = $item['data']; |
||
93 | $item['headline'] = $title; |
||
94 | $item['context'] = $context; |
||
95 | $item['featured'] = true; |
||
96 | |||
97 | $related_debates = array(); |
||
98 | foreach ( $related as $related_gid ) { |
||
99 | if ( $related_gid ) { |
||
100 | $related_item = $debatelist->display('featured_gid', array('gid' => $related_gid), 'none'); |
||
101 | $related_debates[] = $related_item['data']; |
||
102 | } |
||
103 | } |
||
104 | $item['related'] = $related_debates; |
||
105 | return $item; |
||
106 | } |
||
107 | |||
108 | protected function getFrontPageTopics() { |
||
112 | |||
113 | protected function getURLs() { |
||
118 | |||
119 | protected function getDebatesData() { |
||
149 | |||
150 | private function getCalendarData() { |
||
171 | |||
172 | } |
||
173 |
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.