Header   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 70
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 12 5
A addHeader() 0 8 2
B isExcluded() 0 19 8
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
 * HTTP request header value maintenance excluder.
18
 */
19
class Header implements ExcluderInterface
20
{
21
    /**
22
     * Request headers.
23
     *
24
     * @var array
25
     */
26
    protected $headers;
27
28
    /**
29
     * Header constructor.
30
     *
31
     * @param array|string $headers
0 ignored issues
show
Documentation introduced by
Should the type for parameter $headers not be array|string|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
     * @param string       $value
0 ignored issues
show
Documentation introduced by
Should the type for parameter $value not be string|null?

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.

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

Loading history...
33
     */
34
    public function __construct($headers = null, $value = null)
35
    {
36
        if ($headers !== null && !is_array($headers)) {
37
            $headers = [$headers => $value];
38
        }
39
40
        if (is_array($headers)) {
41
            foreach ($headers as $headerName => $headerValue) {
42
                $this->addHeader($headerName, $headerValue);
43
            }
44
        }
45
    }
46
47
    /**
48
     * Add header.
49
     *
50
     * @param string $header
51
     * @param string $value
0 ignored issues
show
Documentation introduced by
Should the type for parameter $value not be string|null?

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.

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

Loading history...
52
     *
53
     * @return $this
54
     */
55
    public function addHeader($header, $value = null)
56
    {
57
        if (trim($header) !== '') {
58
            $this->headers[trim($header)] = $value;
59
        }
60
61
        return $this;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     *
67
     * @throws \RuntimeException
68
     */
69
    public function isExcluded(ServerRequestInterface $request)
70
    {
71
        if (!count($this->headers)) {
72
            throw new \RuntimeException('No headers defined in header excluder');
73
        }
74
75
        foreach ($this->headers as $header => $value) {
76
            if ($value === null && $request->hasHeader($header)) {
77
                return true;
78
            } elseif ($value !== null
79
                && (trim($value) === $request->getHeaderLine($header)
80
                    || @preg_match(trim($value), $request->getHeaderLine($header)))
81
            ) {
82
                return true;
83
            }
84
        }
85
86
        return false;
87
    }
88
}
89