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 | class ReplaceTextTest extends AbstractFileTestCase |
||
29 | { |
||
30 | /** |
||
31 | * @var ReplaceText |
||
32 | */ |
||
33 | protected $replacer; |
||
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->replacer = new ReplaceText(); |
||
44 | $this->replacer->setProcessFactory($this->processFactory); |
||
45 | } |
||
46 | |||
47 | public function testInstanceOf() |
||
48 | { |
||
49 | static::assertInstanceOf(FileModifierInterface::class, $this->replacer); |
||
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->replacer->canModify($localFile)); |
||
58 | static::assertFalse( |
||
59 | $this->replacer->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->replacer->canModify($randomThing)); |
||
66 | } |
||
67 | |||
68 | public function testReplaceTextReplacesASingleEntry() |
||
69 | { |
||
70 | $file = new LocalFile(static::$dir . 'simple_replace.test'); |
||
71 | $file->put('some text that text should be replaced'); |
||
72 | |||
73 | $newFile = $this->replacer->replaceText($file, 'text', 'pants'); |
||
74 | |||
75 | static::assertNotNull($newFile); |
||
76 | static::assertEquals(['some pants that pants should be replaced'], $newFile->getContents()); |
||
77 | } |
||
78 | |||
79 | public function testReplaceTextReplacesMultipleEntries() |
||
80 | { |
||
81 | $file = new LocalFile(static::$dir . 'multiple_replace.test'); |
||
82 | $file->put('some text that text should be replaced'); |
||
83 | |||
84 | $newFile = $this->replacer->replaceText($file, ['text', 'some'], ['pants', 'many']); |
||
85 | |||
86 | static::assertNotNull($newFile); |
||
87 | static::assertEquals(['many pants that pants should be replaced'], $newFile->getContents()); |
||
88 | } |
||
89 | |||
90 | public function testReplaceTextReplacesMultipleEntriesWorksInCompound() |
||
91 | { |
||
92 | $file = new LocalFile(static::$dir . 'multiple_compound_replace.test'); |
||
93 | $file->put('some text that text should be replaced'); |
||
94 | |||
95 | $newFile = $this->replacer->replaceText($file, ['text', 'pants that'], ['pants', 'fish like']); |
||
96 | |||
97 | static::assertNotNull($newFile); |
||
98 | static::assertEquals(['some fish like pants should be replaced'], $newFile->getContents()); |
||
99 | } |
||
100 | |||
101 | View Code Duplication | public function testCallingReplaceTextWithArraysThatHaveMismatchedCountsThrowsAnException() |
|
110 | |||
111 | View Code Duplication | public function testCallingReplaceTextWithAnArrayAndStringThrowsAnException() |
|
119 | |||
120 | View Code Duplication | public function testAddingAPostfixToTheEndOfTheFile() |
|
121 | { |
||
122 | $file = new LocalFile(static::$dir . 'postfix_test.test'); |
||
123 | $file->put('some text that text should be replaced'); |
||
124 | |||
125 | $newFile = $this->replacer->replaceText($file, 'text', 'pants', ['postfix' => 'pfixtest']); |
||
126 | |||
127 | static::assertNotNull($newFile); |
||
128 | static::assertEquals('postfix_test-pfixtest.test', $newFile->getFilename()); |
||
129 | } |
||
130 | |||
131 | View Code Duplication | public function testCallingWithBlankPostfixWillReplaceInLine() |
|
132 | { |
||
133 | $file = new LocalFile(static::$dir . 'inline_replace.test'); |
||
134 | $file->put('some text that text should be replaced'); |
||
135 | |||
136 | $newFile = $this->replacer->replaceText($file, 'text', 'pants', ['postfix' => '']); |
||
137 | |||
138 | static::assertNotNull($newFile); |
||
139 | static::assertEquals($file->getFilename(), $newFile->getFilename()); |
||
140 | } |
||
141 | |||
142 | View Code Duplication | public function testSettingKeepOldFileToFalseWillDeleteTheOldFile() |
|
143 | { |
||
144 | $file = new LocalFile(static::$dir . 'inline_replace.test'); |
||
145 | $file->put('some text that text should be replaced'); |
||
146 | |||
147 | $newFile = $this->replacer->replaceText($file, 'text', 'pants', ['keepOldFile' => false]); |
||
148 | |||
149 | static::assertTrue($newFile->exists()); |
||
150 | static::assertFalse($file->exists()); |
||
151 | } |
||
152 | |||
153 | View Code Duplication | public function testCallingModifyReplacesText() |
|
154 | { |
||
155 | $file = new LocalFile(static::$dir . 'simple_replace.test'); |
||
156 | $file->put('some text that text should be replaced'); |
||
157 | |||
158 | $newFile = $this->replacer->modify($file, ['fromText' => 'text', 'toText' => 'pants']); |
||
159 | |||
160 | static::assertNotNull($newFile); |
||
161 | static::assertEquals(['some pants that pants should be replaced'], $newFile->getContents()); |
||
162 | } |
||
163 | |||
164 | public function testCallingModifyWillPassThroughOptions() |
||
165 | { |
||
166 | $file = new LocalFile(static::$dir . 'option_pass_through.test'); |
||
167 | $file->put('some text that text should be replaced'); |
||
168 | |||
169 | $newFile = $this->replacer->modify( |
||
170 | $file, |
||
171 | [ |
||
172 | 'fromText' => 'text', |
||
173 | 'toText' => 'pants', |
||
174 | 'postfix' => 'pass', |
||
175 | 'keepOldFile' => false, |
||
176 | ] |
||
177 | ); |
||
178 | |||
179 | static::assertTrue($newFile->exists()); |
||
180 | static::assertFalse($file->exists()); |
||
181 | static::assertNotNull($newFile); |
||
182 | static::assertEquals('option_pass_through-pass.test', $newFile->getFilename()); |
||
183 | } |
||
184 | |||
185 | View Code Duplication | public function testCallingModifyWithNoFromTextThrowsInvalidArgumentsException() |
|
186 | { |
||
187 | $this->expectException(InvalidArgumentException::class); |
||
188 | |||
189 | $file = new LocalFile(static::$dir . 'simple_replace.test'); |
||
190 | $file->put('some text that text should be replaced'); |
||
191 | |||
192 | $this->replacer->modify($file, ['toText' => 'pants']); |
||
193 | } |
||
194 | |||
195 | View Code Duplication | public function testCallingModifyWithNoToTextThrowsInvalidArgumentsException() |
|
196 | { |
||
197 | $this->expectException(InvalidArgumentException::class); |
||
198 | |||
199 | $file = new LocalFile(static::$dir . 'simple_replace.test'); |
||
200 | $file->put('some text that text should be replaced'); |
||
201 | |||
202 | $this->replacer->modify($file, ['fromText' => 'pants']); |
||
203 | } |
||
204 | |||
205 | public function testCallingModifyWithANonLocalFileWillThrowAnException() |
||
206 | { |
||
207 | $file = m::mock(FileNodeInterface::class); |
||
208 | $file->shouldReceive('__toString') |
||
209 | ->andReturn('some/file/here'); |
||
210 | |||
211 | $this->expectException(InvalidArgumentException::class); |
||
212 | |||
213 | $this->replacer->modify($file, ['fromText' => 'pants', 'toText' => 'more pants']); |
||
214 | } |
||
215 | |||
216 | View Code Duplication | public function testCallingReplaceTextOnAFileWithoutAnExtensionWorks() |
|
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.