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 CompanyPatchMessageSpec extends ObjectBehavior |
||
| 11 | { |
||
| 12 | function it_is_initializable() |
||
| 13 | { |
||
| 14 | $this->shouldHaveType(CompanyPatchMessage::class); |
||
| 15 | } |
||
| 16 | |||
| 17 | function it_should_build() |
||
| 18 | { |
||
| 19 | $this->setParentId(5); |
||
| 20 | $this->setName('First company'); |
||
| 21 | |||
| 22 | $data = [ |
||
| 23 | 'companyName' => 'First company', |
||
| 24 | 'parent' => 5, |
||
| 25 | ]; |
||
| 26 | |||
| 27 | $this->build()->shouldReturn($data); |
||
| 28 | } |
||
| 29 | |||
| 30 | function it_should_create_from_company(Company $company) |
||
| 31 | { |
||
| 32 | $company->getId()->willReturn(1); |
||
| 33 | $company->getName()->willReturn('First company'); |
||
| 34 | $company->getParentId()->willReturn(5); |
||
| 35 | |||
| 36 | $message = new CompanyPatchMessage(); |
||
| 37 | $message->setId(1); |
||
| 38 | $message->setName('First company'); |
||
| 39 | $message->setParentId(5); |
||
| 40 | |||
| 41 | $this::createFromCompany($company)->shouldBeLike($message); |
||
| 42 | } |
||
| 43 | } |
||
| 44 |