FeatureContext::__get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * The Gherkish subpackage contains experimental support classes to implement
5
 * feature testing with a Gherkin inspired syntax.
6
 *
7
 * WARNING Classes in these package are not covered by semver and may change
8
 * without notice.
9
 *
10
 * See features/ for example usage
11
 */
12
13
declare(strict_types=1);
14
15
namespace hanneskod\readmetester\Gherkish;
16
17
final class FeatureContext
18
{
19
    /** @var array<string, \Closure> */
20
    private array $steps = [];
21
22
    /** @var array<string, mixed> */
23
    private array $properties = [];
24
25
    private \Closure $onTeardown;
26
27
    /**
28
     * Register closure to be called before step registration
29
     */
30
    public function setup(\Closure $setup): self
31
    {
32
        $setup = $setup->bindTo($this);
33
34
        if (!$setup) {
0 ignored issues
show
introduced by
$setup is of type Closure, thus it always evaluated to true.
Loading history...
35
            throw new \LogicException('Unable to register setup');
36
        }
37
38
        $setup();
39
40
        return $this;
41
    }
42
43
    /**
44
     * Register closure to be called after scenario execution
45
     */
46
    public function teardown(\Closure $teardown): self
47
    {
48
        $teardown = $teardown->bindTo($this);
49
50
        if (!$teardown) {
0 ignored issues
show
introduced by
$teardown is of type Closure, thus it always evaluated to true.
Loading history...
51
            throw new \LogicException('Unable to register teardown');
52
        }
53
54
        $this->onTeardown = $teardown;
55
56
        return $this;
57
    }
58
59
    /**
60
     * Register step
61
     */
62
    public function on(string $name, \Closure $step): self
63
    {
64
        $step = $step->bindTo($this);
65
66
        if (!$step) {
0 ignored issues
show
introduced by
$step is of type Closure, thus it always evaluated to true.
Loading history...
67
            throw new \LogicException('Unable to register step');
68
        }
69
70
        $this->steps[strtolower($name)] = $step;
71
72
        return $this;
73
    }
74
75
    /**
76
     * Register step using method overloading
77
     *
78
     * @param array<mixed> $arguments
79
     */
80
    public function __call(string $name, array $arguments): self
81
    {
82
        $step = $arguments[0] ?? null;
83
84
        if (!$step instanceof \Closure) {
85
            throw new \LogicException('Step must be registered with a closure');
86
        }
87
88
        return $this->on($name, $step);
89
    }
90
91
    /**
92
     * Execute step
93
     *
94
     * @param array<mixed> $arguments
95
     */
96
    public function do(string $name, array $arguments): void
97
    {
98
        $name = strtolower($name);
99
100
        if (!isset($this->steps[$name])) {
101
            throw new \Exception("No step $name");
102
        }
103
104
        ($this->steps[$name])(...$arguments);
105
    }
106
107
    public function __destruct()
108
    {
109
        if (isset($this->onTeardown)) {
110
            ($this->onTeardown)();
111
        }
112
    }
113
114
    public function __set(string $name, mixed $value): void
0 ignored issues
show
Bug introduced by
The type hanneskod\readmetester\Gherkish\mixed was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
115
    {
116
        $this->properties[$name] = $value;
117
    }
118
119
    public function &__get(string $name): mixed
120
    {
121
        if (!isset($this->properties[$name])) {
122
            throw new \Exception("no propery $name");
123
        }
124
125
        return $this->properties[$name];
126
    }
127
128
    /**
129
     * Get scenario for configuration
130
     */
131
    public function getScenario(): Scenario
132
    {
133
        return new Scenario($this);
134
    }
135
}
136