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 |
||
7 | class ConfigFactoryTest extends TestCase |
||
8 | { |
||
9 | |||
10 | /** |
||
11 | * @test |
||
12 | */ |
||
13 | public function canCreateFromJson() |
||
14 | { |
||
15 | $factory = new ConfigFactory(); |
||
16 | $config = $factory->createFromFile(__DIR__ . '/config.json'); |
||
17 | |||
18 | $this->assertInstanceOf(Config::class, $config); |
||
19 | $this->assertSame('value', $config->get('key')); |
||
20 | } |
||
21 | |||
22 | /** |
||
23 | * @test |
||
24 | */ |
||
25 | public function canIncludePhpFile() |
||
26 | { |
||
27 | $factory = new ConfigFactory(); |
||
28 | $config = $factory->createFromFile(__DIR__ . '/config.php'); |
||
29 | |||
30 | $this->assertInstanceOf(Config::class, $config); |
||
31 | $this->assertSame('value', $config->get('key')); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @test |
||
36 | * @expectedException InvalidArgumentException |
||
37 | */ |
||
38 | public function throwsExceptionOnInvalidFile() |
||
39 | { |
||
40 | $factory = new ConfigFactory(); |
||
41 | $config = $factory->createFromFile(__DIR__ . '/non_existing_file'); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @test |
||
46 | * @expectedException RuntimeException |
||
47 | */ |
||
48 | public function throwsExceptionOnUnknownConfigFormat() |
||
49 | { |
||
50 | $factory = new ConfigFactory(); |
||
51 | $config = $factory->createFromFile(__DIR__ . '/stub.qwerty'); |
||
52 | } |
||
53 | |||
54 | } |
||
55 |