| Conditions | 17 |
| Paths | 169 |
| Total Lines | 74 |
| Code Lines | 42 |
| 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 = ''; |
||
| 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 | $name = $annotation->getArgument() ?: (string)($index + 1); |
||
| 71 | continue; |
||
| 72 | } |
||
| 73 | |||
| 74 | if ($annotation->isNamed(Annotations::ANNOTATION_INCLUDE)) { |
||
| 75 | $toInclude = $annotation->getArgument(); |
||
| 76 | |||
| 77 | if (!isset($examples[$toInclude])) { |
||
| 78 | throw new \RuntimeException( |
||
| 79 | "Example '$toInclude' does not exist and can not be included in definition $name" |
||
| 80 | ); |
||
| 81 | } |
||
| 82 | |||
| 83 | $code = $code->prepend($examples[$toInclude]->getCodeBlock()); |
||
| 84 | continue; |
||
| 85 | } |
||
| 86 | |||
| 87 | if ($expectation = $this->expectationFactory->createExpectation($annotation)) { |
||
| 88 | if ($expectation instanceof ExpectationInterface) { |
||
| 89 | $expectations[] = $expectation; |
||
| 90 | continue; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | if ($annotation->isNamed(Annotations::ANNOTATION_CONTEXT)) { |
||
| 95 | $context = $code; |
||
| 96 | continue; |
||
| 97 | } |
||
| 98 | |||
| 99 | if (!$this->ignoreUnknownAnnotations) { |
||
| 100 | throw new \RuntimeException("Unknown annotation @{$annotation->getName()}"); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | if (isset($examples[$name])) { |
||
| 105 | throw new \RuntimeException("Example '$name' already exists in definition " . ($index + 1)); |
||
| 106 | } |
||
| 107 | |||
| 108 | if (!$this->filter->isValid($name)) { |
||
| 109 | $ignoreExample = true; |
||
| 110 | } |
||
| 111 | |||
| 112 | if (!$name) { |
||
| 113 | $name = (string)($index + 1); |
||
| 114 | } |
||
| 115 | |||
| 116 | $examples[$name] = $ignoreExample |
||
| 117 | ? new IgnoredExample($name, $code, $expectations) |
||
| 118 | : new Example($name, $code, $expectations); |
||
| 119 | } |
||
| 120 | |||
| 121 | return $examples; |
||
| 122 | } |
||
| 124 |