| Total Complexity | 73 |
| Total Lines | 295 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like HtmlUp often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HtmlUp, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class HtmlUp extends BlockElementParser |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Constructor. |
||
| 27 | * |
||
| 28 | * @param string $markdown |
||
| 29 | * @param int $indentWidth |
||
| 30 | */ |
||
| 31 | public function __construct($markdown = \null, $indentWidth = 4) |
||
| 32 | { |
||
| 33 | $this->scan($markdown, $indentWidth); |
||
| 34 | } |
||
| 35 | |||
| 36 | protected function scan($markdown, $indentWidth = 4) |
||
| 37 | { |
||
| 38 | if ('' === \trim($markdown)) { |
||
| 39 | return; |
||
| 40 | } |
||
| 41 | |||
| 42 | $this->indentLen = $indentWidth == 2 ? 2 : 4; |
||
| 43 | $this->indentStr = $indentWidth == 2 ? ' ' : ' '; |
||
| 44 | |||
| 45 | // Normalize whitespaces |
||
| 46 | $markdown = \str_replace("\t", $this->indentStr, $markdown); |
||
| 47 | $markdown = \str_replace(["\r\n", "\r"], "\n", $markdown); |
||
| 48 | |||
| 49 | $this->lines = \array_merge([''], \explode("\n", $markdown), ['']); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function __toString() |
||
| 53 | { |
||
| 54 | return $this->parse(); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Parse markdown. |
||
| 59 | * |
||
| 60 | * @param string $markdown |
||
| 61 | * @param int $indentWidth |
||
| 62 | * |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | public function parse($markdown = \null, $indentWidth = 4) |
||
| 66 | { |
||
| 67 | if (\null !== $markdown) { |
||
| 68 | $this->reset(\true); |
||
| 69 | |||
| 70 | $this->scan($markdown, $indentWidth); |
||
| 71 | } |
||
| 72 | |||
| 73 | if (empty($this->lines)) { |
||
| 74 | return ''; |
||
| 75 | } |
||
| 76 | |||
| 77 | $this->parseBlockElements(); |
||
| 78 | |||
| 79 | return (new SpanElementParser)->parse($this->markup); |
||
| 80 | } |
||
| 81 | |||
| 82 | protected function parseBlockElements() |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | protected function isBlock() |
||
| 104 | { |
||
| 105 | return $this->atx() || $this->setext() || $this->code() || $this->rule() || $this->listt(); |
||
| 106 | } |
||
| 107 | |||
| 108 | protected function init() |
||
| 109 | { |
||
| 110 | list($this->prevLine, $this->trimmedPrevLine) = [$this->line, $this->trimmedLine]; |
||
| 111 | |||
| 112 | $this->line = $this->lines[$this->pointer]; |
||
| 113 | $this->trimmedLine = \trim($this->line); |
||
| 114 | |||
| 115 | $this->indent = \strlen($this->line) - \strlen(\ltrim($this->line)); |
||
| 116 | $this->nextLine = isset($this->lines[$this->pointer + 1]) |
||
| 117 | ? $this->lines[$this->pointer + 1] |
||
| 118 | : ''; |
||
| 119 | $this->trimmedNextLine = \trim($this->nextLine); |
||
| 120 | $this->nextIndent = \strlen($this->nextLine) - \strlen(\ltrim($this->nextLine)); |
||
| 121 | } |
||
| 122 | |||
| 123 | protected function reset($all = \false) |
||
| 124 | { |
||
| 125 | $except = $all ? [] : \array_flip(['lines', 'pointer', 'markup', 'indentStr', 'indentLen']); |
||
| 126 | |||
| 127 | // Reset all current values. |
||
| 128 | foreach (\get_class_vars(__CLASS__) as $prop => $value) { |
||
| 129 | isset($except[$prop]) || $this->{$prop} = $value; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | protected function flush() |
||
| 134 | { |
||
| 135 | if ('' !== $this->trimmedLine) { |
||
| 136 | return \false; |
||
| 137 | } |
||
| 138 | |||
| 139 | while (!empty($this->stackList)) { |
||
| 140 | $this->markup .= \array_pop($this->stackList); |
||
| 141 | } |
||
| 142 | |||
| 143 | while (!empty($this->stackBlock)) { |
||
| 144 | $this->markup .= \array_pop($this->stackBlock); |
||
| 145 | } |
||
| 146 | |||
| 147 | while (!empty($this->stackTable)) { |
||
| 148 | $this->markup .= \array_pop($this->stackTable); |
||
| 149 | } |
||
| 150 | |||
| 151 | $this->markup .= "\n"; |
||
| 152 | |||
| 153 | $this->reset(\false); |
||
| 154 | |||
| 155 | return \true; |
||
| 156 | } |
||
| 157 | |||
| 158 | protected function raw() |
||
| 159 | { |
||
| 160 | if ($this->inHtml || \preg_match(static::RE_RAW, $this->trimmedLine)) { |
||
| 161 | $this->markup .= "\n$this->line"; |
||
| 162 | if (!$this->inHtml && empty($this->lines[$this->pointer - 1])) { |
||
| 163 | $this->inHtml = \true; |
||
| 164 | } |
||
| 165 | |||
| 166 | return \true; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | protected function quote() |
||
| 171 | { |
||
| 172 | if (\preg_match(static::RE_MD_QUOTE, $this->line, $quoteMatch)) { |
||
| 173 | $this->line = \substr($this->line, \strlen($quoteMatch[0])); |
||
| 174 | $this->trimmedLine = \trim($this->line); |
||
| 175 | |||
| 176 | if (!$this->inQuote || $this->quoteLevel < \strlen($quoteMatch[1])) { |
||
| 177 | $this->markup .= "\n<blockquote>"; |
||
| 178 | |||
| 179 | $this->stackBlock[] = "\n</blockquote>"; |
||
| 180 | |||
| 181 | $this->quoteLevel++; |
||
| 182 | } |
||
| 183 | |||
| 184 | return $this->inQuote = \true; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | protected function atx() |
||
| 189 | { |
||
| 190 | if (isset($this->trimmedLine[0]) && $this->trimmedLine[0] === '#') { |
||
| 191 | $level = \strlen($this->trimmedLine) - \strlen(\ltrim($this->trimmedLine, '#')); |
||
| 192 | |||
| 193 | if ($level < 7) { |
||
| 194 | $this->markup .= "\n<h{$level}>" . \ltrim(\ltrim($this->trimmedLine, '# ')) . "</h{$level}>"; |
||
| 195 | |||
| 196 | return \true; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | protected function setext() |
||
| 202 | { |
||
| 203 | if (\preg_match(static::RE_MD_SETEXT, $this->nextLine)) { |
||
| 204 | $level = \trim($this->nextLine, '- ') === '' ? 2 : 1; |
||
| 205 | |||
| 206 | $this->markup .= "\n<h{$level}>{$this->trimmedLine}</h{$level}>"; |
||
| 207 | |||
| 208 | $this->pointer++; |
||
| 209 | |||
| 210 | return \true; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | protected function code() |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | protected function rule() |
||
| 241 | { |
||
| 242 | if ($this->trimmedPrevLine === '' |
||
| 243 | && \preg_match(static::RE_MD_RULE, $this->trimmedLine) |
||
| 244 | ) { |
||
| 245 | $this->markup .= "\n<hr />"; |
||
| 246 | |||
| 247 | return \true; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | protected function listt() |
||
| 252 | { |
||
| 253 | $isUl = \in_array(\substr($this->trimmedLine, 0, 2), ['- ', '* ', '+ ']); |
||
| 254 | |||
| 255 | if ($isUl || \preg_match(static::RE_MD_OL, $this->trimmedLine)) { |
||
| 256 | $wrapper = $isUl ? 'ul' : 'ol'; |
||
| 257 | |||
| 258 | if (!$this->inList) { |
||
| 259 | $this->stackList[] = "</$wrapper>"; |
||
| 260 | $this->markup .= "\n<$wrapper>\n"; |
||
| 261 | $this->inList = \true; |
||
| 262 | |||
| 263 | $this->listLevel++; |
||
| 264 | } |
||
| 265 | |||
| 266 | $this->markup .= '<li>' . \ltrim($this->trimmedLine, '+-*0123456789. '); |
||
| 267 | |||
| 268 | $this->listInternal(); |
||
| 269 | |||
| 270 | return \true; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | protected function table() |
||
| 275 | { |
||
| 276 | static $headerCount = 0; |
||
| 277 | |||
| 278 | if (!$this->inTable) { |
||
| 279 | $headerCount = \substr_count(\trim($this->trimmedLine, '|'), '|'); |
||
| 280 | |||
| 281 | return $this->tableInternal($headerCount); |
||
| 282 | } |
||
| 283 | |||
| 284 | $this->markup .= "<tr>\n"; |
||
| 285 | |||
| 286 | foreach (\explode('|', \trim($this->trimmedLine, '|')) as $i => $col) { |
||
| 287 | if ($i > $headerCount) { |
||
| 288 | break; |
||
| 289 | } |
||
| 290 | |||
| 291 | $col = \trim($col); |
||
| 292 | $this->markup .= "<td>{$col}</td>\n"; |
||
| 293 | } |
||
| 294 | |||
| 295 | $this->markup .= "</tr>\n"; |
||
| 296 | |||
| 297 | if (empty($this->trimmedNextLine) |
||
| 298 | || !\substr_count(\trim($this->trimmedNextLine, '|'), '|') |
||
| 299 | ) { |
||
| 300 | $headerCount = 0; |
||
| 301 | $this->inTable = \false; |
||
| 302 | $this->stackTable[] = "</tbody>\n</table>"; |
||
| 303 | } |
||
| 304 | |||
| 305 | return \true; |
||
| 306 | } |
||
| 307 | |||
| 308 | protected function paragraph() |
||
| 318 | } |
||
| 319 | } |
||
| 320 | } |
||
| 321 |