Failed Conditions
Branch master (215e8c)
by Johannes
04:26
created

Homepage   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 168
Duplicated Lines 5.95 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
dl 10
loc 168
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A display() 0 22 1
A getRegionalList() 0 3 1
B getEditorialContent() 0 27 4
A getFrontPageTopics() 0 4 1
A getURLs() 0 5 1
B getDebatesData() 0 30 6
A getCalendarData() 3 21 3
B getFeaturedDebate() 7 25 5

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace MySociety\TheyWorkForYou;
4
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() {
24
        $this->db = new \ParlDB;
25
    }
26
27
    public function display() {
28
        global $this_page;
29
        $this_page = $this->page;
30
31
        $data = array();
32
33
        $common = new Common;
34
35
        $data['debates'] = $this->getDebatesData();
36
37
        $user = new User();
38
        $data['mp_data'] = $user->getRep($this->cons_type, $this->mp_house);
39
40
        $data['regional'] = $this->getRegionalList();
41
        $data['popular_searches'] = $common->getPopularSearches();
42
        $data['urls'] = $this->getURLs();
43
        $data['calendar'] = $this->getCalendarData();
44
        $data['featured'] = $this->getEditorialContent();
45
        $data['topics'] = $this->getFrontPageTopics();
46
47
        return $data;
48
    }
49
50
    protected function getRegionalList() {
51
        return NULL;
52
    }
53
54
    protected function getEditorialContent() {
55
        $debatelist = new \DEBATELIST;
56
        $featured = new Model\Featured;
57
        $gid = $featured->get_gid();
58
        $gidCheck = new Gid($gid);
59
        $gid = $gidCheck->checkForRedirect();
60
        if ( $gid ) {
61
            $title = $featured->get_title();
62
            $context = $featured->get_context();
63
            $related = $featured->get_related();
64
            $item = $this->getFeaturedDebate($gid, $title, $context, $related);
65
        } else {
66
            $item = $debatelist->display('recent_debates', array('days' => 7, 'num' => 1), 'none');
67
            if ( isset($item['data']) && count($item['data']) ) {
68
                $item = $item['data'][0];
69
                $more_url = new \URL('debates');
70
                $item['more_url'] = $more_url->generate();
71
                $item['desc'] = 'Commons Debates';
72
                $item['related'] = array();
73
                $item['featured'] = false;
74
            } else {
75
                $item = array();
76
            }
77
        }
78
79
        return $item;
80
    }
81
82
    public function getFeaturedDebate($gid, $title, $context, $related) {
83 View Code Duplication
        if (strpos($gid, 'lords') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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() {
109
        $topics = new Topics();
110
        return $topics->getFrontPageTopics();
111
    }
112
113
    protected function getURLs() {
114
        $urls = array();
115
116
        return $urls;
117
    }
118
119
    protected function getDebatesData() {
120
        $debates = array(); // holds the most recent data there is data for, indexed by type
121
122
        $recent_content = array();
123
124
        foreach ( $this->recent_types as $class => $recent ) {
125
            $class = "\\$class";
126
            $instance = new $class();
127
            $more_url = new \URL($recent[1]);
128
            if ( $recent[0] == 'recent_pbc_debates' ) {
129
                $content = array( 'data' => $instance->display($recent[0], array('num' => 5), 'none') );
130
            } else {
131
                $content = $instance->display($recent[0], array('days' => 7, 'num' => 1), 'none');
132
                if ( isset($content['data']) && count($content['data']) ) {
133
                    $content = $content['data'][0];
134
                } else {
135
                    $content = array();
136
                }
137
            }
138
            if ( $content ) {
139
                $content['more_url'] = $more_url->generate();
140
                $content['desc'] = $recent[2];
141
                $recent_content[] = $content;
142
            }
143
        }
144
145
        $debates['recent'] = $recent_content;
146
147
        return $debates;
148
    }
149
150
    private function getCalendarData() {
151
        $date = date('Y-m-d');
152
        $q = $this->db->query("SELECT * FROM future
153
            LEFT JOIN future_people ON future.id = future_people.calendar_id AND witness = 0
154
            WHERE event_date >= :date
155
            AND deleted = 0
156
            ORDER BY event_date, chamber, pos",
157
            array( ':date' => $date )
158
        );
159
160
        if (!$q->rows()) {
161
            return array();
162
        }
163
164
        $data = array();
165 View Code Duplication
        foreach ($q->data as $row) {
166
            $data[$row['event_date']][$row['chamber']][] = $row;
167
        }
168
169
        return $data;
170
    }
171
172
}
173