From 0f0c29c9bf6b5976271d725b98c07e878ba46233 Mon Sep 17 00:00:00 2001 From: Maxwell Vandervelde Date: Sat, 6 Apr 2013 18:33:57 -0500 Subject: [PATCH] [HttpFoundation] Fixed bug in key searching for NamespacedAttributeBag Fixed a bug in NamespacedAttributeBag causing a result to be falsely found when the last key of the attribute matched the last of the queried name regardless of if the key did not exist in the search. Added Tests to demonstrate the issue and resolved by setting keys to null when iterating through query and returning proper responses in the case that the given array does in fact not exist. * Updated Syntax of null checks * Fixing missing else case for if statement in write context --- .../Session/Attribute/NamespacedAttributeBag.php | 16 ++++++++++------ .../Attribute/NamespacedAttributeBagTest.php | 2 ++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Session/Attribute/NamespacedAttributeBag.php b/src/Symfony/Component/HttpFoundation/Session/Attribute/NamespacedAttributeBag.php index 138aa36145..25dcd22869 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Attribute/NamespacedAttributeBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Attribute/NamespacedAttributeBag.php @@ -46,6 +46,10 @@ class NamespacedAttributeBag extends AttributeBag $attributes = $this->resolveAttributePath($name); $name = $this->resolveKey($name); + if (null === $attributes) { + return false; + } + return array_key_exists($name, $attributes); } @@ -57,6 +61,10 @@ class NamespacedAttributeBag extends AttributeBag $attributes = $this->resolveAttributePath($name); $name = $this->resolveKey($name); + if (null === $attributes) { + return $default; + } + return array_key_exists($name, $attributes) ? $attributes[$name] : $default; } @@ -120,12 +128,8 @@ class NamespacedAttributeBag extends AttributeBag unset($parts[count($parts)-1]); foreach ($parts as $part) { - if (!array_key_exists($part, $array)) { - if (!$writeContext) { - return $array; - } - - $array[$part] = array(); + if (null !== $array && !array_key_exists($part, $array)) { + $array[$part] = $writeContext ? array() : null; } $array = & $array[$part]; diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Attribute/NamespacedAttributeBagTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Attribute/NamespacedAttributeBagTest.php index 432499e945..0f9ef0fb33 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Attribute/NamespacedAttributeBagTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Attribute/NamespacedAttributeBagTest.php @@ -160,8 +160,10 @@ class NamespacedAttributeBagTest extends \PHPUnit_Framework_TestCase array('csrf.token/b', '4321', true), array('category', array('fishing' => array('first' => 'cod', 'second' => 'sole')), true), array('category/fishing', array('first' => 'cod', 'second' => 'sole'), true), + array('category/fishing/missing/first', null, false), array('category/fishing/first', 'cod', true), array('category/fishing/second', 'sole', true), + array('category/fishing/missing/second', null, false), array('user2.login', null, false), array('never', null, false), array('bye', null, false),