Completed
Push — v5-dev ( a42eb3 )
by Oscar
01:56
created

References   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 12 3
A jsonSerialize() 0 4 1
A getIterator() 0 4 1
A count() 0 6 2
A toArray() 0 4 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Gettext;
5
6
use JsonSerializable;
7
use IteratorAggregate;
8
use ArrayIterator;
9
use Countable;
10
11
/**
12
 * Class to manage the references of a translation.
13
 */
14
class References implements JsonSerializable, Countable, IteratorAggregate
15
{
16
    protected $references = [];
17
18
    public function add(string $filename, ?int $line): self
19
    {
20
        $fileReferences = $this->references[$filename] ?? [];
21
22
        if (isset($line) && !in_array($line, $fileReferences)) {
23
            $fileReferences[] = $line;
24
        }
25
26
        $this->references[$filename] = $fileReferences;
27
28
        return $this;
29
    }
30
31
    public function jsonSerialize()
32
    {
33
        return $this->toArray();
34
    }
35
36
    public function getIterator()
37
    {
38
        return new ArrayIterator($this->references);
39
    }
40
41
    public function count(): int
42
    {
43
        return array_reduce($this->references, function ($carry, $item) {
44
            return $carry + (count($item) ?: 1);
45
        }, 0);
46
    }
47
48
    public function toArray(): array
49
    {
50
        return $this->references;
51
    }
52
}
53