Conditions | 14 |
Paths | 113 |
Total Lines | 70 |
Code Lines | 40 |
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 |
||
37 | public function createExamples(Definition ...$defs): array |
||
38 | { |
||
39 | $examples = []; |
||
40 | $context = null; |
||
41 | |||
42 | foreach ($defs as $index => $def) { |
||
43 | $name = ''; |
||
44 | $code = $def->getCodeBlock(); |
||
45 | $expectations = []; |
||
46 | $ignoreExample = false; |
||
47 | |||
48 | if ($context) { |
||
49 | $code = $code->prepend($context); |
||
50 | } |
||
51 | |||
52 | foreach ($def->getAnnotations() as $annotation) { |
||
53 | if ($annotation->isNamed('ignore')) { |
||
54 | $ignoreExample = true; |
||
55 | continue; |
||
56 | } |
||
57 | |||
58 | if ($annotation->isNamed('example')) { |
||
59 | $name = $annotation->getArgument(); |
||
60 | continue; |
||
61 | } |
||
62 | |||
63 | if ($annotation->isNamed('include')) { |
||
64 | $toInclude = $annotation->getArgument(); |
||
65 | |||
66 | if (!isset($examples[$toInclude])) { |
||
67 | throw new \RuntimeException( |
||
68 | "Example '$toInclude' does not exist and can not be included in definition ".($index + 1) |
||
69 | ); |
||
70 | } |
||
71 | |||
72 | $code = $code->prepend($examples[$toInclude]->getCodeBlock()); |
||
73 | continue; |
||
74 | } |
||
75 | |||
76 | if ($expectation = $this->expectationFactory->createExpectation($annotation)) { |
||
77 | $expectations[] = $expectation; |
||
78 | continue; |
||
79 | } |
||
80 | |||
81 | if ($annotation->isNamed('exampleContext')) { |
||
82 | $context = $code; |
||
83 | continue; |
||
84 | } |
||
85 | |||
86 | throw new \RuntimeException("Unknown annotation @{$annotation->getName()}"); |
||
87 | } |
||
88 | |||
89 | if (isset($examples[$name])) { |
||
90 | throw new \RuntimeException("Example '$name' already exists in definition ".($index + 1)); |
||
91 | } |
||
92 | |||
93 | if (!$this->filter->isValid($name)) { |
||
94 | $ignoreExample = true; |
||
95 | } |
||
96 | |||
97 | if (!$name) { |
||
98 | $name = (string)($index + 1); |
||
99 | } |
||
100 | |||
101 | $examples[$name] = $ignoreExample |
||
102 | ? new IgnoredExample($name, $code, $expectations) |
||
103 | : new Example($name, $code, $expectations); |
||
104 | } |
||
105 | |||
106 | return $examples; |
||
107 | } |
||
109 |