1 | <?php |
||
58 | class MergeIntoIndexable |
||
59 | { |
||
60 | /** |
||
61 | * merge their array into our array |
||
62 | * |
||
63 | * @param array &$ours |
||
64 | * the array that we want to merge into |
||
65 | * @param array|Traversable $theirs |
||
66 | * the array that we want to merge from |
||
67 | * @return void |
||
68 | */ |
||
69 | public static function fromArray(&$ours, $theirs) |
||
70 | { |
||
71 | // robustness! |
||
72 | RequireIndexable::check($ours, E4xx_UnsupportedType::class); |
||
73 | RequireTraversable::check($theirs, E4xx_UnsupportedType::class); |
||
74 | |||
75 | // copy from them to us |
||
76 | foreach ($theirs as $key => $value) { |
||
77 | self::mergeKeyIntoArray($ours, $key, $value); |
||
78 | } |
||
79 | |||
80 | // all done |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * merge a single key into our array |
||
85 | * |
||
86 | * @param array &$ours |
||
87 | * the array to merge into |
||
88 | * @param mixed $key |
||
89 | * the array key to merge into |
||
90 | * @param mixed $value |
||
91 | * the data to merge in |
||
92 | * @return void |
||
93 | */ |
||
94 | private static function mergeKeyIntoArray(&$ours, $key, $value) |
||
95 | { |
||
96 | // general case - overwrite because merging isn't possible |
||
97 | if (ShouldOverwrite::intoArray($ours, $key, $value)) { |
||
98 | $ours[$key] = $value; |
||
99 | return; |
||
100 | } |
||
101 | |||
102 | // special case - we are merging into an object |
||
103 | if (is_object($ours[$key])) { |
||
104 | MergeIntoAssignable::from($ours[$key], $value); |
||
105 | return; |
||
106 | } |
||
107 | |||
108 | // at this point, we are merging into an array, using recursion |
||
109 | // for which I am going to hell |
||
110 | MergeIntoIndexable::from($ours[$key], $value); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * merge their object into our array |
||
115 | * |
||
116 | * @param array &$ours |
||
117 | * the array that we want to merge into |
||
118 | * @param object $theirs |
||
119 | * the object that we want to merge from |
||
120 | * @return void |
||
121 | */ |
||
122 | public static function fromObject(&$ours, $theirs) |
||
123 | { |
||
124 | // robustness! |
||
125 | RequireAssignable::check($theirs, E4xx_UnsupportedType::class); |
||
126 | |||
127 | self::fromArray($ours, get_object_vars($theirs)); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * merge their data into our array |
||
132 | * |
||
133 | * @param array &$ours |
||
134 | * the array that we want to merge into |
||
135 | * @param array|object $theirs |
||
136 | * the data that we want to merge from |
||
137 | * @return void |
||
138 | */ |
||
139 | public static function from(&$ours, $theirs) |
||
152 | |||
153 | /** |
||
154 | * merge their data into our array |
||
155 | * |
||
156 | * @deprecated since 2.2.0 |
||
157 | * @codeCoverageIgnore |
||
158 | * |
||
159 | * @param array &$ours |
||
160 | * the array that we want to merge into |
||
161 | * @param array|object $theirs |
||
162 | * the data that we want to merge from |
||
163 | * @return void |
||
164 | */ |
||
165 | public static function fromMixed(&$ours, $theirs) |
||
169 | |||
170 | /** |
||
171 | * merge their data into our array |
||
172 | * |
||
173 | * @param array &$ours |
||
174 | * the array that we want to merge into |
||
175 | * @param array|object $theirs |
||
176 | * the data that we want to merge from |
||
177 | * @return void |
||
178 | */ |
||
179 | public function __invoke(&$ours, $theirs) |
||
183 | } |
||
184 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.