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 |
||
13 | final class FileSystemAdapterTest extends PHPUnit_Framework_TestCase |
||
14 | { |
||
15 | /** |
||
16 | * @var MockInterface|FileSystemInterface |
||
17 | */ |
||
18 | private $fileSystemMock; |
||
19 | |||
20 | /** |
||
21 | * @var FileSystemAdapter |
||
22 | */ |
||
23 | private $adapter; |
||
24 | |||
25 | /** |
||
26 | * @before |
||
27 | */ |
||
28 | public function setUpAdapter() |
||
33 | |||
34 | /** |
||
35 | * @test |
||
36 | * |
||
37 | * @small |
||
38 | */ |
||
39 | public function readFile() |
||
46 | |||
47 | /** |
||
48 | * @test |
||
49 | * |
||
50 | * @small |
||
51 | * |
||
52 | * @expectedException \Hgraca\Phorensic\SharedKernel\Port\FileSystem\Exception\InvalidPathException |
||
53 | */ |
||
54 | View Code Duplication | public function readFile_ThrowsExceptionIfInvalidPath() |
|
61 | |||
62 | /** |
||
63 | * @test |
||
64 | * |
||
65 | * @small |
||
66 | * |
||
67 | * @expectedException \Hgraca\Phorensic\SharedKernel\Port\FileSystem\Exception\FileNotFoundException |
||
68 | */ |
||
69 | View Code Duplication | public function readFile_ThrowsExceptionIfPathNotFound() |
|
76 | |||
77 | /** |
||
78 | * @dataProvider dataProvider_test_getExtension |
||
79 | */ |
||
80 | public function test_getExtension(string $path, string $expectedExtension) |
||
86 | |||
87 | public function dataProvider_test_getExtension() |
||
95 | } |
||
96 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: