Completed
Push — develop ( b4fd40...785f8f )
by greg
03:10
created

Country   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 92
Duplicated Lines 14.13 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 17
c 3
b 1
f 0
lcom 1
cbo 2
dl 13
loc 92
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTranslatedTo() 0 4 1
A setTranslatedTo() 0 6 1
A getPath() 0 8 2
A getCorePath() 0 8 2
A getAllCountries() 7 17 4
B getCountry() 6 24 6

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
namespace PlaygroundCore\Service;
3
4
use Zend\ServiceManager\ServiceManager;
5
use ZfcBase\EventManager\EventProvider;
6
use Zend\ServiceManager\ServiceLocatorInterface;
7
8
final class Country extends EventProvider
9
{
10
    private $translatedTo;
11
12
    private $path;
13
14
    private $corePath;
15
16
    /**
17
     *
18
     * @var ServiceManager
19
     */
20
    protected $serviceLocator;
21
22
    public function __construct(ServiceLocatorInterface $locator)
23
    {
24
        $this->serviceLocator = $locator;
0 ignored issues
show
Documentation Bug introduced by
$locator is of type object<Zend\ServiceManag...erviceLocatorInterface>, but the property $serviceLocator was declared to be of type object<Zend\ServiceManager\ServiceManager>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
25
    }
26
27
    public function getTranslatedTo()
28
    {
29
        return $this->translatedTo;
30
    }
31
32
    public function setTranslatedTo($translatedTo)
33
    {
34
        $this->translatedTo = (string) $translatedTo;
35
36
        return $this;
37
    }
38
39
    public function getPath()
40
    {
41
        if(empty($this->path)){
42
            $this->path = str_replace('\\', '/', getcwd()) . '/language/countries';
43
        }
44
45
        return $this->path;
46
    }
47
48
    public function getCorePath()
49
    {
50
        if(empty($this->corePath)){
51
            $this->corePath = __DIR__ . '/../../../language/countries';
52
        }
53
54
        return $this->corePath;
55
    }
56
57
    public function getAllCountries($translatedTo = null)
58
    {
59
        if (null === $translatedTo) {
60
            $translatedTo = $this->serviceLocator->get('translator')->getLocale();
61
        }
62
        
63
        $fileName = $this->getPath().'/'.$translatedTo.'.php';
64 View Code Duplication
        if (! file_exists($fileName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
65
            $fileName = $this->getCorePath().'/'.$translatedTo.'.php';
66
67
            if (! file_exists($fileName)) {
68
                throw new \InvalidArgumentException("Language $translatedTo not found.");
69
            }
70
        }
71
72
        return include $fileName;
73
    }
74
75
    public function getCountry($country, $translatedTo = null)
76
    {
77
        if (null === $translatedTo) {
78
            $translatedTo = $this->serviceLocator->get('translator')->getLocale();
79
        }
80
        $fileName = $this->getPath().'/'.$translatedTo.'.php';
81 View Code Duplication
        if (! file_exists($fileName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
82
            $fileName = $this->getCorePath().'/'.$translatedTo.'.php';
83
            if (! file_exists($fileName)) {
84
                throw new \InvalidArgumentException("Language $translatedTo not found.");
85
            }
86
        }
87
88
        $list = include $fileName;
89
        if (!is_array($list)) {
90
            throw new \InvalidArgumentException("Language $translatedTo not found.");
91
        }
92
        $country = strtoupper($country);
93
        if (!array_key_exists($country, $list)) {
94
            throw new \InvalidArgumentException("Country $country not found for $translatedTo.");
95
        }
96
97
        return $list[$country];
98
    }
99
}
100