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

FieldMap   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 7 2
A set() 0 9 2
A has() 0 3 1
A getIterator() 0 3 1
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\Field;
13
use Cycle\Schema\Exception\FieldException;
14
15
/**
16
 * Manage the set of fields associated with the entity.
17
 */
18
final class FieldMap implements \IteratorAggregate
19
{
20
    /** @var Field[] */
21
    private $fields = [];
22
23
    /**
24
     * @param string $name
25
     * @return bool
26
     */
27
    public function has(string $name): bool
28
    {
29
        return isset($this->fields[$name]);
30
    }
31
32
    /**
33
     * @param string $name
34
     * @return Field
35
     */
36
    public function get(string $name): Field
37
    {
38
        if (!$this->has($name)) {
39
            throw new FieldException("Undefined field `{$name}`");
40
        }
41
42
        return $this->fields[$name];
43
    }
44
45
    /**
46
     * @param string $name
47
     * @param Field  $field
48
     * @return FieldMap
49
     */
50
    public function set(string $name, Field $field): self
51
    {
52
        if ($this->has($name)) {
53
            throw new FieldException("Field `{$name}` already exists");
54
        }
55
56
        $this->fields[$name] = $field;
57
58
        return $this;
59
    }
60
61
    /**
62
     * @return Field[]|\Traversable
63
     */
64
    public function getIterator()
65
    {
66
        return $this->fields;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fields returns the type Cycle\Schema\Definition\Field[] 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...
67
    }
68
}