Completed
Push — master ( a17288...aa8e2a )
by Derek Stephen
01:49
created

CountryRepository::findAllCountries()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Del\Repository;
4
5
use Del\CountryException;
6
use Del\Entity\Country;
7
8
class CountryRepository
9
{
10
    /** @var Country[] $countries */
11
    private $countries;
12
13
    /**
14
     * CountryRepository constructor.
15
     */
16 1
    public function __construct()
17
    {
18 1
        $this->countries = require_once __DIR__ . '/../Factory/countries.php';
19 1
    }
20
21
    /**
22
     * @return Country[]
23
     */
24
    public function findAllCountries(): array
25
    {
26
        $countries = [];
27
28
        foreach ($this->countries as $country) {
29
            $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...
30
        }
31
32
        return $countries;
33
    }
34
35
    /**
36
     * @param string $id
37
     * @return Country
38
     * @throws CountryException
39
     */
40 3
    public function findCountryByIsoCode(string $id): Country
41
    {
42 3
        if (isset($this->countries[$id])) {
43
            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...
44
        }
45
46 3
        throw new CountryException(CountryException::ERROR_NOT_FOUND);
47
    }
48
49
    /**
50
     * @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...
51
     * @return Country
52
     * @throws CountryException
53
     */
54
    public function findCountryBy(string $key, string $value): Country
55
    {
56
        foreach ($this->countries as $country) {
57
            if ($country[$key] === $value) {
58
                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...
59
            }
60
        }
61
62
        throw new CountryException(CountryException::ERROR_NOT_FOUND);
63
    }
64
65
    /**
66
     * @param array $data
67
     * @return Country
68
     */
69
    public function createFromArray(array $data): Country
70
    {
71
        $country = new Country();
72
        $country->setId($data['id']);
73
        $country->setIso($data['iso']);
74
        $country->setName($data['name']);
75
        $country->setCountry($data['country']);
76
        $country->setNumCode($data['numcode']);
77
        $country->setFlag($data['flag']);
78
79
        return $country;
80
    }
81
}