Skip to content

[PropertyAccess] [Bug] isReadable return true for an array index even if not set. #18969

Closed
@chalasr

Description

@chalasr

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions