Completed
Push — master ( 098545...a17288 )
by Derek Stephen
02:03
created

CountryRepository::findCountryBy()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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