Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
…emoving redundant docblocks, cleaning up comparison operations syntax and simplifying code
  • Loading branch information
Ocramius committed Nov 20, 2014
1 parent 035436b commit 8d151f1
Showing 1 changed file with 41 additions and 43 deletions.
84 changes: 41 additions & 43 deletions src/PriorityList.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
use Countable;
use Iterator;

/**
* Priority list
*/
class PriorityList implements Iterator, Countable
{
const EXTR_DATA = 0x00000001;
Expand All @@ -23,7 +20,7 @@ class PriorityList implements Iterator, Countable
/**
* Internal list of all items.
*
* @var array
* @var array[]
*/
protected $items = array();

Expand Down Expand Up @@ -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)
Expand All @@ -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;
}

Expand All @@ -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]);
}

Expand All @@ -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;
Expand Down Expand Up @@ -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()
{
Expand All @@ -185,56 +193,43 @@ 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()
{
return key($this->items);
}

/**
* 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()
{
Expand All @@ -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
Expand Down

0 comments on commit 8d151f1

Please sign in to comment.