Completed
Push — feature/fix-somescrutinizer-is... ( 03fa6a...7ccc0d )
by Narcotic
09:35
created

LinkHeader::fromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1.008
Metric Value
dl 0
loc 13
ccs 8
cts 10
cp 0.8
rs 9.4286
cc 1
eloc 7
nc 1
nop 1
crap 1.008
1
<?php
2
/**
3
 * Represents a Link header.
4
 */
5
6
namespace Graviton\RestBundle\HttpFoundation;
7
8
use Symfony\Component\HttpFoundation\Response;
9
10
/**
11
 * Represents a Link header.
12
 *
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class LinkHeader
18
{
19
    /**
20
     * @var LinkHeaderItem[]
21
     */
22
    private $items = array();
23
24
    /**
25
     * Constructor
26
     *
27
     * @param LinkHeaderItem[] $items link header items
28
     */
29 2
    public function __construct(array $items)
30
    {
31 2
        $this->items = $items;
32 2
    }
33
34
    /**
35
     * Builds a LinkHeader instance from a string.
36
     *
37
     * @param string $headerValue value of complete header
38
     *
39
     * @return LinkHeader
40
     */
41 2
    public static function fromString($headerValue)
42
    {
43 2
        return new self(
44 2
            array_map(
45 2
                function ($itemValue) use (&$index) {
46
                    $item = LinkHeaderItem::fromString(trim($itemValue));
47
48
                    return $item;
49 2
                },
50 2
                preg_split('/(".+?"|[^,]+)(?:,|$)/', $headerValue, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE)
51 2
            )
52 2
        );
53
    }
54
55
    /**
56
     * get LinkHeader instance from response
57
     *
58
     * @param \Symfony\Component\HttpFoundation\Response $response response to get header from
59
     *
60
     * @return LinkHeader
61
     */
62 2
    public static function fromResponse(Response $response)
63
    {
64 2
        $header = $response->headers->get('Link');
65 2
        if (is_array($header)) {
66
            implode(',', $header);
67
        }
68
69 2
        return self::fromString($header);
0 ignored issues
show
Bug introduced by
It seems like $header defined by $response->headers->get('Link') on line 64 can also be of type array; however, Graviton\RestBundle\Http...inkHeader::fromString() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
70
    }
71
72
    /**
73
     * get all items
74
     *
75
     * @return LinkHeaderItem[]
76
     */
77
    public function all()
78
    {
79
        return $this->items;
80
    }
81
82
    /**
83
     * add a LinkHeaderItem.
84
     *
85
     * @param LinkHeaderItem $item item to add
86
     *
87
     * @return LinkHeader
88
     */
89 2
    public function add(LinkHeaderItem $item)
90
    {
91 2
        $this->items[] = $item;
92
93 2
        return $this;
94
    }
95
96
    /**
97
     * Cast contents to string.
98
     *
99
     * @return string
100
     */
101 2
    public function __toString()
102
    {
103 2
        return implode(',', $this->items);
104
    }
105
}
106