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 |
||
| 23 | class FullUrlTransformerSpec extends ObjectBehavior |
||
| 24 | { |
||
| 25 | function it_is_initializable() |
||
| 26 | { |
||
| 27 | $this->shouldHaveType(FullUrlTransformer::class); |
||
| 28 | } |
||
| 29 | |||
| 30 | function its_a_location_transformer() |
||
| 31 | { |
||
| 32 | $this->shouldBeAnInstanceOf(LocationTransformerInterface::class); |
||
| 33 | } |
||
| 34 | |||
| 35 | function it_returns_the_passed_location_when_its_a_full_URL() |
||
| 36 | { |
||
| 37 | $this->transform('http://test.com/path') |
||
| 38 | ->shouldBeAnUriLike('http://test.com/path'); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function getMatchers() |
||
| 42 | { |
||
| 43 | return [ |
||
| 44 | 'beAnUriLike' => function ($uri, $path) |
||
| 45 | { |
||
| 46 | if (!$uri instanceof UriInterface) { |
||
| 47 | $class = UriInterface::class; |
||
| 48 | $type = gettype($uri); |
||
| 49 | throw new FailureException( |
||
| 50 | "Expected {$class} instance, but got '{$type}'" |
||
| 51 | ); |
||
| 52 | } |
||
| 53 | if ($uri->__toString() !== $path) { |
||
| 54 | throw new FailureException( |
||
| 55 | "Expected URI with path '{$path}', but got '{$uri}'" |
||
| 56 | ); |
||
| 57 | } |
||
| 58 | return true; |
||
| 59 | } |
||
| 60 | ]; |
||
| 61 | } |
||
| 62 | } |