Issues (83)

src/Example/ExampleObj.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace hanneskod\readmetester\Example;
6
7
use hanneskod\readmetester\Attribute\AttributeInterface;
8
use hanneskod\readmetester\Expectation\ExpectationInterface;
9
use hanneskod\readmetester\Utils\NameObj;
10
use hanneskod\readmetester\Utils\CodeBlock;
11
12
class ExampleObj
13
{
14
    /**
15
     * @param array<AttributeInterface> $attributes
16
     * @param array<ExpectationInterface> $expectations
17
     * @param array<NameObj> $imports
18
     */
19
    public function __construct(
20
        private NameObj $name,
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_PRIVATE, expecting T_VARIABLE on line 20 at column 8
Loading history...
21
        private CodeBlock $code,
22
        private array $attributes = [],
23
        private array $expectations = [],
24
        private array $imports = [],
25
        private bool $active = true,
26
        private bool $context = false,
27
    ) {}
28
29
    /**
30
     * @return array<AttributeInterface>
31
     */
32
    public function getAttributes(): array
33
    {
34
        return $this->attributes;
35
    }
36
37
    public function hasAttribute(string $classname): bool
38
    {
39
        foreach ($this->getAttributes() as $attribute) {
40
            if ($attribute instanceof $classname) {
41
                return true;
42
            }
43
        }
44
45
        return false;
46
    }
47
48
    /**
49
     * Get example code block
50
     */
51
    public function getCodeBlock(): CodeBlock
52
    {
53
        return $this->code;
54
    }
55
56
    /**
57
     * @return array<ExpectationInterface>
58
     */
59
    public function getExpectations(): array
60
    {
61
        return $this->expectations;
62
    }
63
64
    /**
65
     * @return array<NameObj>
66
     */
67
    public function getImports(): array
68
    {
69
        return $this->imports;
70
    }
71
72
    /**
73
     * Get example name
74
     */
75
    public function getName(): NameObj
76
    {
77
        return $this->name;
78
    }
79
80
    /**
81
     * Check if example is active, eg. should be evaluated
82
     */
83
    public function isActive(): bool
84
    {
85
        return $this->active;
86
    }
87
88
    /**
89
     * Check of thes example should work as a context for other examples
90
     */
91
    public function isContext(): bool
92
    {
93
        return $this->context;
94
    }
95
96
    public function withActive(bool $active): ExampleObj
97
    {
98
        $new = clone $this;
99
        $new->active = $active;
100
        return $new;
101
    }
102
103
    public function withAttributes(AttributeInterface ...$attributes): ExampleObj
104
    {
105
        $new = clone $this;
106
        $new->attributes = [...$new->attributes, ...$attributes];
107
        return $new;
108
    }
109
110
    public function withCodeBlock(CodeBlock $code): ExampleObj
111
    {
112
        $new = clone $this;
113
        $new->code = $code;
114
        return $new;
115
    }
116
117
    public function withExpectation(ExpectationInterface $expectation): ExampleObj
118
    {
119
        $new = clone $this;
120
        $new->expectations[] = $expectation;
121
        return $new;
122
    }
123
124
    public function withImport(NameObj $name): ExampleObj
125
    {
126
        $new = clone $this;
127
        $new->imports[] = $name;
128
        return $new;
129
    }
130
131
    public function withIsContext(bool $flag): ExampleObj
132
    {
133
        $new = clone $this;
134
        $new->context = $flag;
135
        return $new;
136
    }
137
138
    public function withName(NameObj $name): ExampleObj
139
    {
140
        $new = clone $this;
141
        $new->name = $name;
142
        return $new;
143
    }
144
}
145