Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
GhostFieldAccessTrait | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
4.10 | |
0.00% |
0 / 1 |
getGhostFieldValue | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
4.10 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace Wikimedia\Reflection; |
22 | |
23 | /** |
24 | * Trait for accessing "ghost fields". |
25 | * |
26 | * Ghost fields are fields that have been created in an object instance |
27 | * by PHP's unserialize() mechanism, though they no longer exist |
28 | * in the current version of the corresponding class. |
29 | * |
30 | * Accessing non-public ghost fields offers some challenges due to |
31 | * how they are handled by PHP internally. |
32 | * |
33 | * @see https://www.php.net/manual/en/language.types.array.php#language.types.array.casting |
34 | * @since 1.36 |
35 | */ |
36 | trait GhostFieldAccessTrait { |
37 | |
38 | /** |
39 | * Get the value of the ghost field named $name, |
40 | * or null if the field does not exist. |
41 | * |
42 | * @param string $name |
43 | * @return mixed|null |
44 | */ |
45 | private function getGhostFieldValue( string $name ) { |
46 | if ( isset( $this->$name ) ) { |
47 | return $this->$name; |
48 | } |
49 | |
50 | $data = (array)$this; |
51 | |
52 | // Protected variables have a '*' prepended to the variable name. |
53 | // These prepended values have null bytes on either side. |
54 | $protectedName = "\x00*\x00{$name}"; |
55 | if ( isset( $data[$protectedName] ) ) { |
56 | return $data[$protectedName]; |
57 | } |
58 | |
59 | // Private variables have the class name prepended to the variable name. |
60 | // These prepended values have null bytes on either side. |
61 | $thisClass = get_class( $this ); |
62 | $privateName = "\x00{$thisClass}\x00{$name}"; |
63 | if ( isset( $data[$privateName] ) ) { |
64 | return $data[$privateName]; |
65 | } |
66 | |
67 | return null; |
68 | } |
69 | } |