| Conditions | 8 |
| Paths | 9 |
| Total Lines | 58 |
| Code Lines | 35 |
| 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 |
||
| 88 | public function citeText(string $string): string |
||
| 89 | { |
||
| 90 | if (empty($string)) { |
||
| 91 | return ''; |
||
| 92 | } |
||
| 93 | $quoteSymbol = $this->settings->get('quote_symbol'); |
||
| 94 | $out = ''; |
||
| 95 | // split already quoted lines |
||
| 96 | $citeLines = preg_split( |
||
| 97 | "/(^{$quoteSymbol}.*?$\n)/m", |
||
| 98 | $string, |
||
| 99 | -1, |
||
| 100 | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY |
||
| 101 | ); |
||
| 102 | foreach ($citeLines as $citeLine) { |
||
| 103 | if (mb_strpos($citeLine, $quoteSymbol) === 0) { |
||
| 104 | // already quoted lines need no further processing |
||
| 105 | $out .= $citeLine; |
||
| 106 | continue; |
||
| 107 | } |
||
| 108 | // split [bbcode] |
||
| 109 | $matches = preg_split( |
||
| 110 | '`(\[(.+?)=?.*?\].+?\[/\2\])`', |
||
| 111 | $citeLine, |
||
| 112 | -1, |
||
| 113 | PREG_SPLIT_DELIM_CAPTURE |
||
| 114 | ); |
||
| 115 | $i = 0; |
||
| 116 | $line = ''; |
||
| 117 | foreach ($matches as $match) { |
||
| 118 | // the [bbcode] preg_split uses a backreference \2 which is in the $matches |
||
| 119 | // but is not needed in the results |
||
| 120 | // @td @sm elegant solution |
||
| 121 | $i++; |
||
| 122 | if ($i % 3 == 0) { |
||
| 123 | continue; |
||
| 124 | } |
||
| 125 | // wrap long lines |
||
| 126 | if (mb_strpos($match, '[') !== 0) { |
||
| 127 | $line .= wordwrap($match); |
||
| 128 | } else { |
||
| 129 | $line .= $match; |
||
| 130 | } |
||
| 131 | // add newline to wrapped lines |
||
| 132 | if (mb_strlen($line) > 60) { |
||
| 133 | $out .= $line . "\n"; |
||
| 134 | $line = ''; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | $out .= $line; |
||
| 138 | } |
||
| 139 | $out = preg_replace( |
||
| 140 | "/^/m", |
||
| 141 | $quoteSymbol . " ", |
||
| 142 | $out |
||
| 143 | ); |
||
| 144 | |||
| 145 | return $out; |
||
| 146 | } |
||
| 174 |