DeprecationWrapper::__set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Elgg;
3
/**
4
 * Wrap an object and display warnings whenever the object's variables are
5
 * accessed or a method is used. It can also be used to wrap a string.
6
 *
7
 * Note that the wrapper will not share the type of the wrapped object and will
8
 * fail type hints, instanceof, etc.
9
 *
10
 * This was introduced for deprecating passing particular variabled to views
11
 * automatically in elgg_view(). It also used to wrap the deprecated global $SESSION.
12
 * It can be removed once that use is no longer required.
13
 *
14
 * Wraps:
15
 *  url string in ViewsService
16
 *  config object in ViewsService
17
 *  user object in ViewsService
18
 *  session object in session lib
19
 *  config object in ElggPlugin::includeFile
20
 *
21
 * @access private
22
 * 
23
 * @package Elgg.Core
24
 */
25
class DeprecationWrapper implements \ArrayAccess {
26
	/** @var object */
27
	protected $object;
28
29
	/** @var string */
30
	protected $string;
31
32
	/** @var string */
33
	protected $message;
34
35
	/** @var string */
36
	protected $version;
37
38
	/** @var callable */
39
	protected $reporter;
40
41
	/**
42
	 * Create the wrapper
43
	 * 
44
	 * @param mixed    $object   The object or string to wrap
45
	 * @param string   $message  The deprecation message to display when used
46
	 * @param string   $version  The Elgg version this was deprecated
47
	 * @param callable $reporter function called to report deprecation
48
	 */
49 9
	public function __construct($object, $message, $version, $reporter = 'elgg_deprecated_notice') {
50 9
		if (is_object($object)) {
51 8
			$this->object = $object;
52 8
		} else {
53 6
			$this->string = $object;
54
		}
55 9
		$this->message = $message;
56 9
		$this->version = $version;
57 9
		$this->reporter = $reporter;
58 9
	}
59
60
	/**
61
	 * Get a property on the object
62
	 * 
63
	 * @param string $name Property name
64
	 * @return mixed
65
	 */
66 1
	public function __get($name) {
67 1
		$this->displayWarning();
68 1
		return $this->object->$name;
69
	}
70
71
	/**
72
	 * Set a property on the object
73
	 * 
74
	 * @param string $name  Property name
75
	 * @param mixed  $value Property value
76
	 * @return void
77
	 */
78
	public function __set($name, $value) {
79
		$this->displayWarning();
80
		$this->object->$name = $value;
81
	}
82
83
	/**
84
	 * Call a method on the object
85
	 * 
86
	 * @param string $name      Method name
87
	 * @param array  $arguments Method arguments
88
	 * @return mixed
89
	 */
90 1
	public function __call($name, $arguments) {
91 1
		$this->displayWarning();
92 1
		return call_user_func_array(array($this->object, $name), $arguments);
93
	}
94
95
	/**
96
	 * Get the object as string
97
	 * 
98
	 * @return string
99
	 */
100 2
	public function __toString() {
101 2
		$this->displayWarning();
102 2
		if (isset($this->string)) {
103 1
			return $this->string;
104
		} else {
105 1
			return (string) $this->object;
106
		}
107
	}
108
109
	/**
110
	 * Display a warning
111
	 * 
112
	 * @return void
113
	 */
114 4
	protected function displayWarning() {
115
		// display 3 levels in the function stack to get back to original use
116
		// 1 for __get/__call/__toString()
117
		// 1 for displayWarning()
118
		// 1 for call_user_func()
119 4
		call_user_func($this->reporter, $this->message, $this->version, 3);
120 4
	}
121
122
	/**
123
	 * Array access interface
124
	 *
125
	 * @see \ArrayAccess::offsetSet()
126
	 *
127
	 * @param mixed $key   Name
128
	 * @param mixed $value Value
129
	 *
130
	 * @return void
131
	 */
132 2
	public function offsetSet($key, $value) {
133 2
		$this->displayWarning();
134 2
		if (is_object($this->object) && !$this->object instanceof \ArrayAccess) {
135 1
			$this->object->$key = $value;
136 1
		} else {
137 1
			if ($key === null) {
138
				// Yes this is necessary. Otherwise $key will be interpreted as empty string
139 1
				$this->object[] = $value;
140 1
			} else {
141 1
				$this->object[$key] = $value;
142
			}
143
		}
144 2
	}
145
146
	/**
147
	 * Array access interface
148
	 *
149
	 * @see \ArrayAccess::offsetGet()
150
	 *
151
	 * @param mixed $key Name
152
	 *
153
	 * @return mixed
154
	 */
155 2 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...
156 2
		$this->displayWarning();
157 2
		if (is_object($this->object) && !$this->object instanceof \ArrayAccess) {
158 1
			return $this->object->$key;
159
		} else {
160 1
			return $this->object[$key];
161
		}
162
	}
163
164
	/**
165
	 * Array access interface
166
	 *
167
	 * @see \ArrayAccess::offsetUnset()
168
	 *
169
	 * @param mixed $key Name
170
	 *
171
	 * @return void
172
	 */
173 1 View Code Duplication
	public function offsetUnset($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...
174 1
		$this->displayWarning();
175 1
		if (is_object($this->object) && !$this->object instanceof \ArrayAccess) {
176
			unset($this->object->$key);
177
		} else {
178 1
			unset($this->object[$key]);
179
		}
180 1
	}
181
182
	/**
183
	 * Array access interface
184
	 *
185
	 * @see \ArrayAccess::offsetExists()
186
	 *
187
	 * @param mixed $offset Offset
188
	 *
189
	 * @return bool
190
	 */
191 View Code Duplication
	public function offsetExists($offset) {
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...
192
		$this->displayWarning();
193
		if (is_object($this->object) && !$this->object instanceof \ArrayAccess) {
194
			return isset($this->object->$offset);
195
		} else {
196
			return array_key_exists($offset, $this->object);
197
		}
198
	}
199
}
200
201