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 |
||
5 | abstract class BaseDatabase extends \PHPUnit\Framework\TestCase |
||
6 | { |
||
7 | protected $uri = null; |
||
8 | |||
9 | /** |
||
10 | * @var Migration |
||
11 | */ |
||
12 | protected $migrate = null; |
||
13 | |||
14 | protected $migrationTable = "migration_version"; |
||
15 | |||
16 | /** |
||
17 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
18 | */ |
||
19 | public function setUp() |
||
25 | |||
26 | /** |
||
27 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
28 | */ |
||
29 | public function tearDown() |
||
33 | |||
34 | /** |
||
35 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
36 | * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException |
||
37 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
38 | * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile |
||
39 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
40 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
41 | */ |
||
42 | public function testVersion0() |
||
47 | |||
48 | /** |
||
49 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
50 | * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException |
||
51 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
52 | * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile |
||
53 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
54 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
55 | */ |
||
56 | public function testUpVersion1() |
||
63 | |||
64 | /** |
||
65 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
66 | * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException |
||
67 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
68 | * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile |
||
69 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
70 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
71 | */ |
||
72 | public function testUpVersion2() |
||
79 | |||
80 | /** |
||
81 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
82 | * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException |
||
83 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
84 | * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile |
||
85 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
86 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
87 | */ |
||
88 | public function testDownVersion1() |
||
95 | |||
96 | /** |
||
97 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
98 | * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException |
||
99 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
100 | * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile |
||
101 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
102 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
103 | */ |
||
104 | public function testDownVersion0() |
||
111 | |||
112 | protected function getExpectedUsersVersion0() |
||
119 | |||
120 | protected function getExpectedUsersVersion1() |
||
127 | |||
128 | /** |
||
129 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
130 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
131 | */ |
||
132 | View Code Duplication | protected function assertVersion0() |
|
163 | |||
164 | /** |
||
165 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
166 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
167 | */ |
||
168 | View Code Duplication | protected function assertVersion1() |
|
199 | |||
200 | /** |
||
201 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
202 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
203 | */ |
||
204 | View Code Duplication | protected function assertVersion2() |
|
231 | |||
232 | /** |
||
233 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
234 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
235 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
236 | * @expectedException \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
237 | */ |
||
238 | public function testGetCurrentVersionIsEmpty() |
||
242 | |||
243 | /** |
||
244 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
245 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
246 | */ |
||
247 | public function testCreateVersion() |
||
268 | } |
||
269 |
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.