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  | 
            ||
| 31 | class EditOrganizationHandlerSpec extends ObjectBehavior  | 
            ||
| 32 | { | 
            ||
| 33 | private $slug;  | 
            ||
| 34 | private $name;  | 
            ||
| 35 | private $organizationId;  | 
            ||
| 36 | private $editorId;  | 
            ||
| 37 | |||
| 38 | View Code Duplication | function let(OrganizationRepository $repository, EditOrganizationCommand $command)  | 
            |
| 
                                                                                                    
                        
                         | 
                |||
| 39 |     { | 
            ||
| 40 | $this->beConstructedWith($repository);  | 
            ||
| 41 | |||
| 42 |         $command->name()->shouldBeCalled()->willReturn('Organization name'); | 
            ||
| 43 |         $command->slug()->shouldBeCalled()->willReturn('organization-slug'); | 
            ||
| 44 |         $command->id()->shouldBeCalled()->willReturn('organization-id'); | 
            ||
| 45 |         $command->editorId()->shouldBeCalled()->willReturn('editor-id'); | 
            ||
| 46 | |||
| 47 |         $this->slug = new Slug('organization-slug'); | 
            ||
| 48 |         $this->name = new OrganizationName('Organization name'); | 
            ||
| 49 |         $this->organizationId = OrganizationId::generate('organization-id'); | 
            ||
| 50 |         $this->editorId = UserId::generate('editor-id'); | 
            ||
| 51 | }  | 
            ||
| 52 | |||
| 53 | View Code Duplication | function it_edits_an_organization(  | 
            |
| 54 | EditOrganizationCommand $command,  | 
            ||
| 55 | OrganizationRepository $repository,  | 
            ||
| 56 | Organization $organization  | 
            ||
| 57 |     ) { | 
            ||
| 58 | $this->shouldHaveType(EditOrganizationHandler::class);  | 
            ||
| 59 | |||
| 60 | $repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization);  | 
            ||
| 61 | $organization->isOwner($this->editorId)->shouldBeCalled()->willReturn(true);  | 
            ||
| 62 | $repository->organizationOfSlug($this->slug)->shouldBeCalled()->willReturn(null);  | 
            ||
| 63 | $organization->edit($this->name, $this->slug)->shouldBeCalled();  | 
            ||
| 64 | $repository->persist(Argument::type(Organization::class))->shouldBeCalled();  | 
            ||
| 65 | $this->__invoke($command);  | 
            ||
| 66 | }  | 
            ||
| 67 | |||
| 68 | function it_edits_an_organization_with_the_same_slug(  | 
            ||
| 84 | |||
| 85 | function it_edits_an_organization_without_slug(  | 
            ||
| 86 | EditOrganizationCommand $command,  | 
            ||
| 87 | OrganizationRepository $repository,  | 
            ||
| 88 | Organization $organization  | 
            ||
| 89 |     ) { | 
            ||
| 90 | $command->slug()->shouldBeCalled()->willReturn(null);  | 
            ||
| 91 |         $slug = new Slug('Organization name'); | 
            ||
| 92 | |||
| 93 | $repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization);  | 
            ||
| 94 | $organization->isOwner($this->editorId)->shouldBeCalled()->willReturn(true);  | 
            ||
| 95 | $repository->organizationOfSlug($slug)->shouldBeCalled()->willReturn(null);  | 
            ||
| 96 | $organization->edit($this->name, $slug)->shouldBeCalled();  | 
            ||
| 97 | $repository->persist(Argument::type(Organization::class))->shouldBeCalled();  | 
            ||
| 98 | $this->__invoke($command);  | 
            ||
| 99 | }  | 
            ||
| 100 | |||
| 101 | View Code Duplication | function it_does_not_edits_an_organization_because_the_organization_does_not_exist(  | 
            |
| 102 | EditOrganizationCommand $command,  | 
            ||
| 103 | OrganizationRepository $repository  | 
            ||
| 104 |     ) { | 
            ||
| 105 | $repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn(null);  | 
            ||
| 106 | $this->shouldThrow(OrganizationDoesNotExistException::class)->during__invoke($command);  | 
            ||
| 107 | }  | 
            ||
| 108 | |||
| 109 | function it_does_not_edits_an_organization_because_the_organization_slug_already_exists(  | 
            ||
| 110 | EditOrganizationCommand $command,  | 
            ||
| 111 | OrganizationRepository $repository,  | 
            ||
| 112 | Organization $organization,  | 
            ||
| 113 | Organization $organization2  | 
            ||
| 114 |     ) { | 
            ||
| 115 |         $organizationId2 = OrganizationId::generate('organization-id-2'); | 
            ||
| 116 | |||
| 117 | $repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization);  | 
            ||
| 118 | $organization->isOwner($this->editorId)->shouldBeCalled()->willReturn(true);  | 
            ||
| 119 | $repository->organizationOfSlug($this->slug)->shouldBeCalled()->willReturn($organization2);  | 
            ||
| 120 | $organization2->id()->shouldBeCalled()->willReturn($organizationId2);  | 
            ||
| 121 | $this->shouldThrow(OrganizationAlreadyExistsException::class)->during__invoke($command);  | 
            ||
| 122 | }  | 
            ||
| 123 | |||
| 124 | View Code Duplication | function it_does_not_edits_an_organization_because_the_owner_does_not_authorized(  | 
            |
| 133 | }  | 
            ||
| 134 | 
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.