From 89af9cf3f8d701691305a687273fe6aa22e0a8c5 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 16 Oct 2014 07:05:08 +0700 Subject: [PATCH 1/7] Fixes zendframework/zf2#6768 : boolean values at priority list should be valid --- src/PriorityList.php | 3 +++ test/PriorityListTest.php | 26 ++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/PriorityList.php b/src/PriorityList.php index 1dce67ba9..561bbd5c3 100644 --- a/src/PriorityList.php +++ b/src/PriorityList.php @@ -200,6 +200,9 @@ public function rewind() public function current() { $node = current($this->items); + if (isset($node['data']) && is_bool($node['data'])) { + $node['data'] = (int) $node['data']; + } $this->currentNode = $node; return ($node !== false ? $node['data'] : false); } diff --git a/test/PriorityListTest.php b/test/PriorityListTest.php index e93688539..5f2e51306 100644 --- a/test/PriorityListTest.php +++ b/test/PriorityListTest.php @@ -83,7 +83,7 @@ public function testLIFOOnly() $this->list->insert('foobar', new \stdClass()); $this->list->insert('barbaz', new \stdClass()); - $order = array(); + $orders = array(); foreach ($this->list as $key => $value) { $orders[] = $key; @@ -129,7 +129,7 @@ public function testFIFOWithPriority() $this->list->insert('bar', new \stdClass(), 0); $this->list->insert('baz', new \stdClass(), 1); - $order = array(); + $orders = array(); foreach ($this->list as $key => $value) { $orders[] = $key; @@ -147,7 +147,7 @@ public function testFIFOOnly() $this->list->insert('foobar', new \stdClass()); $this->list->insert('barbaz', new \stdClass()); - $order = array(); + $orders = array(); foreach ($this->list as $key => $value) { $orders[] = $key; @@ -162,7 +162,7 @@ public function testPriorityWithNegativesAndNull() $this->list->insert('bar', new \stdClass(), 1); $this->list->insert('baz', new \stdClass(), -1); - $order = array(); + $orders = array(); foreach ($this->list as $key => $value) { $orders[] = $key; @@ -195,4 +195,22 @@ public function testToArray() $this->list->toArray(PriorityList::EXTR_BOTH) ); } + + /** + * @group ZF2-6768 + */ + public function testBooleanValuesAreValid() + { + $this->list->insert('foo', false, null); + $this->list->insert('bar', 'test', 1); + $this->list->insert('baz', true, -1); + + $orders = array(); + + foreach ($this->list as $key => $value) { + $orders[] = $value; + } + + $this->assertEquals(array('test', false, true), $orders); + } } From bb551a9f502c13b6bddc30a91a050429de6c7930 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 19 Nov 2014 19:33:58 +0700 Subject: [PATCH 2/7] change return on valid() method --- src/PriorityList.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/PriorityList.php b/src/PriorityList.php index 561bbd5c3..3a50c07f6 100644 --- a/src/PriorityList.php +++ b/src/PriorityList.php @@ -53,7 +53,7 @@ class PriorityList implements Iterator, Countable * @var bool */ protected $sorted = false; - + /** * Holds current node, false if current() was invalid * @@ -200,10 +200,6 @@ public function rewind() public function current() { $node = current($this->items); - if (isset($node['data']) && is_bool($node['data'])) { - $node['data'] = (int) $node['data']; - } - $this->currentNode = $node; return ($node !== false ? $node['data'] : false); } @@ -238,8 +234,7 @@ public function next() */ public function valid() { - $this->current(); - return ($this->currentNode !== false); + return (current($this->items) !== false); } /** From a9c6e11fab3edf1a974effd7264d0ab70981c96b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 19 Nov 2014 20:08:06 +0700 Subject: [PATCH 3/7] fixes cs --- src/PriorityList.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PriorityList.php b/src/PriorityList.php index 3a50c07f6..700645af5 100644 --- a/src/PriorityList.php +++ b/src/PriorityList.php @@ -53,7 +53,7 @@ class PriorityList implements Iterator, Countable * @var bool */ protected $sorted = false; - + /** * Holds current node, false if current() was invalid * From 260a60f4c85ab2c892bbcddfbd728cd0d2f813a4 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 19 Nov 2014 20:21:06 +0700 Subject: [PATCH 4/7] add new list insert for null --- test/PriorityListTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/test/PriorityListTest.php b/test/PriorityListTest.php index 5f2e51306..a8fb5ead9 100644 --- a/test/PriorityListTest.php +++ b/test/PriorityListTest.php @@ -202,6 +202,7 @@ public function testToArray() public function testBooleanValuesAreValid() { $this->list->insert('foo', false, null); + $this->list->insert('foo', null, null); $this->list->insert('bar', 'test', 1); $this->list->insert('baz', true, -1); From 1e1c5d7e4b6ca74e4d64122bd3361e67ad3a1d77 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 20 Nov 2014 12:08:55 +0100 Subject: [PATCH 5/7] zendframework/zf2#6773 zendframework/zf2#6768 - adding missing @group annotations --- test/PriorityListTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/PriorityListTest.php b/test/PriorityListTest.php index a8fb5ead9..886194513 100644 --- a/test/PriorityListTest.php +++ b/test/PriorityListTest.php @@ -197,7 +197,8 @@ public function testToArray() } /** - * @group ZF2-6768 + * @group 6768 + * @group 6773 */ public function testBooleanValuesAreValid() { From 035436bdcb2660931c21a7be5f736188a5ef675a Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 20 Nov 2014 12:09:11 +0100 Subject: [PATCH 6/7] zendframework/zf2#6773 zendframework/zf2#6768 - removing unused protected property --- src/PriorityList.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/PriorityList.php b/src/PriorityList.php index 700645af5..48511648a 100644 --- a/src/PriorityList.php +++ b/src/PriorityList.php @@ -54,13 +54,6 @@ class PriorityList implements Iterator, Countable */ protected $sorted = false; - /** - * Holds current node, false if current() was invalid - * - * @var mixed - */ - protected $currentNode = false; - /** * Insert a new item. * From 8d151f1eb3ab96e77544b5ffd53e6f1e548d50f1 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 20 Nov 2014 12:27:58 +0100 Subject: [PATCH 7/7] zendframework/zf2#6773 zendframework/zf2#6768 - removing redundant docblocks, cleaning up comparison operations syntax and simplifying code --- src/PriorityList.php | 84 +++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/src/PriorityList.php b/src/PriorityList.php index 48511648a..e9cc56a22 100644 --- a/src/PriorityList.php +++ b/src/PriorityList.php @@ -12,9 +12,6 @@ use Countable; use Iterator; -/** - * Priority list - */ class PriorityList implements Iterator, Countable { const EXTR_DATA = 0x00000001; @@ -23,7 +20,7 @@ class PriorityList implements Iterator, Countable /** * Internal list of all items. * - * @var array + * @var array[] */ protected $items = array(); @@ -58,8 +55,9 @@ class PriorityList implements Iterator, Countable * Insert a new item. * * @param string $name - * @param mixed $value - * @param int $priority + * @param mixed $value + * @param int $priority + * * @return void */ public function insert($name, $value, $priority = 0) @@ -74,13 +72,23 @@ public function insert($name, $value, $priority = 0) ); } + /** + * @param string $name + * @param int $priority + * + * @return $this + * + * @throws \Exception + */ public function setPriority($name, $priority) { if (!isset($this->items[$name])) { throw new \Exception("item $name not found"); } + $this->items[$name]['priority'] = (int) $priority; - $this->sorted = false; + $this->sorted = false; + return $this; } @@ -92,11 +100,10 @@ public function setPriority($name, $priority) */ public function remove($name) { - if (!isset($this->items[$name])) { - return; + if (isset($this->items[$name])) { + $this->count--; } - $this->count--; unset($this->items[$name]); } @@ -107,7 +114,7 @@ public function remove($name) */ public function clear() { - $this->items = array(); + $this->items = array(); $this->serial = 0; $this->count = 0; $this->sorted = false; @@ -158,25 +165,26 @@ protected function compare(array $item1, array $item2) /** * Get/Set serial order mode * - * @param bool $flag + * @param bool|null $flag + * * @return bool */ public function isLIFO($flag = null) { if ($flag !== null) { - if (($flag = ($flag === true ? 1 : -1)) !== $this->isLIFO) { - $this->isLIFO = $flag; + $isLifo = $flag === true ? 1 : -1; + + if ($isLifo !== $this->isLIFO) { + $this->isLIFO = $isLifo; $this->sorted = false; } } - return $this->isLIFO === 1; + + return 1 === $this->isLIFO; } /** - * rewind(): defined by Iterator interface. - * - * @see Iterator::rewind() - * @return void + * {@inheritDoc} */ public function rewind() { @@ -185,22 +193,17 @@ public function rewind() } /** - * current(): defined by Iterator interface. - * - * @see Iterator::current() - * @return mixed + * {@inheritDoc} */ public function current() { $node = current($this->items); - return ($node !== false ? $node['data'] : false); + + return $node ? $node['data'] : false; } /** - * key(): defined by Iterator interface. - * - * @see Iterator::key() - * @return string + * {@inheritDoc} */ public function key() { @@ -208,33 +211,25 @@ public function key() } /** - * next(): defined by Iterator interface. - * - * @see Iterator::next() - * @return mixed + * {@inheritDoc} */ public function next() { $node = next($this->items); - return ($node !== false ? $node['data'] : false); + + return $node ? $node['data'] : false; } /** - * valid(): defined by Iterator interface. - * - * @see Iterator::valid() - * @return bool + * {@inheritDoc} */ public function valid() { - return (current($this->items) !== false); + return current($this->items) !== false; } /** - * count(): defined by Countable interface. - * - * @see Countable::count() - * @return int + * {@inheritDoc} */ public function count() { @@ -245,16 +240,19 @@ public function count() * Return list as array * * @param int $flag + * * @return array */ public function toArray($flag = self::EXTR_DATA) { $this->sort(); + if ($flag == self::EXTR_BOTH) { return $this->items; } + return array_map( - ($flag == self::EXTR_PRIORITY) + self::EXTR_PRIORITY === $flag ? function ($item) { return $item['priority']; } : function ($item) { return $item['data']; }, $this->items