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