Conditions | 16 |
Paths | 107 |
Total Lines | 72 |
Code Lines | 41 |
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 |
||
48 | public function createExamples(Definition ...$defs): array |
||
49 | { |
||
50 | $examples = []; |
||
51 | $context = null; |
||
52 | |||
53 | foreach ($defs as $index => $def) { |
||
54 | $name = new AnonymousName(''); |
||
55 | $code = $def->getCodeBlock(); |
||
56 | $expectations = []; |
||
57 | $ignoreExample = false; |
||
58 | |||
59 | if ($context) { |
||
60 | $code = $code->prepend($context); |
||
|
|||
61 | } |
||
62 | |||
63 | foreach ($def->getAnnotations() as $annotation) { |
||
64 | if ($annotation->isNamed(Annotations::ANNOTATION_IGNORE)) { |
||
65 | $ignoreExample = true; |
||
66 | continue; |
||
67 | } |
||
68 | |||
69 | if ($annotation->isNamed(Annotations::ANNOTATION_EXAMPLE)) { |
||
70 | if ($annotation->getArgument()) { |
||
71 | $name = new ExampleName($annotation->getArgument(), $name->getNamespaceName()); |
||
72 | } |
||
73 | continue; |
||
74 | } |
||
75 | |||
76 | if ($annotation->isNamed(Annotations::ANNOTATION_INCLUDE)) { |
||
77 | $toInclude = $this->readExample($examples, new ExampleName($annotation->getArgument(), '')); |
||
78 | |||
79 | if (is_null($toInclude)) { |
||
80 | throw new \RuntimeException( |
||
81 | "Example '{$annotation->getArgument()}' can not be included in {$name->getShortName()}" |
||
82 | ); |
||
83 | } |
||
84 | |||
85 | $code = $code->prepend($toInclude->getCodeBlock()); |
||
86 | continue; |
||
87 | } |
||
88 | |||
89 | if ($expectation = $this->expectationFactory->createExpectation($annotation)) { |
||
90 | if ($expectation instanceof ExpectationInterface) { |
||
91 | $expectations[] = $expectation; |
||
92 | continue; |
||
93 | } |
||
94 | } |
||
95 | |||
96 | if ($annotation->isNamed(Annotations::ANNOTATION_CONTEXT)) { |
||
97 | $context = $code; |
||
98 | continue; |
||
99 | } |
||
100 | |||
101 | if (!$this->ignoreUnknownAnnotations) { |
||
102 | throw new \RuntimeException("Unknown annotation @{$annotation->getName()}"); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | if ($this->readExample($examples, $name)) { |
||
107 | throw new \RuntimeException("Example '{$name->getShortName()}' already exists"); |
||
108 | } |
||
109 | |||
110 | if (!$this->filter->isValid($name->getCompleteName())) { |
||
111 | $ignoreExample = true; |
||
112 | } |
||
113 | |||
114 | $examples[] = $ignoreExample |
||
115 | ? new IgnoredExample($name, $code, $expectations) |
||
116 | : new Example($name, $code, $expectations); |
||
117 | } |
||
118 | |||
119 | return $examples; |
||
120 | } |
||
133 |