Completed
Push — master ( 01562b...a268a7 )
by Derek Stephen
02:38
created

CountryRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Del\Repository;
4
5
use Del\Exception\CountryException;
6
use Del\Entity\Country;
7
use Del\Factory\CountryList;
8
9
class CountryRepository
10
{
11
    /** @var Country[] $countries */
12
    private $countries;
13
14
    /**
15
     * CountryRepository constructor.
16
     */
17 1
    public function __construct()
18
    {
19 1
        $list = new CountryList();
20 1
        $this->countries = $list->getCountries();
21 1
    }
22
23
    /**
24
     * @return Country[]
25
     */
26
    public function findAllCountries(): array
27
    {
28
        $countries = [];
29
30
        foreach ($this->countries as $country) {
31
            $countries[] = $this->createFromArray($country);
0 ignored issues
show
Documentation introduced by
$country is of type object<Del\Entity\Country>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
32
        }
33
34
        return $countries;
35
    }
36
37
    /**
38
     * @param string $id
39
     * @return Country
40
     * @throws CountryException
41
     */
42 3
    public function findCountryByIsoCode(string $id): Country
43
    {
44 3
        if (isset($this->countries[$id])) {
45 2
            return $this->createFromArray($this->countries[$id]);
0 ignored issues
show
Documentation introduced by
$this->countries[$id] is of type object<Del\Entity\Country>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
        }
47
48 1
        throw new CountryException(CountryException::ERROR_NOT_FOUND);
49
    }
50
51
    /**
52
     * @param string $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
53
     * @return Country
54
     * @throws CountryException
55
     */
56
    public function findCountryBy(string $key, string $value): Country
57
    {
58
        foreach ($this->countries as $country) {
59
            if ($country[$key] === $value) {
60
                return $this->createFromArray($this->countries[$id]);
0 ignored issues
show
Bug introduced by
The variable $id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Documentation introduced by
$this->countries[$id] is of type object<Del\Entity\Country>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61
            }
62
        }
63
64
        throw new CountryException(CountryException::ERROR_NOT_FOUND);
65
    }
66
67
    /**
68
     * @param array $data
69
     * @return Country
70
     */
71 2
    public function createFromArray(array $data): Country
72
    {
73 2
        $country = new Country();
74 2
        $country->setId($data['id']);
75 2
        $country->setIso($data['iso']);
76 2
        $country->setName($data['name']);
77 2
        $country->setNumCode($data['numcode']);
78 2
        $country->setFlag($data['flag']);
79
80 2
        return $country;
81
    }
82
}