Requests_Cookie_Jar   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 161
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 5

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A normalize_cookie() 0 7 2
A normalizeCookie() 0 3 1
A offsetExists() 0 3 1
A offsetGet() 0 7 2
A offsetSet() 0 7 2
A offsetUnset() 0 3 1
A getIterator() 0 3 1
A register() 0 4 1
B before_request() 0 23 6
A before_redirect_check() 0 10 2
1
<?php
2
/**
3
 * Cookie holder object
4
 *
5
 * @package Requests
6
 * @subpackage Cookies
7
 */
8
9
/**
10
 * Cookie holder object
11
 *
12
 * @package Requests
13
 * @subpackage Cookies
14
 */
15
class Requests_Cookie_Jar 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 $cookies = array();
22
23
	/**
24
	 * Create a new jar
25
	 *
26
	 * @param array $cookies Existing cookie values
27
	 */
28
	public function __construct($cookies = array()) {
29
		$this->cookies = $cookies;
30
	}
31
32
	/**
33
	 * Normalise cookie data into a Requests_Cookie
34
	 *
35
	 * @param string|Requests_Cookie $cookie
36
	 * @return Requests_Cookie
37
	 */
38
	public function normalize_cookie($cookie, $key = null) {
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
39
		if ($cookie instanceof Requests_Cookie) {
40
			return $cookie;
41
		}
42
43
		return Requests_Cookie::parse($cookie, $key);
0 ignored issues
show
Documentation introduced by
$cookie is of type string, but the function expects a object<Cookie>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
	}
45
46
	/**
47
	 * Normalise cookie data into a Requests_Cookie
48
	 *
49
	 * @codeCoverageIgnore
50
	 * @deprecated Use {@see Requests_Cookie_Jar::normalize_cookie}
51
	 * @return Requests_Cookie
52
	 */
53
	public function normalizeCookie($cookie, $key = null) {
54
		return $this->normalize_cookie($cookie, $key);
55
	}
56
57
	/**
58
	 * Check if the given item exists
59
	 *
60
	 * @param string $key Item key
61
	 * @return boolean Does the item exist?
62
	 */
63
	public function offsetExists($key) {
64
		return isset($this->cookies[$key]);
65
	}
66
67
	/**
68
	 * Get the value for the item
69
	 *
70
	 * @param string $key Item key
71
	 * @return string Item value
72
	 */
73
	public function offsetGet($key) {
74
		if (!isset($this->cookies[$key])) {
75
			return null;
76
		}
77
78
		return $this->cookies[$key];
79
	}
80
81
	/**
82
	 * Set the given item
83
	 *
84
	 * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`)
85
	 *
86
	 * @param string $key Item name
87
	 * @param string $value Item value
88
	 */
89
	public function offsetSet($key, $value) {
90
		if ($key === null) {
91
			throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
92
		}
93
94
		$this->cookies[$key] = $value;
95
	}
96
97
	/**
98
	 * Unset the given header
99
	 *
100
	 * @param string $key
101
	 */
102
	public function offsetUnset($key) {
103
		unset($this->cookies[$key]);
104
	}
105
106
	/**
107
	 * Get an iterator for the data
108
	 *
109
	 * @return ArrayIterator
110
	 */
111
	public function getIterator() {
112
		return new ArrayIterator($this->cookies);
113
	}
114
115
	/**
116
	 * Register the cookie handler with the request's hooking system
117
	 *
118
	 * @param Requests_Hooker $hooks Hooking system
119
	 */
120
	public function register(Requests_Hooker $hooks) {
121
		$hooks->register('requests.before_request', array($this, 'before_request'));
122
		$hooks->register('requests.before_redirect_check', array($this, 'before_redirect_check'));
123
	}
124
125
	/**
126
	 * Add Cookie header to a request if we have any
127
	 *
128
	 * As per RFC 6265, cookies are separated by '; '
129
	 *
130
	 * @param string $url
131
	 * @param array $headers
132
	 * @param array $data
133
	 * @param string $type
134
	 * @param array $options
135
	 */
136
	public function before_request($url, &$headers, &$data, &$type, &$options) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
137
		if (!$url instanceof Requests_IRI) {
138
			$url = new Requests_IRI($url);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $url. This often makes code more readable.
Loading history...
139
		}
140
141
		if (!empty($this->cookies)) {
142
			$cookies = array();
143
			foreach ($this->cookies as $key => $cookie) {
144
				$cookie = $this->normalize_cookie($cookie, $key);
145
146
				// Skip expired cookies
147
				if ($cookie->is_expired()) {
148
					continue;
149
				}
150
151
				if ($cookie->domain_matches($url->host)) {
152
					$cookies[] = $cookie->format_for_header();
153
				}
154
			}
155
156
			$headers['Cookie'] = implode('; ', $cookies);
157
		}
158
	}
159
160
	/**
161
	 * Parse all cookies from a response and attach them to the response
162
	 *
163
	 * @var Requests_Response $response
164
	 */
165
	public function before_redirect_check(Requests_Response &$return) {
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
166
		$url = $return->url;
167
		if (!$url instanceof Requests_IRI) {
168
			$url = new Requests_IRI($url);
169
		}
170
171
		$cookies = Requests_Cookie::parse_from_headers($return->headers, $url);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 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...
172
		$this->cookies = array_merge($this->cookies, $cookies);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
173
		$return->cookies = $this;
174
	}
175
}