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 namespace Modules\Media\Tests; |
||
8 | class ImagyTest extends MediaTestCase |
||
9 | { |
||
10 | /** |
||
11 | * @var Imagy |
||
12 | */ |
||
13 | protected $imagy; |
||
14 | /** |
||
15 | * @var \Illuminate\Filesystem\Filesystem |
||
16 | */ |
||
17 | protected $finder; |
||
18 | /** |
||
19 | * @var \Illuminate\Contracts\Config\Repository |
||
20 | */ |
||
21 | protected $config; |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $mediaPath; |
||
26 | private $testbenchPublicPath; |
||
27 | |||
28 | /** |
||
29 | * |
||
30 | */ |
||
31 | public function setUp() |
||
43 | |||
44 | public function tearDown() |
||
49 | |||
50 | /** @test */ |
||
51 | public function it_should_create_a_file() |
||
52 | { |
||
53 | if ($this->finder->isFile("{$this->mediaPath}google-map_smallThumb.png")) { |
||
54 | $this->finder->delete("{$this->mediaPath}google-map_smallThumb.png"); |
||
55 | } |
||
56 | |||
57 | $this->imagy->get("/google-map.png", 'smallThumb', true); |
||
58 | |||
59 | $this->assertTrue($this->finder->isFile("{$this->testbenchPublicPath}google-map_smallThumb.png")); |
||
60 | } |
||
61 | |||
62 | /** @test */ |
||
63 | public function it_should_not_create_thumbs_for_pdf_files() |
||
69 | |||
70 | /** @test */ |
||
71 | View Code Duplication | public function it_should_return_thumbnail_path() |
|
79 | |||
80 | /** @test */ |
||
81 | View Code Duplication | public function it_should_return_same_path_for_non_images() |
|
88 | |||
89 | /** @test */ |
||
90 | public function it_should_detect_an_image() |
||
100 | } |
||
101 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.