RequestMatcher::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Http\Message\RequestMatcher;
4
5
use Http\Message\RequestMatcher as RequestMatcherInterface;
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 RequestMatcher implements RequestMatcherInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    private $path;
20
21
    /**
22
     * @var string
23
     */
24
    private $host;
25
26
    /**
27
     * @var array
28
     */
29
    private $methods = [];
30
31
    /**
32
     * @var string[]
33
     */
34
    private $schemes = [];
35
36
    /**
37
     * The regular expressions used for path or host must be specified without delimiter.
38
     * You do not need to escape the forward slash / to match it.
39
     *
40
     * @param string|null          $path    Regular expression for the path
41
     * @param string|null          $host    Regular expression for the hostname
42
     * @param string|string[]|null $methods Method or list of methods to match
43
     * @param string|string[]|null $schemes Scheme or list of schemes to match (e.g. http or https)
44
     */
45 10
    public function __construct($path = null, $host = null, $methods = [], $schemes = [])
46
    {
47 10
        $this->path = $path;
48 10
        $this->host = $host;
49 10
        $this->methods = array_map('strtoupper', (array) $methods);
50 10
        $this->schemes = array_map('strtolower', (array) $schemes);
51 10
    }
52
53
    /**
54
     * {@inheritdoc}
55
     *
56
     * @api
57
     */
58 8
    public function matches(RequestInterface $request)
59
    {
60 8
        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...
61 1
            return false;
62
        }
63
64 7
        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...
65 1
            return false;
66
        }
67
68 6
        if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getUri()->getPath()))) {
69 1
            return false;
70
        }
71
72 5
        if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getUri()->getHost())) {
73 1
            return false;
74
        }
75
76 4
        return true;
77
    }
78
}
79