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 RedirectTest extends TestCase |
||
13 | { |
||
14 | /** |
||
15 | * @dataProvider sourceProvider |
||
16 | * |
||
17 | * @test |
||
18 | */ |
||
19 | public function set_source($source, $expected) |
||
25 | |||
26 | /** |
||
27 | * @dataProvider destinationProvider |
||
28 | * |
||
29 | * @test |
||
30 | */ |
||
31 | public function set_destination($destination, $expectedDestination) |
||
37 | |||
38 | /** |
||
39 | * @test |
||
40 | */ |
||
41 | public function get_last_accessed_at() |
||
50 | |||
51 | /** |
||
52 | * @test |
||
53 | */ |
||
54 | public function increase_count() |
||
66 | |||
67 | /** |
||
68 | * @test |
||
69 | */ |
||
70 | public function create_from_not_found() |
||
76 | |||
77 | View Code Duplication | public function sourceProvider() |
|
97 | |||
98 | public function destinationProvider() |
||
111 | |||
112 | /** |
||
113 | * @return \Zenstruck\RedirectBundle\Model\Redirect |
||
114 | */ |
||
115 | private function createRedirect($source, $destination, $permanent = true) |
||
122 | } |
||
123 |
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.