AnnotationCollection::findOneBy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Er1z\FakeMock\Annotations;
4
5
class AnnotationCollection
6
{
7
    /**
8
     * @var array
9
     */
10
    private $annotations;
11
12 159
    public function __construct($annotations = [])
13
    {
14 159
        $this->annotations = $annotations;
15 159
    }
16
17 4
    public function getOneBy($class)
18
    {
19 4
        if ($result = $this->findOneBy($class)) {
20 4
            return $result;
21
        }
22
23 4
        throw new \InvalidArgumentException(sprintf('Annotation %s not found in collection', $class));
24
    }
25
26 14
    public function findOneBy($class)
27
    {
28 14
        $result = array_values($this->findAllBy($class))[0] ?? null;
29
30 14
        return $result;
31
    }
32
33 77
    public function findAllBy($class)
34
    {
35
        $result = array_filter($this->annotations, function ($a) use ($class) {
36 66
            return $a instanceof $class;
37 77
        });
38
39 77
        return array_values($result);
40
    }
41
}
42