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 |
||
| 17 | class RouteMatcherSpec extends ObjectBehavior |
||
| 18 | { |
||
| 19 | |||
| 20 | function let(RouteParser $parser, FastrouteDispatcherFactory $factory, Route $route, Dispatcher $dispatcher) |
||
| 21 | { |
||
| 22 | $parser->parse(Argument::containing($route->getWrappedObject()))->willReturn(['parsed']); |
||
| 23 | $factory->create(['parsed'])->willReturn($dispatcher); |
||
| 24 | $this->beConstructedWith($parser, $factory); |
||
| 25 | } |
||
| 26 | |||
| 27 | function it_is_initializable() |
||
| 28 | { |
||
| 29 | $this->shouldImplement(\Venta\Contracts\Routing\RouteMatcher::class); |
||
| 30 | } |
||
| 31 | |||
| 32 | function it_matches_route( |
||
| 33 | ServerRequestInterface $request, |
||
| 34 | RouteCollection $routeCollection, |
||
| 35 | Route $route, |
||
| 36 | UriInterface $uri, |
||
| 37 | Dispatcher $dispatcher |
||
| 38 | ) { |
||
| 39 | $routeCollection->getRoutes()->willReturn([$route]); |
||
| 40 | $request->getUri()->willReturn($uri); |
||
| 41 | $uri->getPath()->willReturn('/url'); |
||
| 42 | $request->getMethod()->willReturn('GET'); |
||
| 43 | $dispatcher->dispatch('GET', '/url')->willReturn([Dispatcher::FOUND, $route, ['vars']]); |
||
| 44 | $route->withVariables(['vars'])->willReturn($route); |
||
| 45 | $this->match($request, $routeCollection)->shouldBe($route); |
||
| 46 | } |
||
| 47 | |||
| 48 | function it_throws_not_allowed_exception( |
||
| 49 | ServerRequestInterface $request, |
||
| 50 | RouteCollection $routeCollection, |
||
| 51 | Route $route, |
||
| 52 | UriInterface $uri, |
||
| 53 | Dispatcher $dispatcher |
||
| 54 | ) { |
||
| 55 | $routeCollection->getRoutes()->willReturn([$route]); |
||
| 56 | $request->getUri()->willReturn($uri); |
||
| 57 | $uri->getPath()->willReturn('/url'); |
||
| 58 | $request->getMethod()->willReturn('GET'); |
||
| 59 | $dispatcher->dispatch('GET', '/url')->willReturn([Dispatcher::METHOD_NOT_ALLOWED, ['POST']]); |
||
| 60 | $this->shouldThrow(NotAllowedException::class)->match($request, $routeCollection); |
||
| 61 | } |
||
| 62 | |||
| 63 | function it_throws_not_found_exception( |
||
| 64 | ServerRequestInterface $request, |
||
| 65 | RouteCollection $routeCollection, |
||
| 66 | Route $route, |
||
| 67 | UriInterface $uri, |
||
| 68 | Dispatcher $dispatcher |
||
| 69 | ) { |
||
| 70 | $routeCollection->getRoutes()->willReturn([$route]); |
||
| 71 | $request->getUri()->willReturn($uri); |
||
| 72 | $uri->getPath()->willReturn('/url'); |
||
| 73 | $request->getMethod()->willReturn('GET'); |
||
| 74 | $dispatcher->dispatch('GET', '/url')->willReturn([Dispatcher::NOT_FOUND]); |
||
| 75 | $this->shouldThrow(NotFoundException::class)->match($request, $routeCollection); |
||
| 76 | } |
||
| 77 | } |
||
| 78 |