Passed
Push — master ( e4edfb...fb7384 )
by Anton
01:31
created

RelationMap::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
namespace Cycle\Schema\Definition\Map;
11
12
use Cycle\Schema\Definition\Relation;
13
use Cycle\Schema\Exception\RelationException;
14
15
class RelationMap implements \IteratorAggregate
16
{
17
    /** @var Relation[] */
18
    private $fields = [];
19
20
    /**
21
     * @param string $name
22
     * @return bool
23
     */
24
    public function has(string $name): bool
25
    {
26
        return isset($this->fields[$name]);
27
    }
28
29
    /**
30
     * @param string $name
31
     * @return Relation
32
     */
33
    public function get(string $name): Relation
34
    {
35
        if (!$this->has($name)) {
36
            throw new RelationException("Undefined relation `{$name}`");
37
        }
38
39
        return $this->fields[$name];
40
    }
41
42
    /**
43
     * @param string   $name
44
     * @param Relation $relation
45
     * @return RelationMap
46
     */
47
    public function set(string $name, Relation $relation): self
48
    {
49
        if ($this->has($name)) {
50
            throw new RelationException("Relation `{$name}` already exists");
51
        }
52
53
        $this->fields[$name] = $relation;
54
55
        return $this;
56
    }
57
58
    /**
59
     * @return Relation[]|\Traversable
60
     */
61
    public function getIterator()
62
    {
63
        return $this->fields;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fields returns the type Cycle\Schema\Definition\Relation[] which is incompatible with the return type mandated by IteratorAggregate::getIterator() of Traversable.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
64
    }
65
}