ChainRequestMatcher   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 54
Duplicated Lines 5.56 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 3
loc 54
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 3 10 5
B matchRequest() 0 30 6

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Skalpa\Silex\Symfony\Routing;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
7
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
8
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
9
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
10
use Symfony\Component\Routing\RequestContext;
11
12
/**
13
 * Calls several request matchers until one successfully recognizes the request.
14
 */
15
class ChainRequestMatcher implements RequestMatcherInterface
16
{
17
    private $context;
18
    private $matchers;
19
20
    /**
21
     * @param RequestMatcherInterface[]|UrlMatcherInterface[] $matchers
22
     * @param RequestContext                                  $context
23
     */
24 10
    public function __construct(array $matchers, RequestContext $context)
25
    {
26 10
        foreach ($matchers as $matcher) {
27 10 View Code Duplication
            if (!$matcher instanceof RequestMatcherInterface && !$matcher instanceof UrlMatcherInterface) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28 1
                throw new \InvalidArgumentException(sprintf('Invalid request matcher. Expected an instance of %s or %s, got %s.', RequestMatcherInterface::class, UrlMatcherInterface::class, is_object($matcher) ? get_class($matcher) : gettype($matcher)));
29
            }
30 9
        }
31 9
        $this->matchers = $matchers;
32 9
        $this->context = $context;
33 9
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 9
    public function matchRequest(Request $request)
39
    {
40
        /** @var ResourceNotFoundException $notFound */
41 9
        $notFound = null;
42
        /** @var MethodNotAllowedException $badMethod */
43 9
        $badMethod = null;
44
45 9
        foreach ($this->matchers as $matcher) {
46
            try {
47 9
                if ($matcher instanceof RequestMatcherInterface) {
48 9
                    return $matcher->matchRequest($request);
49
                } else {
50 1
                    $matcher->setContext($this->context);
51
52 1
                    return $matcher->match($request->getPathInfo());
53
                }
54 5
            } catch (ResourceNotFoundException $e) {
55 4
                $notFound = $e;
56 5
            } catch (MethodNotAllowedException $e) {
57 2
                $badMethod = $e;
58
            }
59 5
        }
60
61 2
        if (null !== $badMethod) {
62
            // If a matcher recognized the URL but not the method, this takes precedence
63 1
            throw $badMethod;
64
        }
65
        // Otherwise, re-throw the last ResourceNotFoundException
66 1
        throw $notFound;
67
    }
68
}
69