1 | <?php |
||
20 | class IndexedObjectStorage extends \SplObjectStorage { |
||
21 | |||
22 | protected $index = array(); |
||
23 | |||
24 | /** |
||
25 | * @param mixed $index |
||
26 | * |
||
27 | * @return bool |
||
28 | */ |
||
29 | 1 | public function indexIsSet($index) { |
|
30 | 1 | return array_key_exists($index, $this->index); |
|
31 | } |
||
32 | |||
33 | /** |
||
34 | * @param mixed $index |
||
35 | * |
||
36 | * @return mixed|null |
||
37 | */ |
||
38 | 1 | public function indexGetObject($index) { |
|
41 | |||
42 | 1 | public function offsetSet($object, $data = null) { |
|
45 | |||
46 | 1 | public function offsetUnset($object) { |
|
50 | |||
51 | |||
52 | /** |
||
53 | * Unassign index from object |
||
54 | * |
||
55 | * @param object $object |
||
56 | */ |
||
57 | 1 | protected function indexUnset($object) { |
|
62 | |||
63 | /** |
||
64 | * Function called when index already exists |
||
65 | * |
||
66 | * Can be used by child object |
||
67 | * |
||
68 | * @param $object |
||
69 | * @param $data |
||
70 | * |
||
71 | * @throws \Exception |
||
72 | */ |
||
73 | 1 | protected function onObjectIndexDuplicated($object, $data) { |
|
74 | 1 | throw new \Exception('Duplicate index [' . $data . '] in ' . __CLASS__); |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Function called when index is empty |
||
79 | * |
||
80 | * To use with child object |
||
81 | * |
||
82 | * @param $object |
||
83 | * |
||
84 | * @return bool |
||
85 | */ |
||
86 | 1 | protected function onObjectIndexEmpty($object) { |
|
87 | // Do something if index is empty |
||
88 | 1 | return true; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Function to handle assigning keys |
||
93 | * |
||
94 | * Primary for dealing with duplicates - child can overwrite function to handle duplicates by itself |
||
95 | * |
||
96 | * @param object $object |
||
97 | * @param mixed|null $data - null means remove index |
||
98 | * |
||
99 | * @return bool |
||
100 | */ |
||
101 | // Set indexes for existing objects |
||
102 | 2 | protected function indexSet($object, $data = null) { |
|
103 | // When calling indexSet - $object already SHOULD not have index associated in this concrete implementation |
||
104 | |||
105 | // Checking if index free. NULL index is always free |
||
106 | 2 | if (isset($this->index[$data])) { |
|
107 | 1 | return $this->onObjectIndexDuplicated($object, $data); |
|
108 | 2 | } elseif ($data === null) { |
|
109 | 1 | return $this->onObjectIndexEmpty($object); |
|
110 | } else { |
||
111 | // Assigning index |
||
112 | 2 | $this->index[$data] = $object; |
|
113 | 2 | return true; |
|
114 | } |
||
115 | } |
||
116 | |||
117 | 1 | public function attach($object, $data = null) { |
|
127 | |||
128 | 1 | public function detach($object) { |
|
132 | |||
133 | 1 | public function setInfo($data) { |
|
143 | |||
144 | |||
145 | /** |
||
146 | * Rebuild index |
||
147 | */ |
||
148 | 2 | protected function indexRebuild() { |
|
156 | |||
157 | 1 | public function addAll($storage) { |
|
163 | |||
164 | 1 | public function removeAll($storage) { |
|
168 | |||
169 | 1 | public function removeAllExcept($storage) { |
|
173 | |||
174 | 1 | public function unserialize($serialized) { |
|
180 | |||
181 | } |
||
182 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.