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 |
||
| 12 | class LocationPatchMessageSpec extends ObjectBehavior |
||
| 13 | { |
||
| 14 | function it_is_initializable() |
||
| 15 | { |
||
| 16 | $this->shouldHaveType(LocationPatchMessage::class); |
||
| 17 | } |
||
| 18 | |||
| 19 | function it_should_build() |
||
| 20 | { |
||
| 21 | $translation = new LocationTranslationMessage(); |
||
| 22 | $translation->setLocale('en'); |
||
| 23 | $translation->setTitle('Some location'); |
||
| 24 | $translation->setOpeningHours('24/7'); |
||
| 25 | |||
| 26 | $this->setTel('+1 123 456 78 90'); |
||
| 27 | $this->setTel2('+2 456 111 78 90'); |
||
| 28 | $this->setFax('+3 111 222 78 11'); |
||
| 29 | $this->setMail('[email protected]'); |
||
| 30 | $this->setAddress('88 St Patrick St'); |
||
| 31 | $this->setPostalCode('M5T 1V1'); |
||
| 32 | $this->setTown('Toronto'); |
||
| 33 | $this->setCountry('Canada'); |
||
| 34 | $this->setAddressForGoogleMap('88 St Patrick St, Toronto, M5T 1V1, Canada'); |
||
| 35 | $this->setDefaultLocation(true); |
||
| 36 | $this->setLatitude('43.6527222'); |
||
| 37 | $this->setLongitude('-79.3918831'); |
||
| 38 | $this->addTranslation($translation); |
||
| 39 | |||
| 40 | $translationData = [ |
||
| 41 | 'title' => 'Some location', |
||
| 42 | 'openingHours' => '24/7', |
||
| 43 | ]; |
||
| 44 | |||
| 45 | $data = [ |
||
| 46 | 'tel' => '+1 123 456 78 90', |
||
| 47 | 'tel2' => '+2 456 111 78 90', |
||
| 48 | 'fax' => '+3 111 222 78 11', |
||
| 49 | 'mail' => '[email protected]', |
||
| 50 | 'address' => '88 St Patrick St', |
||
| 51 | 'postalCode' => 'M5T 1V1', |
||
| 52 | 'town' => 'Toronto', |
||
| 53 | 'country' => 'Canada', |
||
| 54 | 'addressForGoogleMap' => '88 St Patrick St, Toronto, M5T 1V1, Canada', |
||
| 55 | 'defaultLocation' => true, |
||
| 56 | 'latitude' => '43.6527222', |
||
| 57 | 'longitude' => '-79.3918831', |
||
| 58 | 'translations' => ['en' => $translationData] |
||
| 59 | ]; |
||
| 60 | |||
| 61 | $this->build()->shouldReturn($data); |
||
| 62 | } |
||
| 63 | |||
| 64 | function it_should_create_from_location(Location $location, LocationTranslation $translation) |
||
| 65 | { |
||
| 66 | $translation->getLocale()->willReturn('en'); |
||
| 67 | $translation->getTitle()->willReturn('Some location'); |
||
| 68 | $translation->getOpeningHours()->willReturn('24/7'); |
||
| 69 | |||
| 70 | $location->getId()->willReturn(1); |
||
| 71 | $location->getTel()->willReturn('+1 123 456 78 90'); |
||
| 72 | $location->getFax()->willReturn('+3 111 222 78 11'); |
||
| 73 | $location->getMail()->willReturn('[email protected]'); |
||
| 74 | $location->getAddress()->willReturn('88 St Patrick St'); |
||
| 75 | $location->getPostalCode()->willReturn('M5T 1V1'); |
||
| 76 | $location->getTown()->willReturn('Toronto'); |
||
| 77 | $location->getCountry()->willReturn('Canada'); |
||
| 78 | $location->isDefaultLocation()->willReturn(true); |
||
| 79 | $location->getLatitude()->willReturn('43.6527222'); |
||
| 80 | $location->getLongitude()->willReturn('-79.3918831'); |
||
| 81 | $location->getTranslations()->willReturn([$translation]); |
||
| 82 | |||
| 83 | $transMessage = new LocationTranslationMessage(); |
||
| 84 | $transMessage->setLocale('en'); |
||
| 85 | $transMessage->setTitle('Some location'); |
||
| 86 | $transMessage->setOpeningHours('24/7'); |
||
| 87 | |||
| 88 | $message = new LocationPatchMessage(); |
||
| 89 | $message->setId(1); |
||
| 90 | $message->setTel('+1 123 456 78 90'); |
||
| 91 | $message->setFax('+3 111 222 78 11'); |
||
| 92 | $message->setMail('[email protected]'); |
||
| 93 | $message->setAddress('88 St Patrick St'); |
||
| 94 | $message->setPostalCode('M5T 1V1'); |
||
| 95 | $message->setTown('Toronto'); |
||
| 96 | $message->setCountry('Canada'); |
||
| 97 | $message->setDefaultLocation(true); |
||
| 98 | $message->setLatitude('43.6527222'); |
||
| 99 | $message->setLongitude('-79.3918831'); |
||
| 100 | $message->addTranslation($transMessage); |
||
| 101 | |||
| 102 | $this::createFromLocation($location)->shouldBeLike($message); |
||
| 103 | } |
||
| 104 | } |
||
| 105 |