Requests_Response_Headers   A
last analyzed

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

5 Methods

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

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
 * Case-insensitive dictionary, suitable for HTTP headers
4
 *
5
 * @package Requests
6
 */
7
8
/**
9
 * Case-insensitive dictionary, suitable for HTTP headers
10
 *
11
 * @package Requests
12
 */
13
class Requests_Response_Headers extends Requests_Utility_CaseInsensitiveDictionary {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
14
	/**
15
	 * Get the given header
16
	 *
17
	 * Unlike {@see self::getValues()}, this returns a string. If there are
18
	 * multiple values, it concatenates them with a comma as per RFC2616.
19
	 *
20
	 * Avoid using this where commas may be used unquoted in values, such as
21
	 * Set-Cookie headers.
22
	 *
23
	 * @param string $key
24
	 * @return string Header value
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
25
	 */
26 View Code Duplication
	public function offsetGet($key) {
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...
27
		$key = strtolower($key);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $key. This often makes code more readable.
Loading history...
28
		if (!isset($this->data[$key])) {
29
			return null;
30
		}
31
32
		return $this->flatten($this->data[$key]);
33
	}
34
35
	/**
36
	 * Set the given item
37
	 *
38
	 * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`)
39
	 *
40
	 * @param string $key Item name
41
	 * @param string $value Item value
42
	 */
43
	public function offsetSet($key, $value) {
44
		if ($key === null) {
45
			throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
46
		}
47
48
		$key = strtolower($key);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $key. This often makes code more readable.
Loading history...
49
50
		if (!isset($this->data[$key])) {
51
			$this->data[$key] = array();
52
		}
53
54
		$this->data[$key][] = $value;
55
	}
56
57
	/**
58
	 * Get all values for a given header
59
	 *
60
	 * @param string $key
61
	 * @return array Header values
62
	 */
63 View Code Duplication
	public function getValues($key) {
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...
64
		$key = strtolower($key);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $key. This often makes code more readable.
Loading history...
65
		if (!isset($this->data[$key])) {
66
			return null;
67
		}
68
69
		return $this->data[$key];
70
	}
71
72
	/**
73
	 * Flattens a value into a string
74
	 *
75
	 * Converts an array into a string by imploding values with a comma, as per
76
	 * RFC2616's rules for folding headers.
77
	 *
78
	 * @param string|array $value Value to flatten
79
	 * @return string Flattened value
80
	 */
81
	public function flatten($value) {
82
		if (is_array($value)) {
83
			$value = implode(',', $value);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $value. This often makes code more readable.
Loading history...
84
		}
85
86
		return $value;
87
	}
88
89
	/**
90
	 * Get an iterator for the data
91
	 *
92
	 * Converts the internal
93
	 * @return ArrayIterator
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use Requests_Utility_FilteredIterator.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
94
	 */
95
	public function getIterator() {
96
		return new Requests_Utility_FilteredIterator($this->data, array($this, 'flatten'));
97
	}
98
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
99