Skip to content

Commit

Permalink
Merge pull request yiisoft#10590 from yiisoft/revert-array-value-isset
Browse files Browse the repository at this point in the history
reverted c00b97a/yiisoft#9915, this is expected behavior
  • Loading branch information
samdark committed Jan 15, 2016
2 parents f72e9d5 + f6dcb29 commit 79ad12f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 0 additions & 1 deletion framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Yii Framework 2 Change Log
- Bug #9874: Fixed outputting exception stacktrace in non-debug mode when `Response::FORMAT_RAW` is used (nainoon)
- Bug #9883: Passing a single `yii\db\Expression` to `Query::select()` or `::addSelect()` was not handled correctly in all cases (cebe)
- Bug #9911: Fixed `yii\helpers\BaseStringHelper::explode()` code so it doesn't remove items equal to 0 when `skip_empty` is true (silverfire, kidol)
- Bug #9915: `yii\helpers\ArrayHelper::getValue()` was erroring instead of returning `null` for non-existing object properties (totaldev, samdark)
- Bug #9924: Fixed `yii.js` handleAction corrupted parameter values containing quote (") character (silverfire)
- Bug #9984: Fixed wrong captcha color in case Imagick is used (DrDeath72)
- Bug #9999: Fixed `yii\web\UrlRule` to allow route parameter names with `-`, `_`, `.`characters (silverfire)
Expand Down
4 changes: 3 additions & 1 deletion framework/helpers/BaseArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ public static function getValue($array, $key, $default = null)
$key = substr($key, $pos + 1);
}

if (is_object($array) && isset($array->$key)) {
if (is_object($array)) {
// this is expected to fail if the property does not exist, or __get() is not implemented
// it is not reliably possible to check whether a property is accessable beforehand
return $array->$key;
} elseif (is_array($array)) {
return array_key_exists($key, $array) ? $array[$key] : $default;
Expand Down
22 changes: 22 additions & 0 deletions tests/framework/helpers/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,33 @@ public function testGetValue($key, $expected, $default = null)

public function testGetValueObjects()
{
$arrayObject = new \ArrayObject(['id' => 23], \ArrayObject::ARRAY_AS_PROPS);
$this->assertEquals(23, ArrayHelper::getValue($arrayObject, 'id'));

$object = new Post1();
$this->assertEquals(23, ArrayHelper::getValue($object, 'id'));
}

/**
* This is expected to result in a PHP error
* @expectedException \PHPUnit_Framework_Error
*/
public function testGetValueNonexistingProperties1()
{
$object = new Post1();
$this->assertEquals(null, ArrayHelper::getValue($object, 'nonExisting'));
}

/**
* This is expected to result in a PHP error
* @expectedException \PHPUnit_Framework_Error
*/
public function testGetValueNonexistingProperties2()
{
$arrayObject = new \ArrayObject(['id' => 23], \ArrayObject::ARRAY_AS_PROPS);
$this->assertEquals(23, ArrayHelper::getValue($arrayObject, 'nonExisting'));
}

public function testIsAssociative()
{
$this->assertFalse(ArrayHelper::isAssociative('test'));
Expand Down

0 comments on commit 79ad12f

Please sign in to comment.