Skip to content

Commit ef19a38

Browse files
committed
Move method for better diff
1 parent 885390a commit ef19a38

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

src/ArrayHelper.php

+41-41
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,47 @@ public static function getValue(
233233
return self::getRootValue($array, $key, $default);
234234
}
235235

236+
/**
237+
* @param mixed $array Array or object to extract value from, otherwise method will return $default.
238+
* @param float|int|string $key Key name of the array element, object property name or object method like `getValue()`.
239+
* @param mixed $default The default value to be returned if the specified array key does not exist. Not used when
240+
* getting value from an object.
241+
*
242+
* @return mixed The value of the element if found, default value otherwise.
243+
*/
244+
private static function getRootValue(mixed $array, float|int|string $key, mixed $default): mixed
245+
{
246+
if (is_array($array)) {
247+
$key = self::normalizeArrayKey($key);
248+
return array_key_exists($key, $array) ? $array[$key] : $default;
249+
}
250+
251+
if (is_object($array)) {
252+
$key = (string) $key;
253+
254+
if (str_ends_with($key, '()')) {
255+
$method = substr($key, 0, -2);
256+
/** @psalm-suppress MixedMethodCall */
257+
return $array->$method();
258+
}
259+
260+
try {
261+
/** @psalm-suppress MixedPropertyFetch */
262+
return $array::$$key;
263+
} catch (Throwable) {
264+
/**
265+
* This is expected to fail if the property does not exist, or __get() is not implemented.
266+
* It is not reliably possible to check whether a property is accessible beforehand.
267+
*
268+
* @psalm-suppress MixedPropertyFetch
269+
*/
270+
return $array->$key;
271+
}
272+
}
273+
274+
return $default;
275+
}
276+
236277
/**
237278
* Retrieves the value of an array element or object property with the given key or property name.
238279
* If the key does not exist in the array or object, the default value will be returned instead.
@@ -1555,45 +1596,4 @@ private static function parseMixedPath(array|float|int|string $path, string $del
15551596
}
15561597
return $newPath;
15571598
}
1558-
1559-
/**
1560-
* @param mixed $array Array or object to extract value from, otherwise method will return $default.
1561-
* @param float|int|string $key Key name of the array element, object property name or object method like `getValue()`.
1562-
* @param mixed $default The default value to be returned if the specified array key does not exist. Not used when
1563-
* getting value from an object.
1564-
*
1565-
* @return mixed The value of the element if found, default value otherwise.
1566-
*/
1567-
private static function getRootValue(mixed $array, float|int|string $key, mixed $default): mixed
1568-
{
1569-
if (is_array($array)) {
1570-
$key = self::normalizeArrayKey($key);
1571-
return array_key_exists($key, $array) ? $array[$key] : $default;
1572-
}
1573-
1574-
if (is_object($array)) {
1575-
$key = (string) $key;
1576-
1577-
if (str_ends_with($key, '()')) {
1578-
$method = substr($key, 0, -2);
1579-
/** @psalm-suppress MixedMethodCall */
1580-
return $array->$method();
1581-
}
1582-
1583-
try {
1584-
/** @psalm-suppress MixedPropertyFetch */
1585-
return $array::$$key;
1586-
} catch (Throwable) {
1587-
/**
1588-
* This is expected to fail if the property does not exist, or __get() is not implemented.
1589-
* It is not reliably possible to check whether a property is accessible beforehand.
1590-
*
1591-
* @psalm-suppress MixedPropertyFetch
1592-
*/
1593-
return $array->$key;
1594-
}
1595-
}
1596-
1597-
return $default;
1598-
}
15991599
}

0 commit comments

Comments
 (0)