ArrayHash empty property problem

ajda2
Member | 66
+
0
-

Hi,
I found currious situation about \Nette\Utils\ArrayHash.

ArrayHash stores data as \stdClass

ArrayHash code from nette/utils v2.4.5, which require PHP min v5.6

/**
* Replaces or appends a item.
* @return void
*/
public function offsetSet($key, $value)
{
  if (!is_scalar($key)) { // prevents NULL
  	throw new Nette\InvalidArgumentException(sprintf('Key must be either a string or an integer, %s given.', gettype($key)));
  }
  $this->$key = $value;
}

When you try set empty $key it work for PHP v 7.1, but not for PHP 7.0 and less

$arrayHash = new ArrayHash();
$arrayHash->offsetSet("", "value")

If you are not lucky and run on older PHP you will get:
Fatal error: Cannot access empty property in /var/www/vhosts/darujspravne-new-dev.dark-side.cz/vendor/nette/utils/src/Utils/ArrayHash.php on line 67

It is OK, but you dont have any stacktrace or any instruction to find what cause the problem.

Summary

This works only in PHP 7.1

$foo = new \stdClass;
$key = "";
$foo->$key = "empty";

ArrayHash possible fix for older PHP:

/**
* Replaces or appends a item.
* @return void
*/
public function offsetSet($key, $value)
{
  if (!is_scalar($key) || empty($key)) { // prevents NULL or empty string
  	throw new Nette\InvalidArgumentException(sprintf('Key must be either a string or an integer, %s given.', gettype($key)));
  }
  $this->$key = $value;
}

I post this problem here, before create pull request with fix, because I want to ask, if Im missing some reason to not fix this.

What do you think?