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 |
||
| 28 | View Code Duplication | class TailTest extends AbstractFileTestCase |
|
|
|
|||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var Tail |
||
| 32 | */ |
||
| 33 | protected $tail; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var ProcessFactory|MockInterface |
||
| 37 | */ |
||
| 38 | protected $processFactory; |
||
| 39 | |||
| 40 | public function setUp() |
||
| 41 | { |
||
| 42 | $this->processFactory = m::mock(ProcessFactory::class)->makePartial(); |
||
| 43 | $this->tail = new Tail(); |
||
| 44 | $this->tail->setProcessFactory($this->processFactory); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function testInstanceOf() |
||
| 48 | { |
||
| 49 | static::assertInstanceOf(FileModifierInterface::class, $this->tail); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function testCanModifyAcceptsLocalFile() |
||
| 53 | { |
||
| 54 | $localFile = m::mock(LocalFile::class); |
||
| 55 | $localFile->shouldReceive('exists')->andReturn(true, false); |
||
| 56 | |||
| 57 | static::assertTrue($this->tail->canModify($localFile)); |
||
| 58 | static::assertFalse( |
||
| 59 | $this->tail->canModify($localFile), |
||
| 60 | "CanExtend should return false if the file does not exist" |
||
| 61 | ); |
||
| 62 | |||
| 63 | $randomThing = m::mock(FileNodeInterface::class); |
||
| 64 | |||
| 65 | static::assertFalse($this->tail->canModify($randomThing)); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function testBasicReadingTheLastNLines() |
||
| 69 | { |
||
| 70 | $file = $this->createFile('last_five_lines'); |
||
| 71 | |||
| 72 | $newFile = $this->tail->tail($file, 5); |
||
| 73 | |||
| 74 | static::assertEquals( |
||
| 75 | [ |
||
| 76 | "Line 6", |
||
| 77 | "Line 7", |
||
| 78 | "Line 8", |
||
| 79 | "Line 9", |
||
| 80 | "Line 10", |
||
| 81 | ], |
||
| 82 | $newFile->getContents() |
||
| 83 | ); |
||
| 84 | |||
| 85 | $newFile = $this->tail->tail($file, 2); |
||
| 86 | |||
| 87 | static::assertEquals( |
||
| 88 | [ |
||
| 89 | "Line 9", |
||
| 90 | "Line 10", |
||
| 91 | ], |
||
| 92 | $newFile->getContents() |
||
| 93 | ); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $path |
||
| 98 | * |
||
| 99 | * @return LocalFile |
||
| 100 | */ |
||
| 101 | private function createFile($path) |
||
| 102 | { |
||
| 103 | $file = new LocalFile(static::$dir . $path); |
||
| 104 | $file->put( |
||
| 105 | "Line 1 |
||
| 106 | Line 2 |
||
| 107 | Line 3 |
||
| 108 | Line 4 |
||
| 109 | Line 5 |
||
| 110 | Line 6 |
||
| 111 | Line 7 |
||
| 112 | Line 8 |
||
| 113 | Line 9 |
||
| 114 | Line 10 |
||
| 115 | " |
||
| 116 | ); |
||
| 117 | |||
| 118 | return $file; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function testOutputLinesStartingFromN() |
||
| 122 | { |
||
| 123 | $file = $this->createFile('from.second.line.onwards'); |
||
| 124 | |||
| 125 | $newFile = $this->tail->tail($file, '+2'); |
||
| 126 | |||
| 127 | static::assertEquals( |
||
| 128 | [ |
||
| 129 | "Line 2", |
||
| 130 | "Line 3", |
||
| 131 | "Line 4", |
||
| 132 | "Line 5", |
||
| 133 | "Line 6", |
||
| 134 | "Line 7", |
||
| 135 | "Line 8", |
||
| 136 | "Line 9", |
||
| 137 | "Line 10", |
||
| 138 | ], |
||
| 139 | $newFile->getContents() |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | |||
| 143 | public function testAddingAPostfixToTheEndOfTheFile() |
||
| 144 | { |
||
| 145 | $file = $this->createFile('postfix_test.test'); |
||
| 146 | |||
| 147 | $newFile = $this->tail->tail($file, 4, ['postfix' => 'pfixtest']); |
||
| 148 | |||
| 149 | static::assertNotNull($newFile); |
||
| 150 | static::assertEquals('postfix_test-pfixtest.test', $newFile->getFilename()); |
||
| 151 | } |
||
| 152 | |||
| 153 | public function testCallingWithBlankPostfixWillReplaceInLine() |
||
| 154 | { |
||
| 155 | $file = $this->createFile('inline_tail.test'); |
||
| 156 | |||
| 157 | $newFile = $this->tail->tail($file, 2, ['postfix' => '']); |
||
| 158 | |||
| 159 | static::assertNotNull($newFile); |
||
| 160 | static::assertEquals($file->getFilename(), $newFile->getFilename()); |
||
| 161 | } |
||
| 162 | |||
| 163 | public function testSettingKeepOldFileToFalseWillDeleteTheOldFile() |
||
| 164 | { |
||
| 165 | $file = $this->createFile('inline_replace.test'); |
||
| 166 | |||
| 167 | $newFile = $this->tail->tail($file, 5, ['keepOldFile' => false]); |
||
| 168 | |||
| 169 | static::assertTrue($newFile->exists()); |
||
| 170 | static::assertFalse($file->exists()); |
||
| 171 | } |
||
| 172 | |||
| 173 | public function testCallingModifyDoesTail() |
||
| 174 | { |
||
| 175 | $file = $this->createFile('simple_tail.test'); |
||
| 176 | |||
| 177 | $newFile = $this->tail->modify($file, ['lines' => 4]); |
||
| 178 | |||
| 179 | static::assertEquals( |
||
| 180 | [ |
||
| 181 | "Line 7", |
||
| 182 | "Line 8", |
||
| 183 | "Line 9", |
||
| 184 | "Line 10", |
||
| 185 | ], |
||
| 186 | $newFile->getContents() |
||
| 187 | ); |
||
| 188 | } |
||
| 189 | |||
| 190 | public function testCallingModifyWillPassThroughOptions() |
||
| 191 | { |
||
| 192 | $file = $this->createFile('option_pass_through.test'); |
||
| 193 | |||
| 194 | $newFile = $this->tail->modify( |
||
| 195 | $file, |
||
| 196 | [ |
||
| 197 | 'lines' => 2, |
||
| 198 | 'postfix' => 'pass', |
||
| 199 | 'keepOldFile' => false, |
||
| 200 | ] |
||
| 201 | ); |
||
| 202 | |||
| 203 | static::assertTrue($newFile->exists()); |
||
| 204 | static::assertFalse($file->exists()); |
||
| 205 | static::assertNotNull($newFile); |
||
| 206 | static::assertEquals('option_pass_through-pass.test', $newFile->getFilename()); |
||
| 207 | } |
||
| 208 | |||
| 209 | public function testCallingModifyWithoutLinesWillThrowAnException() |
||
| 210 | { |
||
| 211 | $file = $this->createFile('option_pass_through.test'); |
||
| 212 | |||
| 213 | $this->expectException(InvalidArgumentException::class); |
||
| 214 | |||
| 215 | $this->tail->modify($file); |
||
| 216 | } |
||
| 217 | |||
| 218 | public function testCallingModifyWithANonLocalFileWillThrowAnException() |
||
| 228 | |||
| 229 | public function testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding() |
||
| 243 | } |
||
| 244 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.