Completed
Push — master ( b7ad07...7762ce )
by Hannes
02:04
created

ExampleFactory::createExamples()   D

Complexity

Conditions 10
Paths 40

Size

Total Lines 45
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 23
nc 40
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\readmetester;
6
7
use hanneskod\readmetester\Expectation\ExpectationFactory;
8
9
/**
10
 * Create examples from definitions created by Parser
11
 */
12
class ExampleFactory
13
{
14
    /**
15
     * @var ExpectationFactory
16
     */
17
    private $expectationFactory;
18
19
    public function __construct(ExpectationFactory $expectationFactory = null)
20
    {
21
        $this->expectationFactory = $expectationFactory ?: new ExpectationFactory;
22
    }
23
24
    /**
25
     * Create examples from definitions
26
     *
27
     * @param  Definition[] $defenitions Example definitions as created by Parser
28
     * @return Example[]
29
     */
30
    public function createExamples(array $defenitions): array
31
    {
32
        $examples = [];
33
        $context = null;
34
35
        foreach ($defenitions as $index => $def) {
36
            if ($def->isAnnotatedWith('ignore')) {
37
                continue;
38
            }
39
40
            $name = $def->readAnnotation('example') ?: (string)($index + 1);
41
42
            if (isset($examples[$name])) {
43
                throw new \RuntimeException("Example '$name' already exists in definition ".($index + 1));
44
            }
45
46
            if ($context) {
47
                $def->getCodeBlock()->prepend($context);
48
            }
49
50
            if ($extends = $def->readAnnotation('extends')) {
51
                if (!isset($examples[$extends])) {
52
                    throw new \RuntimeException(
53
                        "Example '$extends' does not exist and can not be extended in definition ".($index + 1)
54
                    );
55
                }
56
57
                $def->getCodeBlock()->prepend($examples[$extends]->getCodeBlock());
58
            }
59
60
            $expectations = $this->createExpectations($def);
61
62
            if (empty($expectations)) {
63
                $expectations[] = $this->expectationFactory->createExpectation('expectnothing', []);
64
            }
65
66
            $examples[$name] = new Example($name, $def->getCodeBlock(), $expectations);
67
68
            if ($def->isAnnotatedWith('exampleContext')) {
69
                $context = $def->getCodeBlock();
70
            }
71
        }
72
73
        return $examples;
74
    }
75
76
    /**
77
     * Create expectation from definition data
78
     *
79
     * @return Expectation\ExpectationInterface[]
80
     */
81
    private function createExpectations(Definition $def): array
82
    {
83
        $expectations = [];
84
85
        foreach ($def->getAnnotations() as list($name, $args)) {
86
            $expectations[] = $this->expectationFactory->createExpectation($name, $args);
87
        }
88
89
        return array_filter($expectations);
90
    }
91
}
92