Completed
Push — master ( 7762ce...618a95 )
by Hannes
02:39
created

Definition::getCodeBlock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\readmetester;
6
7
/**
8
 * Wrapper around the code and annotations defining an example
9
 */
10
class Definition
11
{
12
    /**
13
     * @var Annotation[] The set of annotations
14
     */
15
    private $annotations;
16
17
    /**
18
     * @var CodeBlock Example code
19
     */
20
    private $code;
21
22
    public function __construct(CodeBlock $code, Annotation ...$annotations)
23
    {
24
        $this->code = $code;
25
        $this->annotations = $annotations;
26
    }
27
28
    public function getCodeBlock(): CodeBlock
29
    {
30
        return $this->code;
31
    }
32
33
    /**
34
     * @return Annotation[]
35
     */
36
    public function getAnnotations(): array
37
    {
38
        return $this->annotations;
39
    }
40
41
    /**
42
     * Get annotation matching $name
43
     */
44
    public function getAnnotation(string $name): Annotation
45
    {
46
        foreach ($this->getAnnotations() as $annotation) {
47
            if ($annotation->isNamed($name)) {
48
                return $annotation;
49
            }
50
        }
51
52
        throw new \LogicException("Annotation $name does not exist");
53
    }
54
55
    /**
56
     * Check if annotation $name exists
57
     */
58
    public function isAnnotatedWith(string $name): bool
59
    {
60
        foreach ($this->getAnnotations() as $annotation) {
61
            if ($annotation->isNamed($name)) {
62
                return true;
63
            }
64
        }
65
66
        return false;
67
    }
68
}
69