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 |
||
| 8 | class PointParser extends MarkdownParser |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var RouterInterface |
||
| 12 | */ |
||
| 13 | protected $router; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * {@inheritdoc} |
||
| 17 | */ |
||
| 18 | protected $features = array( |
||
| 19 | 'header' => true, |
||
| 20 | 'list' => true, |
||
| 21 | 'horizontal_rule' => true, |
||
| 22 | 'table' => false, |
||
| 23 | 'foot_note' => true, |
||
| 24 | 'fenced_code_block' => true, |
||
| 25 | 'abbreviation' => true, |
||
| 26 | 'definition_list' => true, |
||
| 27 | 'inline_link' => true, // [link text](url "optional title") |
||
|
1 ignored issue
–
show
|
|||
| 28 | 'reference_link' => true, // [link text] [id] |
||
|
1 ignored issue
–
show
|
|||
| 29 | 'shortcut_link' => true, // [link text] |
||
| 30 | 'images' => true, |
||
| 31 | 'block_quote' => true, |
||
| 32 | 'code_block' => true, |
||
| 33 | 'html_block' => false, |
||
| 34 | 'auto_link' => true, |
||
| 35 | 'auto_mailto' => true, |
||
| 36 | 'entities' => true, |
||
| 37 | 'no_html' => true, |
||
| 38 | 'point_usernames' => true, // @user |
||
| 39 | 'point_posts' => true, // #post |
||
| 40 | 'point_tags' => false, // @todo implement |
||
| 41 | 'point_inline_images' => true, // https://i.skobk.in/i/facepalm.jpg |
||
| 42 | ); |
||
| 43 | |||
| 44 | 12 | public function __construct(array $features, RouterInterface $router) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Point.im breaks Markdown rule about quotation on next line |
||
| 53 | * |
||
| 54 | * @param $text |
||
| 55 | * |
||
| 56 | * @return mixed |
||
| 57 | */ |
||
| 58 | 1 | protected function doBlockQuotes($text) { |
|
| 59 | 1 | $text = preg_replace_callback('/ |
|
| 60 | ( # Wrap whole match in $1 |
||
| 61 | (?> |
||
| 62 | ^[ ]*>[ ]? # ">" at the start of a line |
||
| 63 | .+\n # rest of the first line |
||
| 64 | \n* # blanks |
||
| 65 | )+ |
||
| 66 | ) |
||
| 67 | 1 | /xm', |
|
| 68 | 1 | array($this, '_doBlockQuotes_callback'), $text |
|
| 69 | ); |
||
| 70 | |||
| 71 | 1 | return $text; |
|
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Point.im breaks Markdown double line wrap rule |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | 1 | protected function formParagraphs($text, $wrap_in_p = true) { |
|
| 79 | // Strip leading and trailing lines: |
||
| 80 | 1 | $text = preg_replace('/\A\n+|\n+\z/', '', $text); |
|
| 81 | |||
| 82 | 1 | $grafs = preg_split('/\n+/', $text, -1, PREG_SPLIT_NO_EMPTY); |
|
| 83 | |||
| 84 | // Wrap <p> tags and unhashify HTML blocks |
||
| 85 | 1 | foreach ($grafs as $key => $value) { |
|
| 86 | 1 | if (!preg_match('/^B\x1A[0-9]+B$/', $value)) { |
|
| 87 | // Is a paragraph. |
||
| 88 | 1 | $value = $this->runSpanGamut($value); |
|
| 89 | 1 | if ($wrap_in_p) { |
|
| 90 | 1 | $value = preg_replace('/^([ ]*)/', "<p>", $value); |
|
| 91 | 1 | $value .= "</p>"; |
|
| 92 | } |
||
| 93 | 1 | $grafs[$key] = $this->unhash($value); |
|
| 94 | } else { |
||
| 95 | // Is a block. |
||
| 96 | // Modify elements of @grafs in-place... |
||
| 97 | $graf = $value; |
||
| 98 | $block = $this->html_hashes[$graf]; |
||
| 99 | $graf = $block; |
||
| 100 | 1 | $grafs[$key] = $graf; |
|
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | 1 | return implode("\n\n", $grafs); |
|
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $text |
||
| 109 | * |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | 1 | public function doAutoLinks($text) |
|
| 113 | { |
||
| 114 | 1 | if ($this->features['point_inline_images']) { |
|
| 115 | 1 | return preg_replace_callback('{((https?):\/\/[^\'">\s]+\.(jpg|jpeg|png|gif))}i', array(&$this, 'doAutoLinksInlineImageCallback'), $text); |
|
| 116 | } |
||
| 117 | |||
| 118 | return parent::doAutoLinks($text); |
||
| 119 | } |
||
| 120 | |||
| 121 | 1 | public function doAnchors($text) |
|
| 122 | { |
||
| 123 | 1 | $text = parent::doAnchors($text); |
|
| 124 | |||
| 125 | # |
||
| 126 | # Turn Markdown link shortcuts into XHTML <a> tags. |
||
| 127 | # |
||
| 128 | 1 | if ($this->in_anchor) { |
|
| 129 | return $text; |
||
| 130 | } |
||
| 131 | 1 | $this->in_anchor = true; |
|
| 132 | |||
| 133 | # |
||
| 134 | # Handling username links: @some-point-username |
||
| 135 | # |
||
| 136 | 1 | if ($this->features['point_usernames']) { |
|
| 137 | 1 | $text = preg_replace_callback('{ |
|
| 138 | ( # wrap whole match in $1 |
||
| 139 | @ |
||
| 140 | ( |
||
| 141 | [a-zA-Z0-9\-]+? # username = $2 |
||
| 142 | ) |
||
| 143 | ) |
||
| 144 | ([^a-zA-Z0-9\-]|$) # Tail of username |
||
| 145 | 1 | }xs', |
|
| 146 | 1 | array(&$this, 'doAnchorsPointUsernameCallback'), $text); |
|
| 147 | } |
||
| 148 | |||
| 149 | # |
||
| 150 | # Handling post links: #somepost |
||
| 151 | # |
||
| 152 | 1 | if ($this->features['point_posts']) { |
|
| 153 | 1 | $text = preg_replace_callback('{ |
|
| 154 | ( # wrap whole match in $1 |
||
| 155 | \# |
||
| 156 | ( |
||
| 157 | [a-zA-Z]+? # post id = $2 |
||
| 158 | ) |
||
| 159 | ) |
||
| 160 | ([^a-zA-Z]|$) # Tail of post |
||
| 161 | 1 | }xs', |
|
| 162 | 1 | array(&$this, 'doAnchorsPointPostCallback'), $text); |
|
| 163 | } |
||
| 164 | |||
| 165 | 1 | $this->in_anchor = false; |
|
| 166 | |||
| 167 | 1 | return $text; |
|
| 168 | } |
||
| 169 | |||
| 170 | protected function doAutoLinksInlineImageCallback($matches) |
||
| 178 | |||
| 179 | View Code Duplication | protected function doAnchorsPointUsernameCallback($matches) |
|
| 188 | |||
| 189 | View Code Duplication | protected function doAnchorsPointPostCallback($matches) |
|
| 202 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.