Flip   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 41
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A applicableFeature() 0 11 3
A buildFeatures() 0 6 2
A __call() 0 7 2
1
<?php
2
3
namespace Vehikl\Flip;
4
5
class Flip
6
{
7
    private $class;
8
    private $features;
9
10 8
    public function __construct($class, array $features = [])
11
    {
12 8
        $this->class = $class;
13 8
        $this->features = $features;
14 8
    }
15
16 8
    private function applicableFeature(string $method): ?Feature
17
    {
18
        // Probably want a factory class to resolve any dependencies and cache
19 8
        foreach ($this->buildFeatures() as $feature) {
20 8
            if ($feature->hasToggle($method)) {
21 8
                return $feature;
22
            }
23
        }
24
25 2
        return null;
26
    }
27
28
    /**
29
     * @return \Generator|Feature[]
30
     */
31 8
    private function buildFeatures(): \Generator
32
    {
33 8
        foreach ($this->features as $feature) {
34 8
            yield new $feature($this->class);
35
        }
36 2
    }
37
38 8
    public function __call($method, $arguments)
39
    {
40
        // What happens if a method applies to multiple features?
41 8
        $first = $this->applicableFeature($method) ?: $this->class;
42
43 8
        return $first->{$method}($arguments);
44
    }
45
}
46