Completed
Pull Request — master (#36)
by David
09:26 queued 03:16
created

RegexRequestMatcher   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 9.33 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 10
c 3
b 1
f 1
lcom 1
cbo 2
dl 7
loc 75
ccs 5
cts 5
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B matches() 7 20 9

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 Http\Message\RequestMatcher;
4
5
use Http\Message\RequestMatcher;
6
use Psr\Http\Message\RequestInterface;
7
8
/**
9
 * A port of the Symfony RequestMatcher for PSR-7.
10
 *
11
 * @author Fabien Potencier <[email protected]>
12
 * @author Joel Wurtz <[email protected]>
13
 */
14
final class RegexRequestMatcher implements RequestMatcher
15
{
16
    /**
17
     * @var string
18
     */
19
    private $path;
20
21
    /**
22 4
     * @var string
23
     */
24 4
    private $host;
25 4
26
    /**
27
     * @var array
28
     */
29
    private $methods = [];
30 2
31
    /**
32 2
     * @var string
33
     */
34
    private $ips = [];
0 ignored issues
show
Unused Code introduced by
The property $ips is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
35
36
    /**
37
     * @var array
38
     */
39
    private $attributes = [];
0 ignored issues
show
Unused Code introduced by
The property $attributes is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
40
41
    /**
42
     * @var string[]
43
     */
44
    private $schemes = [];
45
46
    /**
47
     * The regular expressions used for path or host must be specified without delimiter.
48
     * You do not need to escape the forward slash / to match it.
49
     *
50
     * @param string|null          $path    Regular expression for the path
51
     * @param string|null          $host    Regular expression for the hostname
52
     * @param string|string[]|null $methods Method or list of methods to match
53
     * @param string|string[]|null $schemes Scheme or list of schemes to match (e.g. http or https)
54
     */
55
    public function __construct($path = null, $host = null, $methods = [], $schemes = [])
56
    {
57
        $this->path = $path;
58
        $this->host = $host;
59
        $this->methods = array_map('strtoupper', (array) $methods);
60
        $this->schemes = array_map('strtolower', (array) $schemes);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     *
66
     * @api
67
     */
68
    public function matches(RequestInterface $request)
69
    {
70
        if ($this->schemes && !in_array($request->getUri()->getScheme(), $this->schemes)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->schemes of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
71
            return false;
72
        }
73
74
        if ($this->methods && !in_array($request->getMethod(), $this->methods)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->methods of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
75
            return false;
76
        }
77
78 View Code Duplication
        if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getUri()->getPath()))) {
0 ignored issues
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...
79
            return false;
80
        }
81
82 View Code Duplication
        if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getUri()->getHost())) {
0 ignored issues
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...
83
            return false;
84
        }
85
86
        return true;
87
    }
88
}
89