Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 17 | class HtmlConverter |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var Environment |
||
| 21 | */ |
||
| 22 | protected $environment; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $whiteTags = array(); |
||
| 28 | |||
| 29 | 75 | /** |
|
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | 75 | protected $wildCard = ''; |
|
| 33 | 75 | ||
| 34 | 75 | /** |
|
| 35 | 75 | * Constructor |
|
| 36 | 75 | * |
|
| 37 | 75 | * @param array $options Configuration options |
|
| 38 | 75 | */ |
|
| 39 | public function __construct(array $options = array()) |
||
| 40 | 75 | { |
|
| 41 | $defaults = array( |
||
| 42 | 75 | 'header_style' => 'setext', // Set to 'atx' to output H1 and H2 headers as # Header1 and ## Header2 |
|
| 43 | 75 | 'suppress_errors' => true, // Set to false to show warnings when loading malformed HTML |
|
| 44 | 'strip_tags' => false, // Set to true to strip tags that don't have markdown equivalents. N.B. Strips tags, not their content. Useful to clean MS Word HTML output. |
||
| 45 | 'bold_style' => '**', // Set to '__' if you prefer the underlined style |
||
| 46 | 'italic_style' => '_', // Set to '*' if you prefer the asterisk style |
||
| 47 | 'remove_nodes' => '', // space-separated list of dom nodes that should be removed. example: 'meta style script' |
||
| 48 | 'white_tags' => array(), // Array with allowed html tags |
||
| 49 | 'white_tag_wildcard' => '|', // Use a non common character |
||
| 50 | ); |
||
| 51 | |||
| 52 | $this->environment = Environment::createDefaultEnvironment($defaults); |
||
| 53 | |||
| 54 | $this->environment->getConfig()->merge($options); |
||
| 55 | } |
||
| 56 | 72 | ||
| 57 | /** |
||
| 58 | 72 | * @return Environment |
|
| 59 | */ |
||
| 60 | public function getEnvironment() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return Configuration |
||
| 67 | */ |
||
| 68 | public function getConfig() |
||
| 72 | 3 | ||
| 73 | /** |
||
| 74 | * Convert |
||
| 75 | * |
||
| 76 | * @see HtmlConverter::convert |
||
| 77 | * |
||
| 78 | * @param string $html |
||
| 79 | * |
||
| 80 | * @return string The Markdown version of the html |
||
| 81 | */ |
||
| 82 | public function __invoke($html) |
||
| 86 | 75 | ||
| 87 | 3 | /** |
|
| 88 | 3 | * Convert |
|
| 89 | * |
||
| 90 | 75 | * Loads HTML and passes to getMarkdown() |
|
| 91 | * |
||
| 92 | * @param $html |
||
| 93 | 75 | * |
|
| 94 | 3 | * @return string The Markdown version of the html |
|
| 95 | 3 | */ |
|
| 96 | public function convert($html) |
||
| 125 | |||
| 126 | 72 | /** |
|
| 127 | 72 | * Set the values for use after |
|
| 128 | 72 | */ |
|
| 129 | protected function setWhiteTagVariables() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Add each "whiteTag" into <code> tags and add the "wildCard" before and after the "<code>" tag |
||
| 137 | * for avoid convert into markdown and indentify them later |
||
| 138 | * |
||
| 139 | * @param string $html |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | 72 | View Code Duplication | protected function escapeWhiteTags($html) |
| 161 | |||
| 162 | /** |
||
| 163 | 72 | * @param string $tag |
|
| 164 | 72 | * |
|
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | protected function getOpenTag($tag) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param string $tag |
||
| 174 | * |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | 72 | protected function getCloseTag($tag) |
|
| 181 | |||
| 182 | 72 | /** |
|
| 183 | 72 | * @param string $html |
|
| 184 | 3 | * |
|
| 185 | * @return \DOMDocument |
||
| 186 | */ |
||
| 187 | 72 | private function createDOMDocument($html) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Convert Children |
||
| 209 | * |
||
| 210 | * Recursive function to drill into the DOM and convert each node into Markdown from the inside out. |
||
| 211 | * |
||
| 212 | * Finds children of each node and convert those to #text nodes containing their Markdown equivalent, |
||
| 213 | * starting with the innermost element and working up to the outermost element. |
||
| 214 | * |
||
| 215 | * @param ElementInterface $element |
||
| 216 | */ |
||
| 217 | private function convertChildren(ElementInterface $element) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Convert to Markdown |
||
| 242 | * |
||
| 243 | * Converts an individual node into a #text node containing a string of its Markdown equivalent. |
||
| 244 | * |
||
| 245 | * Example: An <h3> node with text content of 'Title' becomes a text node with content of '### Title' |
||
| 246 | * |
||
| 247 | * @param ElementInterface $element |
||
| 248 | * |
||
| 249 | * @return string The converted HTML as Markdown |
||
| 250 | */ |
||
| 251 | protected function convertToMarkdown(ElementInterface $element) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param string $markdown |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | protected function sanitize($markdown) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Remove the previously added <code> for the "whiteTags" marked by the "wildCard" |
||
| 284 | * to return the "html" as the user typed |
||
| 285 | * |
||
| 286 | * @param string $markdown |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | View Code Duplication | protected function removeEscapedWhiteTags($markdown) |
|
| 308 | } |
||
| 309 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..