Skip to content

Commit 205d5b3

Browse files
committed
efficiency improvments and using "property_exists" for existence determination over "isset" on objects
1 parent 24da7f4 commit 205d5b3

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

src/ObjectMerge.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ class ObjectMerge
5151
self::DOUBLE_T
5252
);
5353

54-
/** @var mixed */
55-
private static $_leftContext;
56-
/** @var mixed */
57-
private static $_rightContext;
58-
5954
/**
6055
* @param stdClass ...$objects
6156
* @return stdClass|null
@@ -159,7 +154,7 @@ private static function compareTypes($left, $right)
159154
* @param bool $recurse
160155
* @param int $opts
161156
* @param callable $cb
162-
* @param mixed $key
157+
* @param string|int $key
163158
* @param array $leftValue
164159
* @param array $rightValue
165160
* @return array
@@ -188,6 +183,7 @@ private static function mergeArrayValues($recurse, $opts, $cb, $key, array $left
188183
* @param bool $recurse
189184
* @param int $opts
190185
* @param callable $cb
186+
* @param string|int $key
191187
* @param stdClass $leftValue
192188
* @param stdClass $rightValue
193189
* @return stdClass
@@ -196,8 +192,8 @@ private static function mergeObjectValues($recurse, $opts, $cb, $key, stdClass $
196192
{
197193
$out = new stdClass();
198194
foreach (array_merge(get_object_vars($leftValue), get_object_vars($rightValue)) as $k => $v) {
199-
$leftDefined = isset($leftValue->{$k});
200-
$rightDefined = isset($rightValue->{$k});
195+
$leftDefined = property_exists($leftValue, $k);
196+
$rightDefined = property_exists($rightValue, $k);
201197
$out->{$k} = self::mergeValues(
202198
$recurse,
203199
$opts,
@@ -263,9 +259,6 @@ private static function mergeValues($recurse, $opts, $cb, $key, $leftValue, $rig
263259
return $rightValue;
264260
}
265261

266-
self::$_leftContext = $leftValue;
267-
self::$_rightContext = $rightValue;
268-
269262
if (self::ARRAY_T === $leftType) {
270263
return self::mergeArrayValues($recurse, $opts, $cb, $key, $leftValue, $rightValue);
271264
}
@@ -294,18 +287,13 @@ private static function doMerge($recurse, $opts, $cb, array $objects)
294287
}
295288

296289
if (null === $root) {
297-
$root = self::$_leftContext = clone $object;
290+
$root = clone $object;
298291
continue;
299292
}
300293

301-
self::$_rightContext = $object;
302-
303294
$root = self::mergeObjectValues($recurse, $opts, $cb, null, $root, $object);
304295
}
305296

306-
self::$_leftContext = null;
307-
self::$_rightContext = null;
308-
309297
return $root;
310298
}
311299
}

0 commit comments

Comments
 (0)