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 HeadTest extends AbstractFileTestCase |
|
|
|
|||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var Head |
||
| 32 | */ |
||
| 33 | protected $head; |
||
| 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->head = new Head(); |
||
| 44 | $this->head->setProcessFactory($this->processFactory); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function testInstanceOf() |
||
| 48 | { |
||
| 49 | static::assertInstanceOf(FileModifierInterface::class, $this->head); |
||
| 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->head->canModify($localFile)); |
||
| 58 | static::assertFalse( |
||
| 59 | $this->head->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->head->canModify($randomThing)); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function testBasicReadingTheFirstNLines() |
||
| 69 | { |
||
| 70 | $file = $this->createFile('first_five_lines'); |
||
| 71 | |||
| 72 | $newFile = $this->head->head($file, 5); |
||
| 73 | |||
| 74 | static::assertEquals( |
||
| 75 | [ |
||
| 76 | "Line 1", |
||
| 77 | "Line 2", |
||
| 78 | "Line 3", |
||
| 79 | "Line 4", |
||
| 80 | "Line 5", |
||
| 81 | ], |
||
| 82 | $newFile->getContents() |
||
| 83 | ); |
||
| 84 | |||
| 85 | $newFile = $this->head->head($file, 2); |
||
| 86 | |||
| 87 | static::assertEquals( |
||
| 88 | [ |
||
| 89 | "Line 1", |
||
| 90 | "Line 2", |
||
| 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 testOutputLinesUpToN() |
||
| 122 | { |
||
| 123 | $file = $this->createFile('from.second.line.onwards'); |
||
| 124 | |||
| 125 | $newFile = $this->head->head($file, '-2'); |
||
| 126 | |||
| 127 | static::assertEquals( |
||
| 128 | [ |
||
| 129 | "Line 1", |
||
| 130 | "Line 2", |
||
| 131 | "Line 3", |
||
| 132 | "Line 4", |
||
| 133 | "Line 5", |
||
| 134 | "Line 6", |
||
| 135 | "Line 7", |
||
| 136 | "Line 8", |
||
| 137 | ], |
||
| 138 | $newFile->getContents() |
||
| 139 | ); |
||
| 140 | } |
||
| 141 | |||
| 142 | public function testAddingAPostfixToTheEndOfTheFile() |
||
| 143 | { |
||
| 144 | $file = $this->createFile('postfix_test.test'); |
||
| 145 | |||
| 146 | $newFile = $this->head->head($file, 4, ['postfix' => 'pfixtest']); |
||
| 147 | |||
| 148 | static::assertNotNull($newFile); |
||
| 149 | static::assertEquals('postfix_test-pfixtest.test', $newFile->getFilename()); |
||
| 150 | } |
||
| 151 | |||
| 152 | public function testCallingWithBlankPostfixWillReplaceInLine() |
||
| 153 | { |
||
| 154 | $file = $this->createFile('inline_tail.test'); |
||
| 155 | |||
| 156 | $newFile = $this->head->head($file, 2, ['postfix' => '']); |
||
| 157 | |||
| 158 | static::assertNotNull($newFile); |
||
| 159 | static::assertEquals($file->getFilename(), $newFile->getFilename()); |
||
| 160 | } |
||
| 161 | |||
| 162 | public function testSettingKeepOldFileToFalseWillDeleteTheOldFile() |
||
| 163 | { |
||
| 164 | $file = $this->createFile('inline_replace.test'); |
||
| 165 | |||
| 166 | $newFile = $this->head->head($file, 5, ['keepOldFile' => false]); |
||
| 167 | |||
| 168 | static::assertTrue($newFile->exists()); |
||
| 169 | static::assertFalse($file->exists()); |
||
| 170 | } |
||
| 171 | |||
| 172 | public function testCallingModifyDoesTail() |
||
| 173 | { |
||
| 174 | $file = $this->createFile('simple_tail.test'); |
||
| 175 | |||
| 176 | $newFile = $this->head->modify($file, ['lines' => 4]); |
||
| 177 | |||
| 178 | static::assertEquals( |
||
| 179 | [ |
||
| 180 | "Line 1", |
||
| 181 | "Line 2", |
||
| 182 | "Line 3", |
||
| 183 | "Line 4", |
||
| 184 | ], |
||
| 185 | $newFile->getContents() |
||
| 186 | ); |
||
| 187 | } |
||
| 188 | |||
| 189 | public function testCallingModifyWillPassThroughOptions() |
||
| 190 | { |
||
| 191 | $file = $this->createFile('option_pass_through.test'); |
||
| 192 | |||
| 193 | $newFile = $this->head->modify( |
||
| 194 | $file, |
||
| 195 | [ |
||
| 196 | 'lines' => 2, |
||
| 197 | 'postfix' => 'pass', |
||
| 198 | 'keepOldFile' => false, |
||
| 199 | ] |
||
| 200 | ); |
||
| 201 | |||
| 202 | static::assertTrue($newFile->exists()); |
||
| 203 | static::assertFalse($file->exists()); |
||
| 204 | static::assertNotNull($newFile); |
||
| 205 | static::assertEquals('option_pass_through-pass.test', $newFile->getFilename()); |
||
| 206 | } |
||
| 207 | |||
| 208 | public function testCallingModifyWithoutLinesWillThrowAnException() |
||
| 209 | { |
||
| 210 | $file = $this->createFile('option_pass_through.test'); |
||
| 211 | |||
| 212 | $this->expectException(InvalidArgumentException::class); |
||
| 213 | |||
| 214 | $this->head->modify($file); |
||
| 215 | } |
||
| 216 | |||
| 217 | public function testCallingModifyWithANonLocalFileWillThrowAnException() |
||
| 227 | |||
| 228 | public function testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding() |
||
| 242 | } |
||
| 243 |
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.