ForeignKeyMap   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 56
rs 10
c 2
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A generateIdentifier() 0 7 1
A has() 0 3 1
A set() 0 5 1
A __clone() 0 4 2
A remove() 0 5 1
A count() 0 3 1
A getIterator() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Definition\Map;
6
7
use Cycle\Schema\Definition\ForeignKey;
8
9
/**
10
 * Manage the set of foreign keys associated with the entity.
11
 *
12
 * @implements \IteratorAggregate<non-empty-string, ForeignKey>
13
 */
14
final class ForeignKeyMap implements \IteratorAggregate, \Countable
15
{
16
    /**
17
     * @var array<non-empty-string, ForeignKey>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, ForeignKey> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, ForeignKey>.
Loading history...
18
     */
19
    private array $foreignKeys = [];
20
21
    public function has(ForeignKey $foreignKey): bool
22
    {
23
        return isset($this->foreignKeys[$this->generateIdentifier($foreignKey)]);
24
    }
25
26
    public function set(ForeignKey $foreignKey): self
27
    {
28
        $this->foreignKeys[$this->generateIdentifier($foreignKey)] = $foreignKey;
29
30
        return $this;
31
    }
32
33
    public function remove(ForeignKey $foreignKey): self
34
    {
35
        unset($this->foreignKeys[$this->generateIdentifier($foreignKey)]);
36
37
        return $this;
38
    }
39
40
    public function count(): int
41
    {
42
        return \count($this->foreignKeys);
43
    }
44
45
    public function getIterator(): \Traversable
46
    {
47
        return new \ArrayIterator($this->foreignKeys);
48
    }
49
50
    /**
51
     * Cloning.
52
     */
53
    public function __clone()
54
    {
55
        foreach ($this->foreignKeys as $index => $foreignKey) {
56
            $this->foreignKeys[$index] = clone $foreignKey;
57
        }
58
    }
59
60
    /**
61
     * @return non-empty-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
62
     */
63
    private function generateIdentifier(ForeignKey $foreignKey): string
64
    {
65
        return \sprintf(
66
            '%s:%s:%s',
67
            $foreignKey->getTarget(),
68
            \implode(',', $foreignKey->getInnerColumns()),
69
            \implode(',', $foreignKey->getOuterColumns()),
70
        );
71
    }
72
}
73