Test Failed
Push — uuid_annotation ( d6506f )
by David
06:37
created

Annotations   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAnnotations() 0 4 1
A findAnnotations() 0 6 1
A findAnnotation() 0 13 3
1
<?php
2
3
4
namespace TheCodingMachine\TDBM\Utils\Annotation;
5
6
use TheCodingMachine\TDBM\TDBMException;
7
8
/**
9
 * Represents a list of annotations in a column comment.
10
 */
11
class Annotations
12
{
13
    /**
14
     * @var array|Annotation[]
15
     */
16
    private $annotations;
17
18
    /**
19
     * @param Annotation[] $annotations
20
     */
21
    public function __construct(array $annotations)
22
    {
23
        $this->annotations = $annotations;
24
    }
25
26
    /**
27
     * @return array|Annotation[]
28
     */
29
    public function getAnnotations(): array
30
    {
31
        return $this->annotations;
32
    }
33
34
    /**
35
     * @param string $annotationType
36
     * @return Annotation[]
37
     */
38
    public function findAnnotations(string $annotationType): array
39
    {
40
        return array_filter($this->annotations, function (Annotation $annotation) use ($annotationType) {
41
            return $annotation->getAnnotationType() === $annotationType;
42
        });
43
    }
44
45
    public function findAnnotation(string $annotationType): ?Annotation
46
    {
47
        $annotations = $this->findAnnotations($annotationType);
48
49
        $annotationsCount = count($annotations);
50
        if ($annotationsCount === 1) {
51
            return $annotations[0];
52
        } elseif ($annotationsCount === 0) {
53
            return null;
54
        } else {
55
            throw new TDBMException(sprintf('Unexpected column annotation. Found %d annotations of type %s', $annotationsCount, $annotationType));
56
        }
57
    }
58
}
59