1 | <?php |
||||||||
2 | |||||||||
3 | /* |
||||||||
4 | * This file is part of the ICanBoogie package. |
||||||||
5 | * |
||||||||
6 | * (c) Olivier Laviale <[email protected]> |
||||||||
7 | * |
||||||||
8 | * For the full copyright and license information, please view the LICENSE |
||||||||
9 | * file that was distributed with this source code. |
||||||||
10 | */ |
||||||||
11 | |||||||||
12 | namespace ICanBoogie\Accessor; |
||||||||
13 | |||||||||
14 | use ReflectionException; |
||||||||
15 | |||||||||
16 | use function array_combine; |
||||||||
17 | use function array_keys; |
||||||||
18 | use function get_object_vars; |
||||||||
19 | |||||||||
20 | /** |
||||||||
21 | * Improves serialization of objects, exporting façade properties and removing properties for |
||||||||
22 | * which lazy getters are defined. |
||||||||
23 | * |
||||||||
24 | * @see HasAccessor |
||||||||
25 | */ |
||||||||
26 | trait SerializableTrait |
||||||||
27 | { |
||||||||
28 | /** |
||||||||
29 | * @throws ReflectionException |
||||||||
30 | */ |
||||||||
31 | public function __sleep() |
||||||||
32 | { |
||||||||
33 | return $this->accessor_sleep(); |
||||||||
34 | } |
||||||||
35 | |||||||||
36 | public function __wakeup() |
||||||||
37 | { |
||||||||
38 | $this->accessor_wakeup(); |
||||||||
39 | } |
||||||||
40 | |||||||||
41 | /** |
||||||||
42 | * The method returns an array of key/key pairs. |
||||||||
43 | * |
||||||||
44 | * Properties for which a lazy getter is defined are discarded. For instance, if the property |
||||||||
45 | * `next` is defined and the class of the instance defines the getter `lazy_get_next()`, the |
||||||||
46 | * property is discarded. |
||||||||
47 | * |
||||||||
48 | * Note that façade properties are also included. |
||||||||
49 | * |
||||||||
50 | * @throws ReflectionException |
||||||||
51 | */ |
||||||||
52 | private function accessor_sleep(): array |
||||||||
53 | { |
||||||||
54 | $properties = array_keys(get_object_vars($this)); |
||||||||
55 | |||||||||
56 | if ($properties) { |
||||||||
0 ignored issues
–
show
|
|||||||||
57 | $properties = array_combine($properties, $properties); |
||||||||
58 | |||||||||
59 | foreach ($properties as $property) { |
||||||||
60 | if ( |
||||||||
61 | $this->has_method(static::accessor_format( |
||||||||
0 ignored issues
–
show
It seems like
has_method() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() The method
accessor_format() does not exist on ICanBoogie\Accessor\SerializableTrait . Did you maybe mean accessor_wakeup() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||||
62 | $property, |
||||||||
63 | HasAccessor::ACCESSOR_TYPE_GETTER, |
||||||||
64 | HasAccessor::ACCESSOR_IS_LAZY |
||||||||
65 | )) |
||||||||
66 | ) { |
||||||||
67 | unset($properties[$property]); |
||||||||
68 | } |
||||||||
69 | } |
||||||||
70 | } |
||||||||
71 | |||||||||
72 | foreach (AccessorReflection::resolve_facade_properties($this) as $name => $property) { |
||||||||
73 | $properties[$name] = "\x00" . $property->class . "\x00" . $name; |
||||||||
74 | } |
||||||||
75 | |||||||||
76 | return $properties; |
||||||||
77 | } |
||||||||
78 | |||||||||
79 | /** |
||||||||
80 | * Unsets null properties for which a lazy getter is defined so that it is called when |
||||||||
81 | * the property is accessed. |
||||||||
82 | */ |
||||||||
83 | private function accessor_wakeup(): void |
||||||||
84 | { |
||||||||
85 | $properties = get_object_vars($this); |
||||||||
86 | |||||||||
87 | foreach ($properties as $property => $value) { |
||||||||
88 | if ( |
||||||||
89 | $this->has_method(static::accessor_format( |
||||||||
90 | $property, |
||||||||
91 | HasAccessor::ACCESSOR_TYPE_GETTER, |
||||||||
92 | HasAccessor::ACCESSOR_IS_LAZY |
||||||||
93 | )) |
||||||||
94 | ) { |
||||||||
95 | unset($this->$property); |
||||||||
96 | } |
||||||||
97 | } |
||||||||
98 | } |
||||||||
99 | } |
||||||||
100 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.