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 |
||
| 18 | class CombineKeysCollectionTest extends UnitTestCase |
||
| 19 | { |
||
| 20 | View Code Duplication | public function testCombineKeysUsesIncomingTraversableAsKeysForCollectionsValues() |
|
|
|
|||
| 21 | { |
||
| 22 | $coll = Factory::create($this->fixtures['names']); |
||
| 23 | $orig = $coll->toArray(); |
||
| 24 | $newkeys = $coll->combineKeys($this->fixtures['emails']); |
||
| 25 | $this->assertEquals( |
||
| 26 | [ |
||
| 27 | '[email protected]' => 'Chelsea', |
||
| 28 | '[email protected]' => 'Adella', |
||
| 29 | '[email protected]' => 'Monte', |
||
| 30 | '[email protected]' => 'Maye', |
||
| 31 | '[email protected]' => 'Lottie', |
||
| 32 | '[email protected]' => 'Don', |
||
| 33 | '[email protected]' => 'Dayton', |
||
| 34 | '[email protected]' => 'Kirk', |
||
| 35 | '[email protected]' => 'Troy', |
||
| 36 | '[email protected]' => 'Nakia', |
||
| 37 | ], |
||
| 38 | $newkeys->toArray() |
||
| 39 | ); |
||
| 40 | $this->assertNotSame($newkeys->toArray(), $orig); |
||
| 41 | $this->assertSame( |
||
| 42 | $coll->toArray(), |
||
| 43 | $orig, |
||
| 44 | 'The original collection should not be affected by Collection::combineKeys().' |
||
| 45 | ); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @expectedException \InvalidArgumentException |
||
| 50 | * @expectedExceptionMessage Invalid input type for combineKeys. |
||
| 51 | */ |
||
| 52 | public function testCombineKeysThrowsExceptionIfPassedNonTraversableNonArray() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @expectedException \InvalidArgumentException |
||
| 60 | * @expectedExceptionMessage Invalid input for combineKeys, number of items does not match. |
||
| 61 | */ |
||
| 62 | public function testCombineKeysThrowsExceptionIfPassedTraversableWithBadCount() |
||
| 67 | |||
| 68 | View Code Duplication | public function testCombineKeysAcceptsTraversable() |
|
| 95 | } |
||
| 96 |
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.