CookieParser   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 0
loc 44
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D parse() 0 36 9
1
<?php
2
3
/*
4
 * This file is part of the Ivory Http Adapter package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\HttpAdapter\Parser;
13
14
use Ivory\HttpAdapter\Asset\AbstractUninstantiableAsset;
15
16
/**
17
 * @author GeLo <[email protected]>
18
 */
19
class CookieParser extends AbstractUninstantiableAsset
20
{
21
    /**
22
     * @param string $header
23
     *
24
     * @return array
25
     */
26 432
    public static function parse($header)
27
    {
28 432
        if (strpos($header, '=') === false) {
29 72
            $header = '='.$header;
30 56
        }
31
32 432
        list($name, $header) = explode('=', $header, 2);
33
34 432
        if (strpos($header, ';') === false) {
35 144
            $value = $header;
36 144
            $header = null;
37 112
        } else {
38 288
            list($value, $header) = explode(';', $header, 2);
39
        }
40
41 432
        $attributes = [];
42 432
        foreach (array_map('trim', explode(';', $header)) as $pair) {
43 432
            if (empty($pair)) {
44 216
                continue;
45
            }
46
47 216
            if (strpos($pair, '=') === false) {
48 72
                $attributeName = $pair;
49 72
                $attributeValue = null;
50 56
            } else {
51 180
                list($attributeName, $attributeValue) = explode('=', $pair);
52
            }
53
54 216
            $attributes[trim($attributeName)] = $attributeValue ? trim($attributeValue) : true;
55 336
        }
56
57 432
        $name = trim($name);
58 432
        $value = trim($value);
59
60 432
        return [!empty($name) ? $name : null, !empty($value) ? $value : null, $attributes];
61
    }
62
}
63