nette / utils

🛠 Lightweight utilities for string & array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.
https://doc.nette.org/utils
Other
1.98k stars 147 forks source link

Wrong mechanism of detecting ArrayHash::offsetExists() #252

Closed zeleznypa closed 3 years ago

zeleznypa commented 3 years ago

When i call the following code:

$value = null;
$data = new Nette\Utils\ArrayHash();
$data->offsetSet('foo', $value);

I expect that key foo exists, with NULL as value, but when I call offsetExists I will gain false

It will be better IMHO to use the property_exists instead of isset here

dg commented 3 years ago

Do you think isset($data->foo) should be different from isset($data['foo'])?

zeleznypa commented 3 years ago

Good question :)

Unfortunately you can't call array_key_exists on \ArrayAccess for this case. I can call the property_exists on it, but it is based on the current inner implementation of the ArrayHash. Another \ ArrayAccess implementation can be implemented differently.

So the answer is not 100% "no"...

dg commented 3 years ago

I suggest not wasting time and energy on this matter :)

zeleznypa commented 3 years ago

I though about possibilities how to do it properly, but also not found the 100% silver bullet solution.

So for my case I have own class that extends the Nette\Utils\ArrayHash object and overwrite the method.

    /**
     * offsetExists null value hotfix
     *
    public function offsetExists($key): bool
    {
        return property_exists($this, (string) $key);
    }

Thanks for your time David and for your more than good work.