Passed
Push — master ( c65e58...1194b0 )
by Oss
02:24
created

CookieList   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 34.78 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
dl 16
loc 46
ccs 25
cts 25
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCookieParams() 0 16 4
A initList() 15 15 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @category    Brownie/HttpClient
4
 * @author      Brownie <[email protected]>
5
 * @license     http://www.gnu.org/copyleft/lesser.html
6
 */
7
8
namespace Brownie\HttpClient\Cookie;
9
10
use Brownie\HttpClient\HeadList;
11
12
class CookieList extends HeadList
13
{
14
15
    /**
16
     * Initializing the cookies container.
17
     */
18 3 View Code Duplication
    protected function initList()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
    {
20 3
        $cookieList = array();
21 3
        foreach (explode("\r\n", trim($this->getInitData())) as $headerLine) {
22 3
            if (empty($headerLine)) {
23 2
                $cookieList = array();
24 2
                continue;
25
            }
26 2
            if (!$this->isHeaderSetCookie($headerLine)) {
27 2
                continue;
28
            }
29 2
            $cookieParams = $this->getCookieParams($headerLine);
30 2
            $cookieList[$cookieParams['name']] = new Cookie($cookieParams);
31
        }
32 3
        $this->setList($cookieList);
33 3
    }
34
35
    /**
36
     * Splits HTTP header on the parameters.
37
     *
38
     * @param string    $headerLine     HTTP header line Set-Cookie.
39
     *
40
     * @return array
41
     */
42 2
    private function getCookieParams($headerLine)
43
    {
44 2
        $cookieParamsStrings = explode(':', trim($headerLine), 2);
45 2
        $params = array();
46 2
        foreach (explode(';', trim($cookieParamsStrings[1])) as $index => $paramString) {
47 2
            $pairParam = explode('=', trim($paramString), 2);
48 2
            $name = trim($pairParam[0]);
49 2
            $value = (!empty($pairParam[1]) ? trim($pairParam[1]) : true);
50 2
            if (0 == $index) {
51 2
                $params['name'] = $name;
52 2
                $params['value'] = $value;
53 2
                continue;
54
            }
55 1
            $params[$name] = $value;
56
        }
57 2
        return $params;
58
    }
59
}
60