Completed
Push — master ( 922be9...285e65 )
by Nick
41s queued 31s
created

Constituencies   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 64.29%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 34
ccs 9
cts 14
cp 0.6429
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A normaliseConstituencyName() 0 22 3
1
<?php
2
3
namespace MySociety\TheyWorkForYou\Utility;
4
5
/**
6
 * Constituency Utilities
7
 *
8
 * Utility functions for dealing with constituency names
9
 */
10
11
class Constituencies
12
{
13
14
    /**
15
     * Normalise Constituency Name
16
     *
17
     * Turn variations on a constituency name into the canonical version.
18
     *
19
     * @param $names string A constituency name to normalise.
20
     *
21
     * @return string|bool The normalised constituency name, or false if no match.
22
     */
23 1
    public static function normaliseConstituencyName($name) {
24
25 1
        $db = new \ParlDB;
26
27
        // In case we still have an &amp; lying around
28 1
        $name = str_replace("&amp;", "&", $name);
29
30 1
        $query = "select cons_id from constituency where name like :name and from_date <= date(now()) and date(now()) <= to_date";
31 1
        $q1 = $db->query($query, array(
32 1
            ':name' => $name
33 1
            ))->first();
34 1
        if (!$q1) {
35 1
            return false;
36
        }
37
38
        $query = "select name from constituency where main_name and cons_id = '" . $q1['cons_id'] . "'";
39
        $q2 = $db->query($query)->first();
40
        if (!$q2) {
41
            return false;
42
        }
43
44
        return $q2['name'];
45
    }
46
47
}
48