Completed
Push — 2.x ( db763c...24284c )
by Aleksei
05:34 queued 05:33
created

ForeignKeyMap::__clone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
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
    /**
22
     * Cloning.
23
     */
24
    public function __clone()
25
    {
26
        foreach ($this->foreignKeys as $index => $foreignKey) {
27
            $this->foreignKeys[$index] = clone $foreignKey;
28
        }
29
    }
30
31
    public function has(ForeignKey $foreignKey): bool
32
    {
33
        return isset($this->foreignKeys[$this->generateIdentifier($foreignKey)]);
34
    }
35
36
    public function set(ForeignKey $foreignKey): self
37
    {
38
        $this->foreignKeys[$this->generateIdentifier($foreignKey)] = $foreignKey;
39
40
        return $this;
41
    }
42
43
    public function remove(ForeignKey $foreignKey): self
44
    {
45
        unset($this->foreignKeys[$this->generateIdentifier($foreignKey)]);
46
47
        return $this;
48
    }
49
50
    public function count(): int
51
    {
52
        return \count($this->foreignKeys);
53
    }
54
55
    public function getIterator(): \Traversable
56
    {
57
        return new \ArrayIterator($this->foreignKeys);
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