Closed
Description
Description
Let's take the following empty array:
$array = array();
And the following empty class:
$object = new class {};
Check if an undefined property is readable from the class:
$accessor = PropertyAccess::createPropertyAccessor();
$accessor->isReadable($object, 'foo'); // false
Same check on the array:
$accessor = PropertyAccess::createPropertyAccessor();
$accessor->isReadable($array, '[foo]'); // true
As $array['foo']
doesn't exist, I assume it's a bug, right?
Possible solution
In PropertyAccessor::readIndex
, in the following condition:
if (isset($zval[self::VALUE][$index])) {
$result[self::VALUE] = $zval[self::VALUE][$index];
if (!isset($zval[self::REF])) {
// Save creating references when doing read-only lookups
} elseif (is_array($zval[self::VALUE])) {
$result[self::REF] = &$zval[self::REF][$index];
} elseif (is_object($result[self::VALUE])) {
$result[self::REF] = $result[self::VALUE];
}
}
Add an else
statement like:
if (isset($zval[self::VALUE][$index])) {
// ...
} else {
throw new NoSuchIndexException(...);
}
Sort as isReadable
properly returns false
on unreadable index.