Conditions | 10 |
Paths | 40 |
Total Lines | 47 |
Code Lines | 24 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
30 | public function createExamples(array $defenitions): array |
||
31 | { |
||
32 | $examples = []; |
||
33 | $context = null; |
||
34 | |||
35 | foreach ($defenitions as $index => $def) { |
||
36 | if ($this->isAnnotatedWith('ignore', $def['annotations'])) { |
||
37 | continue; |
||
38 | } |
||
39 | |||
40 | $name = $this->readAnnotation('example', $def['annotations']) ?: (string)($index + 1); |
||
41 | |||
42 | if (isset($examples[$name])) { |
||
43 | throw new \RuntimeException("Example '$name' already exists in definition ".($index + 1)); |
||
44 | } |
||
45 | |||
46 | $codeBlock = new CodeBlock($def['code']); |
||
47 | |||
48 | if ($context) { |
||
49 | $codeBlock->prepend($context); |
||
50 | } |
||
51 | |||
52 | if ($extends = $this->readAnnotation('extends', $def['annotations'])) { |
||
53 | if (!isset($examples[$extends])) { |
||
54 | throw new \RuntimeException( |
||
55 | "Example '$extends' does not exist and can not be extended in definition ".($index + 1) |
||
56 | ); |
||
57 | } |
||
58 | |||
59 | $codeBlock->prepend($examples[$extends]->getCodeBlock()); |
||
60 | } |
||
61 | |||
62 | $expectations = $this->createExpectations($def['annotations']); |
||
63 | |||
64 | if (empty($expectations)) { |
||
65 | $expectations[] = $this->expectationFactory->createExpectation('expectnothing', []); |
||
66 | } |
||
67 | |||
68 | $examples[$name] = new Example($name, $codeBlock, $expectations); |
||
69 | |||
70 | if ($this->isAnnotatedWith('exampleContext', $def['annotations'])) { |
||
71 | $context = $examples[$name]->getCodeBlock(); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | return $examples; |
||
76 | } |
||
77 | |||
122 |