Completed
Pull Request — master (#15)
by Lars
02:47
created

Utility::getMunicipalityCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 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
    function substrInArray($needle, array $haystack)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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