Completed
Push — master ( 7fad79...963e22 )
by Markus
05:05 queued 02:55
created

ParameterBag   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 81.82%

Importance

Changes 7
Bugs 3 Features 3
Metric Value
wmc 16
c 7
b 3
f 3
lcom 1
cbo 0
dl 0
loc 95
ccs 27
cts 33
cp 0.8182
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setParam() 0 4 1
A hasParams() 0 4 1
A __toString() 0 4 1
A getParam() 0 6 2
B toString() 0 20 5
B escapeParamValue() 0 12 5
1
<?php
2
3
namespace Eluceo\iCal;
4
5
class ParameterBag
6
{
7
    /**
8
     * The params.
9
     *
10
     * @var array
11
     */
12
    protected $params;
13
14 11
    public function __construct($params = array())
15
    {
16 11
        $this->params = $params;
17 11
    }
18
19
    /**
20
     * @param string $name
21
     * @param mixed  $value
22
     */
23 1
    public function setParam($name, $value)
24
    {
25 1
        $this->params[$name] = $value;
26 1
    }
27
28
    /**
29
     * @param $name
30
     */
31
    public function getParam($name)
32
    {
33
        if (array_key_exists($name, $this->params)) {
34
            return $this->params[$name];
35
        }
36
    }
37
38
    /**
39
     * Checks if there are any params.
40
     *
41
     * @return bool
42
     */
43 8
    public function hasParams()
44
    {
45 8
        return count($this->params) > 0;
46
    }
47
48
    /**
49
     * @return string
50
     */
51 4
    public function toString()
52
    {
53 4
        $line = '';
54 4
        foreach ($this->params as $param => $paramValues) {
55 4
            if (!is_array($paramValues)) {
56 4
                $paramValues = array($paramValues);
57
            }
58 4
            foreach ($paramValues as $k => $v) {
59 4
                $paramValues[$k] = $this->escapeParamValue($v);
60
            }
61
62 4
            if ('' != $line) {
63 1
                $line .= ';';
64
            }
65
66 4
            $line .= $param . '=' . implode(',', $paramValues);
67
        }
68
69 4
        return $line;
70
    }
71
72
    /**
73
     * Returns an escaped string for a param value.
74
     *
75
     * @param string $value
76
     *
77
     * @return string
78
     */
79 5
    public function escapeParamValue($value)
80
    {
81 5
        $count = 0;
82 5
        $value = str_replace('\\', '\\\\', $value);
83 5
        $value = str_replace('"', '\"', $value, $count);
84 5
        $value = str_replace("\n", '\\n', $value);
85 5
        if (false !== strpos($value, ';') || false !== strpos($value, ',') || false !== strpos($value, ':') || $count) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $count of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
86 2
            $value = '"' . $value . '"';
87
        }
88
89 5
        return $value;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function __toString()
96
    {
97
        return $this->toString();
98
    }
99
}
100