diff --git a/src/Get.php b/src/Get.php index 22a8b12..60d4d2e 100644 --- a/src/Get.php +++ b/src/Get.php @@ -46,12 +46,12 @@ protected static function call($path, $target, callable $outOfBoundsHandler) foreach (self::normalizePath($path) as $key) { $trace[] = $key; - if (isset($value->$key)) { + if (is_object($value) && property_exists($value, $key)) { $value = $value->$key; continue; } - if (is_array($value) && isset($value[$key])) { + if (is_array($value) && array_key_exists($key, $value)) { $value = $value[$key]; continue; } diff --git a/tests/Fixtures/ArrayAccessObject.php b/tests/Fixtures/ArrayAccessObject.php index 3964361..e4a7950 100644 --- a/tests/Fixtures/ArrayAccessObject.php +++ b/tests/Fixtures/ArrayAccessObject.php @@ -17,11 +17,11 @@ class ArrayAccessObject implements \ArrayAccess /** * Create new ArrayAccessObject. * - * @param array|null $container prefilled container + * @param array $container prefilled container */ - public function __construct(array $container = null) + public function __construct(array $container = array()) { - $this->container = $container ?: array(); + $this->container = $container; } public function offsetSet($offset, $value) @@ -37,12 +37,12 @@ public function offsetSet($offset, $value) public function offsetExists($offset) { - return isset($this->container[$offset]); + return array_key_exists($offset, $this->container); } public function offsetGet($offset) { - return isset($this->container[$offset]) ? $this->container[$offset] : null; + return $this->offsetExists($offset) ? $this->container[$offset] : null; } public function offsetUnset($offset) diff --git a/tests/GetTest.php b/tests/GetTest.php index dbf488f..491d599 100644 --- a/tests/GetTest.php +++ b/tests/GetTest.php @@ -13,6 +13,7 @@ * * @runTestsInSeparateProcesses * @preserveGlobalState disabled + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) * @SuppressWarnings(PHPMD.ExcessivePublicCount) * @SuppressWarnings(PHPMD.TooManyMethods) * @SuppressWarnings(PHPMD.TooManyPublicMethods) @@ -126,6 +127,45 @@ public function testValueShouldWorkWithNull() $this->assertEquals(value(['foo', 'bar'], null, 42), 42); } + + public function testValueShouldReturnArrayNullValues() + { + $array = ['foo' => null]; + $this->assertEquals(value('foo', $array, 'FAIL'), null); + } + + public function testValueOrFailShouldReturnArrayNullValues() + { + $array = ['foo' => null]; + $this->assertEquals(valueOrFail('foo', $array, 'FAIL'), null); + } + + public function testValueShouldReturnArrayAccessNullValues() + { + $arrayAccesss = new ArrayAccessObject(['foo' => null]); + $this->assertEquals(value('foo', $arrayAccesss, 'FAIL'), null); + } + + public function testValueOrFailShouldReturnArrayAccessNullValues() + { + $arrayAccesss = new ArrayAccessObject(['foo' => null]); + $this->assertEquals(valueOrFail('foo', $arrayAccesss, 'FAIL'), null); + } + + public function testValueShouldReturnObjectNullProperties() + { + $object = new \stdClass(); + $object->foo = null; + $this->assertEquals(value('foo', $object, 'FAIL'), null); + } + + public function testValueOrFailShouldReturnObjectNullProperties() + { + $object = new \stdClass(); + $object->foo = null; + $this->assertEquals(valueOrFail('foo', $object, 'FAIL'), null); + } + public function testValueOrFailWithArrayShouldReturn() { $this->assertEquals(valueOrFail(1, ['foo', 'bar']), 'bar');