Completed
Push — master ( 1ca939...ae40e8 )
by Lars
03:51
created

Utility   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 63
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCountryCode() 0 6 1
A getMunicipalityCode() 0 6 1
A getCSV() 0 13 3
A substrInArray() 0 8 1
1
<?php
2
/**
3
 * Utility to get country and municipality codes with EDB-Brugs
4
 *
5
 * PHP Version 5
6
 *
7
 * @category EDBBrugs
8
 * @package  EDBBrugs
9
 * @author   Lars Olesen <[email protected]>
10
 * @license  MIT Open Source License https://opensource.org/licenses/MIT
11
 * @version  GIT: <git_id>
12
 */
13
14
namespace EDBBrugs;
15
16
use EDBBrugs\UtilityInterface;
17
18
/**
19
 * Utility to get country and municipality codes with EDB-Brugs
20
 *
21
 * @category EDBBrugs
22
 * @package  EDBBrugs
23
 * @author   Lars Olesen <[email protected]>
24
 * @license  MIT Open Source License https://opensource.org/licenses/MIT
25
 * @version  GIT: <git_id>
26
 */
27
class Utility implements UtilityInterface
28
{
29
    /**
30
     * Gets country code
31
     *
32
     * $return integer
33
     */
34 2
    public function getCountryCode($country)
35
    {
36 2
        $rows = $this->getCSV(DIRNAME(__FILE__) . "/data/Landekoder.csv", $country);
37 2
        $stored_in_column = 2;
38 2
        return $rows[$stored_in_column];
39
    }
40
41
    /**
42
     * Gets municipality code
43
     *
44
     * $return integer
45
     */
46 2
    public function getMunicipalityCode($municipality)
47
    {
48 2
        $rows = $this->getCSV(DIRNAME(__FILE__) . "/data/Kommuner.csv", $municipality);
49 2
        $stored_in_column = 2;
50 2
        return $rows[$stored_in_column];
51
    }
52
53
    /**
54
     * Loops through csv file and stops with search
55
     *
56
     * @param $file_name
57
     * @param $search
58
     * @return array
59
     */
60 4
    protected function getCSV($file_name, $search)
61
    {
62 4
        $ch = fopen($file_name, "r");
63 4
        $header_row = fgetcsv($ch);
0 ignored issues
show
Unused Code introduced by
$header_row is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
65
        // This will loop through all the rows until it reaches the end
66 4
        while ($row = fgetcsv($ch, 0, ";")) {
67 4
            if ($this->substrInArray($search, $row)) {
68 2
                return $row;
69
            }
70 4
        }
71 2
        return 0;
72
    }
73
74
    /**
75
     * A version of in_array() that does a sub string match on $needle
76
     *
77
     * @param  mixed   $needle    The searched value
78
     * @param  array   $haystack  The array to search in
79
     * @return boolean
80
     */
81
    private function substrInArray($needle, array $haystack)
82
    {
83 4
        $filtered = array_filter($haystack, function ($item) use ($needle) {
84 4
            return false !== strpos($item, $needle);
85 4
        });
86
87 4
        return !empty($filtered);
88
    }
89
}
90