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 |
||
8 | class StatementTest extends \PHPUnit_Framework_TestCase |
||
9 | { |
||
10 | public function testBankAssesor() |
||
18 | |||
19 | public function testAccountAssesor() |
||
27 | |||
28 | public function testTransactionsAssesor() |
||
29 | { |
||
30 | $expected = [ |
||
31 | new Transaction(), |
||
32 | new Transaction(), |
||
33 | ]; |
||
34 | $statement = new Statement(); |
||
35 | $statement->setTransactions($expected); |
||
36 | |||
37 | $this->assertEquals($expected, $statement->getTransactions()); |
||
38 | } |
||
39 | |||
40 | public function testStartPriceAssesor() |
||
48 | |||
49 | public function testEndPriceAssesor() |
||
57 | |||
58 | /** |
||
59 | * @expectedException PHPUnit_Framework_Error_Deprecated |
||
60 | * @expectedExceptionMessage Deprecated in favor of splitting the start and end timestamps for a statement. Please use setStartTimestamp($format) or setEndTimestamp($format) instead. setTimestamp is now setStartTimestamp |
||
61 | */ |
||
62 | View Code Duplication | public function testDeprecatedTimestampAssesor() |
|
70 | |||
71 | public function testTimestampAssesor() |
||
83 | |||
84 | public function testNumberAssesor() |
||
92 | |||
93 | /** |
||
94 | * @depends testStartPriceAssesor |
||
95 | * @depends testEndPriceAssesor |
||
96 | */ |
||
97 | public function testDeltaPrice() |
||
106 | |||
107 | /** |
||
108 | * @depends testTransactionsAssesor |
||
109 | */ |
||
110 | public function testAddTransaction() |
||
111 | { |
||
112 | $statement = new Statement(); |
||
113 | $statement->setTransactions([ |
||
114 | new Transaction(), |
||
115 | new Transaction(), |
||
116 | ]); |
||
117 | $statement->addTransaction(new Transaction()); |
||
118 | |||
119 | $this->assertCount(3, $statement->getTransactions()); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @depends testTimestampAssesor |
||
124 | * @expectedException PHPUnit_Framework_Error_Deprecated |
||
125 | */ |
||
126 | View Code Duplication | public function testGetTimestampWithFormat() |
|
134 | |||
135 | public function testJsonSerialization() |
||
154 | |||
155 | /** |
||
156 | * @depends testJsonSerialization |
||
157 | * @expectedException PHPUnit_Framework_Error_Deprecated |
||
158 | */ |
||
159 | public function testJsonSerializationWithTransactions() |
||
202 | } |
||
203 |
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.