Completed
Pull Request — master (#46)
by
unknown
05:21
created

RouteMatchingPolicy::match()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
1
<?php
2
3
namespace Coduo\TuTu\Request;
4
5
use Coduo\TuTu\Config\Element;
6
use Symfony\Component\HttpFoundation\Request;
7
8
class RouteMatchingPolicy implements MatchingPolicy
9
{
10
    /**
11
     * @param Request $request
12
     * @param Element $config
13
     * @return bool
14
     */
15
    public function match(Request $request, Element $config)
16
    {
17
        $pathPattern = $config->getRequest()->getPath();
18
        preg_match_all('#\{\w+\}#', $config->getRequest()->getPath(), $placeholders, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
19
20
        foreach ($placeholders as $placeholderMatch) {
0 ignored issues
show
Bug introduced by
The expression $placeholders of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
21
            $placeholder = $placeholderMatch[0][0];
22
            $pathPattern = str_replace($placeholder, '__PLACEHOLDER__', $pathPattern);
23
        }
24
25
        $pathPattern = '/^' . str_replace('__PLACEHOLDER__', '([^\/]*)', preg_quote($pathPattern, '/')) . '$/i';
26
27
        return 0 !== preg_match($pathPattern, $request->getPathInfo());
28
    }
29
}
30