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 |
||
12 | class DropDatabaseDoctrineTest extends TestCase |
||
13 | { |
||
14 | View Code Duplication | public function testExecute() : void |
|
45 | |||
46 | View Code Duplication | public function testExecuteWithAlias(): void |
|
47 | { |
||
48 | $connectionName = 'default'; |
||
49 | $dbName = 'test'; |
||
50 | $params = [ |
||
51 | 'url' => 'sqlite:///' . sys_get_temp_dir() . '/test.db', |
||
52 | 'path' => sys_get_temp_dir() . '/' . $dbName, |
||
53 | 'driver' => 'pdo_sqlite', |
||
54 | ]; |
||
55 | |||
56 | $container = $this->getMockContainer($connectionName, $params); |
||
57 | |||
58 | $application = new Application(); |
||
59 | $application->add(new DropDatabaseDoctrineCommand($container->get('doctrine'))); |
||
60 | |||
61 | $command = $application->find('doctrine:database:drop'); |
||
62 | |||
63 | $commandTester = new CommandTester($command); |
||
64 | $commandTester->execute( |
||
65 | array_merge(['command' => $command->getName(), '-f' => true]) |
||
66 | ); |
||
67 | |||
68 | $this->assertContains( |
||
69 | sprintf( |
||
70 | 'Dropped database %s for connection named %s', |
||
71 | sys_get_temp_dir() . '/' . $dbName, |
||
72 | $connectionName |
||
73 | ), |
||
74 | $commandTester->getDisplay() |
||
75 | ); |
||
76 | } |
||
77 | |||
78 | public function testItThrowsWhenUsingIfExistsWithAnIncompatibleDriver(): void |
||
79 | { |
||
80 | $connectionName = 'default'; |
||
81 | $dbName = 'test'; |
||
82 | $params = [ |
||
83 | 'url' => 'sqlite:///' . sys_get_temp_dir() . '/test.db', |
||
84 | 'path' => sys_get_temp_dir() . '/' . $dbName, |
||
85 | 'driver' => 'pdo_sqlite', |
||
86 | ]; |
||
87 | |||
88 | $container = $this->getMockContainer($connectionName, $params); |
||
89 | |||
90 | $application = new Application(); |
||
91 | $application->add(new DropDatabaseDoctrineCommand($container->get('doctrine'))); |
||
92 | |||
93 | $command = $application->find('doctrine:database:drop'); |
||
94 | |||
95 | $commandTester = new CommandTester($command); |
||
96 | |||
97 | static::expectException(DBALException::class); |
||
98 | $commandTester->execute( |
||
99 | array_merge(['command' => $command->getName(), '-f' => true, '-e' => true]) |
||
100 | ); |
||
101 | |||
102 | $this->assertContains( |
||
103 | sprintf( |
||
104 | 'Dropped database %s for connection named %s', |
||
105 | sys_get_temp_dir() . '/' . $dbName, |
||
106 | $connectionName |
||
107 | ), |
||
108 | $commandTester->getDisplay() |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | public function testExecuteWithoutOptionForceWillFailWithAttentionMessage() : void |
||
143 | |||
144 | /** |
||
145 | * @param array|null $params Connection parameters |
||
146 | */ |
||
147 | View Code Duplication | private function getMockContainer(string $connectionName, array $params = null) : MockObject |
|
184 | } |
||
185 |
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.