1 | <?php |
||
16 | class JailedDocument implements \ArrayAccess |
||
17 | { |
||
18 | /** |
||
19 | * @var string[] |
||
20 | */ |
||
21 | private $whiteListFunctions; |
||
22 | |||
23 | /** |
||
24 | * @var string[] |
||
25 | */ |
||
26 | private $jailedFunctions; |
||
27 | |||
28 | /** |
||
29 | * @var JailedDocumentInterface |
||
30 | */ |
||
31 | private $object; |
||
32 | |||
33 | /** |
||
34 | * JailObject constructor. |
||
35 | * |
||
36 | * @param JailedDocumentInterface $object The object that will be jailed |
||
37 | * @param array $whiteListFunctions A list of function names that can be called |
||
38 | * @param array $jailedFunctions |
||
39 | */ |
||
40 | 49 | public function __construct(&$object, array $whiteListFunctions, array $jailedFunctions = array()) |
|
41 | { |
||
42 | 49 | if (!($object instanceof JailedDocumentInterface) && !($object instanceof \ArrayAccess)) |
|
43 | 49 | { |
|
44 | throw new \InvalidArgumentException('Must implement the ArrayAccess and Jailable interfaces'); |
||
45 | } |
||
46 | |||
47 | 49 | $this->object = &$object; |
|
|
|||
48 | 49 | $this->whiteListFunctions = $whiteListFunctions; |
|
49 | 49 | $this->jailedFunctions = $jailedFunctions; |
|
50 | 49 | } |
|
51 | |||
52 | 10 | public function __call($name, $arguments) |
|
53 | { |
||
54 | // White listed functions will always be getter functions, so somehow get the name of a possible getter function |
||
55 | // name. |
||
56 | 10 | $lcName = lcfirst($name); |
|
57 | 10 | $getFxnCall = ($lcName[0] === 'g' && strpos($lcName, 'get') === 0) ? $lcName : sprintf('get%s', ucfirst($name)); |
|
58 | |||
59 | // Check if our function call is a jailed call, meaning the function should be mapped to special "jailed" |
||
60 | // jailed version of the function call. |
||
61 | 10 | if (array_key_exists($getFxnCall, $this->jailedFunctions)) |
|
62 | 10 | { |
|
63 | 3 | return call_user_func_array(array($this->object, $this->jailedFunctions[$getFxnCall]), $arguments); |
|
64 | } |
||
65 | |||
66 | // Otherwise, test to see if the function call is in our white list and call it |
||
67 | 7 | if (in_array($getFxnCall, $this->whiteListFunctions)) |
|
68 | 7 | { |
|
69 | 6 | return call_user_func_array(array($this->object, $getFxnCall), $arguments); |
|
70 | } |
||
71 | |||
72 | 1 | throw new \BadMethodCallException(); |
|
73 | } |
||
74 | |||
75 | 4 | public function coreInstanceOf($class) |
|
79 | |||
80 | // |
||
81 | // ArrayAccess Implementation |
||
82 | // |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 22 | public function offsetExists($offset) |
|
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | 30 | public function offsetGet($offset) |
|
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function offsetSet($offset, $value) |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function offsetUnset($offset) |
||
115 | } |
||
116 |
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
will produce issues in the first and second line, while this second example
will produce no issues.