Completed
Push — master ( 0aa69f...1f2f47 )
by Hannes
04:03
created

ExampleFactory::__construct()   A

Complexity

Conditions 2
Paths 2

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 2
eloc 2
nc 2
nop 1
1
<?php
2
3
namespace hanneskod\readmetester;
4
5
use hanneskod\readmetester\Expectation\ExpectationFactory;
6
7
/**
8
 * Create examples from definitions created by Parser
9
 */
10
class ExampleFactory
11
{
12
    /**
13
     * @var ExpectationFactory
14
     */
15
    private $expectationFactory;
16
17
    public function __construct(ExpectationFactory $expectationFactory = null)
18
    {
19
        $this->expectationFactory = $expectationFactory ?: new ExpectationFactory;
20
    }
21
22
    /**
23
     * Create examples from definitions
24
     *
25
     * @param  array $defenitions Example definitions as created by Parser
26
     * @return Example[]
27
     */
28
    public function createExamples(array $defenitions)
29
    {
30
        $examples = [];
31
32
        foreach ($defenitions as $index => $def) {
33
            if ($this->shouldIgnoreExample($def['annotations'])) {
34
                continue;
35
            }
36
37
            $name = $this->readName($def['annotations']) ?: (string)$index + 1;
38
39
            if (isset($examples[$name])) {
40
                throw new \RuntimeException("Example '$name' already exists in definition " . ($index + 1));
41
            }
42
43
            $examples[$name] = new Example(
44
                $name,
45
                new CodeBlock($def['code']),
46
                $this->createExpectations($def['annotations'])
47
            );
48
        }
49
50
        return $examples;
51
    }
52
53
    /**
54
     * Read name from example annotation
55
     *
56
     * @return string
57
     */
58
    private function readName(array $annotations)
59
    {
60
        foreach ($annotations as list($name, $args)) {
61
            if (strcasecmp($name, 'example') == 0) {
62
                return isset($args[0]) ? $args[0] : '';
63
            }
64
        }
65
66
        return '';
67
    }
68
69
    /**
70
     * Check if this example is marked as ignored
71
     *
72
     * @return boolean
73
     */
74
    private function shouldIgnoreExample(array $annotations)
75
    {
76
        foreach ($annotations as list($name, $args)) {
77
            if (strcasecmp($name, 'ignore') == 0) {
78
                return true;
79
            }
80
        }
81
82
        return false;
83
    }
84
85
    /**
86
     * Create expectation from definition data
87
     *
88
     * @return Expectation\ExpectationInterface[]
89
     */
90
    private function createExpectations(array $annotations)
91
    {
92
        $expectations = [];
93
94
        foreach ($annotations as list($name, $args)) {
95
            $expectations[] = $this->expectationFactory->createExpectation($name, $args);
96
        }
97
98
        return array_filter($expectations);
99
    }
100
}
101