Requests_Utility_CaseInsensitiveDictionary   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 8.99 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 8
loc 89
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A offsetExists() 0 4 1
A offsetGet() 8 8 2
A offsetSet() 0 8 2
A offsetUnset() 0 3 1
A getIterator() 0 3 1
A getAll() 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
 * @subpackage Utilities
7
 */
8
9
/**
10
 * Case-insensitive dictionary, suitable for HTTP headers
11
 *
12
 * @package Requests
13
 * @subpackage Utilities
14
 */
15
class Requests_Utility_CaseInsensitiveDictionary implements ArrayAccess, IteratorAggregate {
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...
16
	/**
17
	 * Actual item data
18
	 *
19
	 * @var array
20
	 */
21
	protected $data = array();
22
23
	/**
24
	 * Creates a case insensitive dictionary.
25
	 *
26
	 * @param array $data Dictionary/map to convert to case-insensitive
27
	 */
28
	public function __construct(array $data = array()) {
29
		foreach ($data as $key => $value) {
30
			$this->offsetSet($key, $value);
31
		}
32
	}
33
34
	/**
35
	 * Check if the given item exists
36
	 *
37
	 * @param string $key Item key
38
	 * @return boolean Does the item exist?
39
	 */
40
	public function offsetExists($key) {
41
		$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...
42
		return isset($this->data[$key]);
43
	}
44
45
	/**
46
	 * Get the value for the item
47
	 *
48
	 * @param string $key Item key
49
	 * @return string Item value
50
	 */
51 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...
52
		$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...
53
		if (!isset($this->data[$key])) {
54
			return null;
55
		}
56
57
		return $this->data[$key];
58
	}
59
60
	/**
61
	 * Set the given item
62
	 *
63
	 * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`)
64
	 *
65
	 * @param string $key Item name
66
	 * @param string $value Item value
67
	 */
68
	public function offsetSet($key, $value) {
69
		if ($key === null) {
70
			throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
71
		}
72
73
		$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...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 14 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
74
		$this->data[$key] = $value;
75
	}
76
77
	/**
78
	 * Unset the given header
79
	 *
80
	 * @param string $key
81
	 */
82
	public function offsetUnset($key) {
83
		unset($this->data[strtolower($key)]);
84
	}
85
86
	/**
87
	 * Get an iterator for the data
88
	 *
89
	 * @return ArrayIterator
90
	 */
91
	public function getIterator() {
92
		return new ArrayIterator($this->data);
93
	}
94
95
	/**
96
	 * Get the headers as an array
97
	 *
98
	 * @return array Header data
99
	 */
100
	public function getAll() {
101
		return $this->data;
102
	}
103
}
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...
104