Passed
Pull Request — master (#1700)
by Struan
04:10
created

House::getCountryDetails()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 51
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0023

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 41
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 51
rs 9.264
ccs 11
cts 12
cp 0.9167
crap 2.0023

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace MySociety\TheyWorkForYou\Utility;
4
5
/**
6
 * House Utilities
7
 *
8
 * Utility functions related to house types
9
 */
10
11
class House
12
{
13
    public static function division_house_name_to_number($name) {
14
        $name_to_number = array(
15
            'commons' => HOUSE_TYPE_COMMONS,
16
            'lords' => HOUSE_TYPE_LORDS,
17
            'scotland' => HOUSE_TYPE_SCOTLAND,
18
            'pbc' => HOUSE_TYPE_COMMONS,
19
            'senedd' => HOUSE_TYPE_WALES,
20
        );
21
22
        return $name_to_number[$name];
23
    }
24
25
    public static function house_to_members($house) {
26
        $house_to_members = array(
27
            HOUSE_TYPE_COMMONS => array(
28
                'singular' => 'MP',
29
                'plural'   => 'MPs'
30
            ),
31
            HOUSE_TYPE_LORDS => array(
32
                'singular' => 'Member of the House of Lords',
33
                'plural'   => 'Members of the House of Lords'
34
            ),
35
            HOUSE_TYPE_NI => array(
36
                'singular' => 'MLA',
37
                'plural'   => 'MLAs'
38
            ),
39
            HOUSE_TYPE_SCOTLAND => array(
40
                'singular' => 'MSP',
41
                'plural'   => 'MSPs'
42
            ),
43
            HOUSE_TYPE_WALES => array(
44
                'singular' => gettext('MS'),
45
                'plural'   => gettext('MSs')
46
            ),
47
            HOUSE_TYPE_LONDON_ASSEMBLY => array(
48
                'singular' => 'Member of the London Assembly',
49
                'plural'   => 'Members of the London Assembly'
50
            )
51
        );
52
53
        return $house_to_members[$house];
54
    }
55
56 4
    public static function getCountryDetails($house) {
57
        $details = array(
58 4
            HOUSE_TYPE_COMMONS => array (
59 4
                'country' => 'UK',
60
                'assembly' => 'uk-commons',
61
                'location' => '&ndash; in the House of Commons',
62
                'cons_type' => 'WMC',
63
                'assembly_name' => 'House of Commons',
64
            ),
65 4
            HOUSE_TYPE_NI => array (
66
                'country' => 'NORTHERN IRELAND',
67
                'assembly' => 'ni',
68
                'location' => '&ndash; in the Northern Ireland Assembly',
69
                'cons_type' => 'NIE',
70
                'assembly_name' => 'Northern Ireland Assembly',
71
            ),
72 4
            HOUSE_TYPE_SCOTLAND => array (
73
                'country' => 'SCOTLAND',
74
                'assembly' => 'scotland',
75
                'location' => '&ndash; in the Scottish Parliament',
76
                'cons_type' => 'SPC',
77
                'assembly_name' => 'Scottish Parliament',
78
            ),
79 4
            HOUSE_TYPE_WALES => array (
80
                'country' => 'WALES',
81
                'assembly' => 'wales',
82
                'location' => '&ndash; in the Senedd',
83
                'cons_type' => 'WAC',
84
                'assembly_name' => 'Welsh Parliament',
85
            ),
86 4
            HOUSE_TYPE_LORDS => array (
87
                'country' => 'UK',
88
                'assembly' => 'uk-lords',
89
                'location' => '&ndash; in the House of Lords',
90
                'cons_type' => '',
91
                'assembly_name' => 'House of Lords',
92
            ),
93 4
            HOUSE_TYPE_LONDON_ASSEMBLY => array (
94
                'country' => 'UK',
95
                'assembly' => 'london-assembly',
96
                'location' => '&ndash; in the London Assembly',
97
                'cons_type' => 'LAS',
98
                'assembly_name' => 'London Assembly',
99
            )
100
        );
101 4
        if (!array_key_exists($house, $details)) {
102
            return array('', '', '', '', '');
103
        }
104
105 4
        $detail = $details[$house];
106 4
        return array($detail['country'], $detail['location'], $detail['assembly'], $detail['cons_type'], $detail['assembly_name']);
107
    }
108
109 2
    public static function majorToHouse($major) {
110
        $major_to_house = array(
111 2
            1 => array(HOUSE_TYPE_COMMONS),
112 2
            2 => array(HOUSE_TYPE_COMMONS),
113 2
            3 => array(HOUSE_TYPE_COMMONS, HOUSE_TYPE_LORDS),
114 2
            4 => array(HOUSE_TYPE_COMMONS, HOUSE_TYPE_LORDS),
115 2
            5 => array(HOUSE_TYPE_NI),
116 2
            6 => array(HOUSE_TYPE_COMMONS),
117 2
            7 => array(HOUSE_TYPE_SCOTLAND),
118 2
            8 => array(HOUSE_TYPE_SCOTLAND),
119 2
            9 => array(HOUSE_TYPE_LONDON_ASSEMBLY),
120 2
            10 => array(HOUSE_TYPE_WALES),
121 2
            11 => array(HOUSE_TYPE_WALES),
122 2
            101 => array(HOUSE_TYPE_LORDS),
123
        );
124
125 2
        return $major_to_house[$major];
126
    }
127
}
128