Path::__construct()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 6
nc 6
nop 1
1
<?php
2
3
/*
4
 * janitor (http://juliangut.com/janitor).
5
 * Effortless maintenance management.
6
 *
7
 * @license BSD-3-Clause
8
 * @link https://github.com/juliangut/janitor
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
namespace Janitor\Excluder;
13
14
use Psr\Http\Message\ServerRequestInterface;
15
16
/**
17
 * Path based maintenance excluder.
18
 */
19
class Path implements ExcluderInterface
20
{
21
    /**
22
     * List of paths to be excluded.
23
     *
24
     * @var array
25
     */
26
    protected $paths = [];
27
28
    /**
29
     * Path constructor.
30
     *
31
     * @param string|array $paths
0 ignored issues
show
Documentation introduced by
Should the type for parameter $paths not be string|array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
32
     */
33
    public function __construct($paths = null)
34
    {
35
        if ($paths !== null && !is_array($paths)) {
36
            $paths = [$paths];
37
        }
38
39
        if (is_array($paths)) {
40
            foreach ($paths as $path) {
41
                $this->addPath($path);
42
            }
43
        }
44
    }
45
46
    /**
47
     * Add path.
48
     *
49
     * @param string $path
50
     *
51
     * @return $this
52
     */
53
    public function addPath($path)
54
    {
55
        if (trim($path) !== '') {
56
            $this->paths[] = trim($path);
57
        }
58
59
        return $this;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function isExcluded(ServerRequestInterface $request)
66
    {
67
        if (!count($this->paths)) {
68
            throw new \RuntimeException('No paths defined in path excluder');
69
        }
70
71
        $currentPath = $request->getUri()->getPath();
72
73
        foreach ($this->paths as $path) {
74
            if ($path === $currentPath || @preg_match($path, $currentPath) === 1) {
75
                return true;
76
            }
77
        }
78
79
        return false;
80
    }
81
}
82