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 CategoryManagerSpec extends ObjectBehavior |
||
| 11 | { |
||
| 12 | public function it_is_initializable() |
||
| 16 | |||
| 17 | public function let(CategoryDB $categoryRepository) |
||
| 21 | |||
| 22 | /** @test */ |
||
| 23 | public function it_find_a_category_by_name(CategoryDB $categoryRepository) |
||
| 32 | |||
| 33 | /** @test */ |
||
| 34 | public function it_try_to_find_a_non_existing_category(CategoryDB $categoryRepository) |
||
| 44 | |||
| 45 | /** @test */ |
||
| 46 | View Code Duplication | public function it_store_a_category(CategoryDB $categoryRepository) |
|
|
|
|||
| 47 | { |
||
| 48 | $categoryName = 'hello'; |
||
| 49 | $categoryText = 'wow'; |
||
| 50 | |||
| 51 | $categoryRepository->add($categoryName, $categoryText)->shouldBeCalled() |
||
| 52 | ->willReturn(new NotificationCategory()); |
||
| 53 | |||
| 54 | $this->add($categoryName, $categoryText)->shouldReturnAnInstanceOf(NotificationCategory::class); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** @test */ |
||
| 58 | public function it_delete_a_category_by_id(CategoryDB $categoryRepository) |
||
| 69 | |||
| 70 | /** @test */ |
||
| 71 | public function it_delete_a_category_by_name(CategoryDB $categoryRepository) |
||
| 80 | } |
||
| 81 |
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.