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 |
||
10 | class TaggableTraitTest extends BaseTestCase |
||
11 | { |
||
12 | /** |
||
13 | * @var TagRepository |
||
14 | */ |
||
15 | private $tag; |
||
16 | /** |
||
17 | * @var PageRepository |
||
18 | */ |
||
19 | private $page; |
||
20 | |||
21 | public function setUp() |
||
28 | |||
29 | /** @test */ |
||
30 | public function it_creates_tags_on_creation_of_related_model() |
||
38 | |||
39 | /** @test */ |
||
40 | public function it_can_create_multiple_tags_at_once() |
||
48 | |||
49 | /** @test */ |
||
50 | View Code Duplication | public function it_links_tags_to_entity() |
|
60 | |||
61 | /** @test */ |
||
62 | View Code Duplication | public function it_removes_tags_from_link_with_entity() |
|
72 | |||
73 | /** @test */ |
||
74 | View Code Duplication | public function it_can_get_pages_with_one_of_specified_tags() |
|
82 | |||
83 | /** @test */ |
||
84 | View Code Duplication | public function it_gets_pages_with_all_specified_tags() |
|
92 | |||
93 | private function createPage(array $tags = []) |
||
106 | } |
||
107 |
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.