| Conditions | 32 |
| Paths | 48 |
| Total Lines | 71 |
| 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 |
||
| 59 | public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0): string |
||
| 60 | { |
||
| 61 | if ($input instanceof CommentedItem) { |
||
| 62 | //$comment = $input->getComment(); |
||
| 63 | $output = '# '.$input->getComment()."\n"; |
||
| 64 | $input = $input->getItem(); |
||
| 65 | } else { |
||
| 66 | //$comment = null; |
||
| 67 | $output = ''; |
||
| 68 | } |
||
| 69 | |||
| 70 | // $output = ''; |
||
| 71 | $prefix = $indent ? str_repeat(' ', $indent) : ''; |
||
| 72 | $dumpObjectAsInlineMap = true; |
||
| 73 | |||
| 74 | if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) { |
||
| 75 | $dumpObjectAsInlineMap = empty((array) $input); |
||
| 76 | } |
||
| 77 | |||
| 78 | if ($inline <= 0 || (!\is_array($input) && $dumpObjectAsInlineMap) || empty($input)) { |
||
| 79 | $output .= $prefix.Inline::dump($input, $flags); |
||
| 80 | } else { |
||
| 81 | $dumpAsMap = Inline::isHash($input); |
||
| 82 | |||
| 83 | foreach ($input as $key => $value) { |
||
| 84 | if ($value instanceof CommentedItem) { |
||
| 85 | $comment = $value->getComment(); |
||
| 86 | $value = $value->getItem(); |
||
| 87 | } else { |
||
| 88 | $comment = null; |
||
| 89 | } |
||
| 90 | |||
| 91 | if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r\n")) { |
||
| 92 | // If the first line starts with a space character, the spec requires a blockIndicationIndicator |
||
| 93 | // http://www.yaml.org/spec/1.2/spec.html#id2793979 |
||
| 94 | $blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : ''; |
||
| 95 | $output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator); |
||
| 96 | |||
| 97 | $pregSplit = preg_split('/\n|\r\n/', $value); |
||
| 98 | if ($pregSplit === false) { |
||
| 99 | throw new \RuntimeException('An error occured in preg_split'); |
||
| 100 | } |
||
| 101 | foreach ($pregSplit as $row) { |
||
| 102 | $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row); |
||
| 103 | } |
||
| 104 | |||
| 105 | continue; |
||
| 106 | } |
||
| 107 | |||
| 108 | $dumpObjectAsInlineMap = true; |
||
| 109 | |||
| 110 | if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) { |
||
| 111 | $dumpObjectAsInlineMap = empty((array) $value); |
||
| 112 | } |
||
| 113 | |||
| 114 | $willBeInlined = $inline - 1 <= 0 || !\is_array($value) && $dumpObjectAsInlineMap || empty($value); |
||
| 115 | |||
| 116 | if ($comment) { |
||
| 117 | $output .= sprintf("%s# %s\n", $prefix, $comment); |
||
| 118 | } |
||
| 119 | $output .= sprintf( |
||
| 120 | '%s%s%s%s', |
||
| 121 | $prefix, |
||
| 122 | $dumpAsMap ? Inline::dump($key, $flags).':' : '-', |
||
| 123 | $willBeInlined ? ' ' : "\n", |
||
| 124 | $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags) |
||
| 125 | ).($willBeInlined ? "\n" : ''); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | return $output; |
||
| 130 | } |
||
| 132 |