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 UserPatchMessageSpec extends ObjectBehavior |
||
| 11 | { |
||
| 12 | function it_is_initializable() |
||
| 13 | { |
||
| 14 | $this->shouldHaveType(UserPatchMessage::class); |
||
| 15 | } |
||
| 16 | |||
| 17 | function it_should_build() |
||
| 18 | { |
||
| 19 | $this->setFirstName('Example'); |
||
| 20 | $this->setLastName(''); |
||
| 21 | $this->setEmail('[email protected]'); |
||
| 22 | $this->setPlainPassword('12345'); |
||
| 23 | $this->setPhone('+1 234 567 890'); |
||
| 24 | $this->setDefaultLocale('en'); |
||
| 25 | |||
| 26 | $data = [ |
||
| 27 | 'firstName' => 'Example', |
||
| 28 | 'lastName' => '', |
||
| 29 | 'email' => '[email protected]', |
||
| 30 | 'plainPassword' => '12345', |
||
| 31 | 'phone' => '+1 234 567 890', |
||
| 32 | 'defaultLocale' => 'en', |
||
| 33 | ]; |
||
| 34 | |||
| 35 | $this->build()->shouldReturn($data); |
||
| 36 | } |
||
| 37 | |||
| 38 | function it_should_create_from_user(User $user) |
||
| 39 | { |
||
| 40 | $user->getId()->willReturn(1); |
||
| 41 | $user->getFirstName()->willReturn('Example'); |
||
| 42 | $user->getLastName()->willReturn(''); |
||
| 43 | $user->getEmail()->willReturn('[email protected]'); |
||
| 44 | $user->getCompanyId()->willReturn(2); |
||
| 45 | |||
| 46 | $message = new UserPatchMessage(); |
||
| 47 | $message->setId(1); |
||
| 48 | $message->setFirstName('Example'); |
||
| 49 | $message->setLastName(''); |
||
| 50 | $message->setEmail('[email protected]'); |
||
| 51 | $message->setCompanyId(2); |
||
| 52 | |||
| 53 | $this::createFromUser($user)->shouldBeLike($message); |
||
| 54 | } |
||
| 55 | } |
||
| 56 |