Completed
Pull Request — master (#271)
by
unknown
03:48
created

Requests_Response_Headers   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 18.6 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 16
loc 86
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Headers::offsetGet() 8 8 2
A Headers::offsetSet() 0 13 3
A Headers::getValues() 8 8 2
A Headers::flatten() 0 7 2

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
namespace Rmccue\Requests\Response;
3
4
use Rmccue\Requests as Requests;
5
use Rmccue\Requests\Exception as Exception;
6
use Rmccue\Requests\Utility\FilteredIterator as FilteredIterator;
7
use Rmccue\Requests\Utility\CaseInsensitiveDictionary as CaseInsensitiveDictionary;
8
/**
9
 * Case-insensitive dictionary, suitable for HTTP headers
10
 *
11
 * @package Rmccue\Requests
12
 */
13
14
/**
15
 * Case-insensitive dictionary, suitable for HTTP headers
16
 *
17
 * @package Rmccue\Requests
18
 */
19
class Headers extends CaseInsensitiveDictionary {
20
	/**
21
	 * Get the given header
22
	 *
23
	 * Unlike {@see self::getValues()}, this returns a string. If there are
24
	 * multiple values, it concatenates them with a comma as per RFC2616.
25
	 *
26
	 * Avoid using this where commas may be used unquoted in values, such as
27
	 * Set-Cookie headers.
28
	 *
29
	 * @param string $key
30
	 * @return string Header value
31
	 */
32 View Code Duplication
	public function offsetGet($key) {
33
		$key = strtolower($key);
34
		if (!isset($this->data[$key])) {
35
			return null;
36
		}
37
38
		return $this->flatten($this->data[$key]);
39
	}
40
41
	/**
42
	 * Set the given item
43
	 *
44
	 * @throws Rmccue\Requests\Exception On attempting to use dictionary as list (`invalidset`)
45
	 *
46
	 * @param string $key Item name
47
	 * @param string $value Item value
48
	 */
49
	public function offsetSet($key, $value) {
50
		if ($key === null) {
51
			throw new Exception('Object is a dictionary, not a list', 'invalidset');
52
		}
53
54
		$key = strtolower($key);
55
56
		if (!isset($this->data[$key])) {
57
			$this->data[$key] = array();
58
		}
59
60
		$this->data[$key][] = $value;
61
	}
62
63
	/**
64
	 * Get all values for a given header
65
	 *
66
	 * @param string $key
67
	 * @return array Header values
68
	 */
69 View Code Duplication
	public function getValues($key) {
70
		$key = strtolower($key);
71
		if (!isset($this->data[$key])) {
72
			return null;
73
		}
74
75
		return $this->data[$key];
76
	}
77
78
	/**
79
	 * Flattens a value into a string
80
	 *
81
	 * Converts an array into a string by imploding values with a comma, as per
82
	 * RFC2616's rules for folding headers.
83
	 *
84
	 * @param string|array $value Value to flatten
85
	 * @return string Flattened value
86
	 */
87
	public function flatten($value) {
88
		if (is_array($value)) {
89
			$value = implode(',', $value);
90
		}
91
92
		return $value;
93
	}
94
95
	/**
96
	 * Get an iterator for the data
97
	 *
98
	 * Converts the internal
99
	 * @return ArrayIterator
100
	 */
101
	public function getIterator() {
102
		return new FilteredIterator($this->data, array($this, 'flatten'));
103
	}
104
}
105