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

Hansard   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 85.71 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 30
loc 35
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get_gid_from_url() 17 17 4
A gid_to_url() 13 13 2

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\Utility;
4
5
/**
6
 * Hansard Utilities
7
 *
8
 * Utility functions related to content
9
 */
10
11
class Hansard
12
{
13 View Code Duplication
    public static function get_gid_from_url($url) {
14
        $gid = NULL;
15
        $parts = parse_url($url);
16
        parse_str($parts['query'], $query);
17
18
        if ( $query['id'] ) {
19
            if (strpos($parts['path'], 'lords') !== false) {
20
                $gid = 'uk.org.publicwhip/lords/';
21
            } elseif (strpos($parts['path'], 'whall') !== false) {
22
                $gid = 'uk.org.publicwhip/westminhall/';
23
            } else {
24
                $gid = 'uk.org.publicwhip/debate/';
25
            }
26
            $gid .= $query['id'];
27
        }
28
        return $gid;
29
    }
30
31
32 View Code Duplication
    public static function gid_to_url($gid) {
33
        if ( !$gid ) {
34
            return '';
35
        }
36
        global $hansardmajors;
37
        $db = new \ParlDB();
38
39
        $q = $db->query("SELECT major FROM hansard WHERE gid = :gid", array( ':gid' => $gid ));
40
        $url_gid = fix_gid_from_db($gid);
41
        $url = new \URL($hansardmajors[$q->field(0, 'major')]['page']);
42
        $url->insert(array('id' => $url_gid));
43
        return $url->generate();
44
    }
45
}
46