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 |
||
| 23 | class SlackMessageTraitTest extends TestCase |
||
| 24 | { |
||
| 25 | use SlackMessageTrait; |
||
| 26 | |||
| 27 | public function testInlineMultilines() |
||
| 28 | { |
||
| 29 | $lines = []; |
||
| 30 | for ($i = rand(parent::ARRAY_MIN_ITEMS, parent::ARRAY_MAX_ITEMS); $i <= parent::ARRAY_MAX_ITEMS; ++$i) { |
||
| 31 | $randomString = $this->randomString(); |
||
| 32 | $lines[] = $randomString; |
||
| 33 | } |
||
| 34 | |||
| 35 | $expect = implode("\n", $lines); |
||
| 36 | $actual = $this->inlineMultilines($lines); |
||
| 37 | |||
| 38 | $this->assertEquals($expect, $actual); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function testFormatBold() |
||
| 42 | { |
||
| 43 | $randomString = $this->randomString(); |
||
| 44 | $expect = sprintf('*%s*', $randomString); |
||
| 45 | $actual = $this->formatBold($randomString); |
||
| 46 | |||
| 47 | $this->assertEquals($expect, $actual); |
||
| 48 | } |
||
| 49 | |||
| 50 | public function testFormatItalic() |
||
| 51 | { |
||
| 52 | $randomString = $this->randomString(); |
||
| 53 | $expect = sprintf('_%s_', $randomString); |
||
| 54 | $actual = $this->formatItalic($randomString); |
||
| 55 | |||
| 56 | $this->assertEquals($expect, $actual); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function testFormatStrikeThought() |
||
| 60 | { |
||
| 61 | $randomString = $this->randomString(); |
||
| 62 | $expect = sprintf('~%s~', $randomString); |
||
| 63 | $actual = $this->formatStrikeThought($randomString); |
||
| 64 | |||
| 65 | $this->assertEquals($expect, $actual); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function testFormatListMarker() |
||
| 69 | { |
||
| 70 | $lines = []; |
||
| 71 | for ($i = rand(parent::ARRAY_MIN_ITEMS, parent::ARRAY_MAX_ITEMS); $i <= parent::ARRAY_MAX_ITEMS; ++$i) { |
||
| 72 | $randomString = $this->randomString(); |
||
| 73 | $lines[] = $randomString; |
||
| 74 | } |
||
| 75 | |||
| 76 | $expect = "\n"; |
||
| 77 | foreach ($lines as $line) { |
||
| 78 | $expect .= "• " . $line . "\n"; |
||
| 79 | } |
||
| 80 | $actual = $this->formatListMarker($lines); |
||
| 81 | |||
| 82 | $this->assertEquals($expect, $actual); |
||
| 83 | } |
||
| 84 | |||
| 85 | public function testFormatListNumeric() |
||
| 86 | { |
||
| 87 | $lines = []; |
||
| 88 | for ($i = rand(parent::ARRAY_MIN_ITEMS, parent::ARRAY_MAX_ITEMS); $i <= parent::ARRAY_MAX_ITEMS; ++$i) { |
||
| 89 | $randomString = $this->randomString(); |
||
| 90 | $lines[] = $randomString; |
||
| 91 | } |
||
| 92 | |||
| 93 | $expect = "\n"; |
||
| 94 | foreach ($lines as $key => $line) { |
||
| 95 | $expect .= sprintf("%d. %s\n", $key + 1, $line); |
||
| 96 | } |
||
| 97 | $actual = $this->formatListNumeric($lines); |
||
| 98 | |||
| 99 | $this->assertEquals($expect, $actual); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function testFormatCode() |
||
| 103 | { |
||
| 104 | $lines = []; |
||
| 105 | for ($i = rand(parent::ARRAY_MIN_ITEMS, parent::ARRAY_MAX_ITEMS); $i <= parent::ARRAY_MAX_ITEMS; ++$i) { |
||
| 106 | $randomString = $this->randomString(); |
||
| 107 | $lines[] = $randomString; |
||
| 108 | } |
||
| 109 | |||
| 110 | $expect = sprintf( |
||
| 111 | "%s%s%s", |
||
| 112 | "\n```", |
||
| 113 | $this->inlineMultilines($lines), |
||
| 114 | "```\n" |
||
| 115 | ); |
||
| 116 | $actual = $this->formatCode($lines); |
||
| 117 | |||
| 118 | $this->assertEquals($expect, $actual); |
||
| 119 | } |
||
| 120 | |||
| 121 | public function testNewLine() |
||
| 122 | { |
||
| 123 | $expect = "\n"; |
||
| 124 | $actual = $this->newLine(); |
||
| 125 | |||
| 126 | $this->assertEquals($expect, $actual); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function testFormatLink() |
||
| 130 | { |
||
| 131 | $url = $this->randomString(); |
||
| 132 | $title = $this->randomString(); |
||
| 133 | |||
| 134 | $expect = sprintf("<%s|%s>", $url, $title); |
||
| 135 | $actual = $this->formatLink($title, $url); |
||
| 136 | |||
| 137 | $this->assertEquals($expect, $actual); |
||
| 138 | } |
||
| 139 | |||
| 140 | public function testEscapeCharacters() |
||
| 141 | { |
||
| 142 | $expect = $testString = $this->randomString() . '<&>'; |
||
| 143 | $expect = str_replace('&', '&', $expect); |
||
| 144 | $expect = str_replace('<', '<', $expect); |
||
| 145 | $expect = str_replace('>', '>', $expect); |
||
| 146 | $actual = $this->escapeCharacters($testString); |
||
| 147 | |||
| 148 | $this->assertEquals($expect, $actual); |
||
| 149 | } |
||
| 150 | } |
||
| 151 |