CountryLoader   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 43.48 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 56.52%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 30
loc 69
ccs 13
cts 23
cp 0.5652
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A loadAllCountries() 0 7 1
A loadCountry() 15 15 3
A loadCountryPolygon() 15 15 3

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 GeoBase\Countries\Country;
4
5
use GeoBase\CountriesData\CountriesData;
6
7
class CountryLoader
8
{
9
    const ALL_COUNTRIES_FILE = 'countries.json';
10
    const COUNTRY_FILE = 'countries/%s.json';
11
    const COUNTRY_POLYGON_FILE = 'countries/%s/polygon.json';
12
13
    /**
14
     * @var Storage
15
     */
16
    private $path;
17
18 3
    public function __construct()
19
    {
20 3
        $this->path = CountriesData::getCountriesPath();
0 ignored issues
show
Documentation Bug introduced by
It seems like \GeoBase\CountriesData\C...ata::getCountriesPath() of type string is incompatible with the declared type object<GeoBase\Countries\Country\Storage> of property $path.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
21 3
    }
22
23
    /**
24
     * @return array
25
     */
26 3
    public function loadAllCountries()
27
    {
28 3
        return json_decode(
29 3
            file_get_contents($this->path.DIRECTORY_SEPARATOR.self::ALL_COUNTRIES_FILE),
30 3
            true
31
        );
32
    }
33
34
    /**
35
     * @param string $country
36
     *
37
     * @return array
38
     */
39 1 View Code Duplication
    public function loadCountry($country)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41 1
        if (!$country) {
42
            return;
43
        }
44 1
        $file = $this->path.DIRECTORY_SEPARATOR.sprintf(self::COUNTRY_FILE, $country);
45 1
        if (!file_exists($file)) {
46
            return;
47
        }
48
49 1
        return json_decode(
50
            file_get_contents($file),
51 1
            true
52
        );
53
    }
54
55
    /**
56
     * @param string $country
57
     *
58
     * @return array
59
     */
60 View Code Duplication
    public function loadCountryPolygon($country)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
    {
62
        if (!$country) {
63
            return;
64
        }
65
        $file = $this->path.DIRECTORY_SEPARATOR.sprintf(self::COUNTRY_POLYGON_FILE, $country);
66
        if (!file_exists($file)) {
67
            return;
68
        }
69
70
        return json_decode(
71
            file_get_contents($file),
72
            true
73
        );
74
    }
75
}
76