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 |
||
9 | class SqliteSchemaManagerTest extends \PHPUnit\Framework\TestCase |
||
10 | { |
||
11 | /** |
||
12 | * @dataProvider getDataColumnCollation |
||
13 | */ |
||
14 | View Code Duplication | public function testParseColumnCollation($collation, string $column, string $sql) : void |
|
|
|||
15 | { |
||
16 | $conn = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock(); |
||
17 | $conn->method('getDatabasePlatform')->willReturn(new SqlitePlatform()); |
||
18 | |||
19 | $manager = new SqliteSchemaManager($conn); |
||
20 | $ref = new \ReflectionMethod($manager, 'parseColumnCollationFromSQL'); |
||
21 | $ref->setAccessible(true); |
||
22 | |||
23 | self::assertEquals($collation, $ref->invoke($manager, $column, $sql)); |
||
24 | } |
||
25 | |||
26 | public function getDataColumnCollation() |
||
27 | { |
||
28 | return [ |
||
29 | [ |
||
30 | 'RTRIM', 'a', 'CREATE TABLE "a" ("a" text DEFAULT "aa" COLLATE "RTRIM" NOT NULL)', |
||
31 | ], |
||
32 | [ |
||
33 | 'utf-8', 'a', 'CREATE TABLE "a" ("b" text UNIQUE NOT NULL COLLATE NOCASE, "a" text DEFAULT "aa" COLLATE "utf-8" NOT NULL)', |
||
34 | ], |
||
35 | [ |
||
36 | 'NOCASE', 'a', 'CREATE TABLE "a" ("a" text DEFAULT (lower(ltrim(" a") || rtrim("a "))) CHECK ("a") NOT NULL COLLATE NOCASE UNIQUE, "b" text COLLATE RTRIM)', |
||
37 | ], |
||
38 | [ |
||
39 | false, 'a', 'CREATE TABLE "a" ("a" text CHECK ("a") NOT NULL, "b" text COLLATE RTRIM)', |
||
40 | ], |
||
41 | [ |
||
42 | 'RTRIM', 'a"b', 'CREATE TABLE "a" ("a""b" text COLLATE RTRIM)', |
||
43 | ], |
||
44 | [ |
||
45 | 'BINARY', 'b', 'CREATE TABLE "a" (bb TEXT COLLATE RTRIM, b VARCHAR(42) NOT NULL COLLATE BINARY)', |
||
46 | ], |
||
47 | [ |
||
48 | 'BINARY', 'b', 'CREATE TABLE "a" (bbb TEXT COLLATE NOCASE, bb TEXT COLLATE RTRIM, b VARCHAR(42) NOT NULL COLLATE BINARY)', |
||
49 | ], |
||
50 | [ |
||
51 | 'BINARY', 'b', 'CREATE TABLE "a" (b VARCHAR(42) NOT NULL COLLATE BINARY, bb TEXT COLLATE RTRIM)', |
||
52 | ], |
||
53 | ]; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @dataProvider getDataColumnComment |
||
58 | */ |
||
59 | View Code Duplication | public function testParseColumnCommentFromSQL($comment, string $column, string $sql) : void |
|
60 | { |
||
61 | $conn = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock(); |
||
62 | $conn->method('getDatabasePlatform')->willReturn(new SqlitePlatform()); |
||
63 | |||
64 | $manager = new SqliteSchemaManager($conn); |
||
65 | $ref = new \ReflectionMethod($manager, 'parseColumnCommentFromSQL'); |
||
66 | $ref->setAccessible(true); |
||
67 | |||
68 | self::assertSame($comment, $ref->invoke($manager, $column, $sql)); |
||
69 | } |
||
70 | |||
71 | public function getDataColumnComment() |
||
100 | } |
||
101 |
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.