Move FrameIterator to FrameCollection

This commit is contained in:
filp
2013-05-10 18:41:25 +01:00
parent b3403e3f95
commit 0535adaa0f
4 changed files with 51 additions and 93 deletions
+41
View File
@@ -0,0 +1,41 @@
<?php
/**
* Whoops - php errors for cool kids
* @author Filipe Dobreira <http://github.com/filp>
*/
namespace Whoops\Exception;
use Whoops\Exception\Frame;
use IteratorAggregate;
/**
* Mostly just implements iterator methods, the only
* notable aspects is that it is read-only, and instantiates
* Frame objects on demand.
*/
class FrameCollection implements IteratorAggregate
{
/**
* @var array[]
*/
private $frames;
/**
* @param array $frames
*/
public function __construct(array $frames)
{
$this->frames = array_map(function($frame) {
return new Frame($frame);
}, $frames);
}
/**
* @see IteratorAggregate::getIterator
* @return Whoops\Exception\Frame[]
*/
public function getIterator()
{
return $this->frames;
}
}
-83
View File
@@ -1,83 +0,0 @@
<?php
/**
* Whoops - php errors for cool kids
* @author Filipe Dobreira <http://github.com/filp>
*/
namespace Whoops\Exception;
use Whoops\Exception\Frame;
use Iterator;
use Countable;
/**
* Mostly just implements iterator methods, the only
* notable aspects is that it is read-only, and instantiates
* Frame objects on demand.
*/
class FrameIterator implements Iterator, Countable
{
/**
* @var array[]
*/
private $frames;
/**
* @return Whoops\Exception\Frame|false
*/
public function current()
{
$current = current($this->frames);
if($current === false) {
return false;
}
if(!$current instanceof Frame) {
$current = $this->frames[$this->key()] = new Frame($current);
}
return $current;
}
/**
* @param array[]
*/
public function __construct(array $frames)
{
$this->frames = $frames;
}
/**
* @return bool
*/
public function valid()
{
return $this->current() !== false;
}
public function next()
{
next($this->frames);
}
public function rewind()
{
reset($this->frames);
}
/**
* @return mixed
*/
public function key()
{
return key($this->frames);
}
/**
* @return int
*/
public function count()
{
return count($this->frames);
}
}
+8 -8
View File
@@ -5,7 +5,7 @@
*/
namespace Whoops\Exception;
use Whoops\Exception\FrameIterator;
use Whoops\Exception\FrameCollection;
use Whoops\Exception\ErrorException;
use Exception;
@@ -17,9 +17,9 @@ class Inspector
private $exception;
/**
* @var Whoops\Exception\FrameIterator
* @var Whoops\Exception\FrameCollection
*/
private $framesIterator;
private $frames;
/**
* @param Exception $exception The exception to inspect
@@ -56,12 +56,12 @@ class Inspector
/**
* Returns an iterator for the inspected exception's
* frames.
* @return Whoops\Exception\FrameIterator
* @return Whoops\Exception\FrameCollection
*/
public function getFrames()
{
if($this->framesIterator === null) {
$frames = $this->exception->getTrace();
if($this->frames === null) {
$frames = $this->exception->getTrace();
// If we're handling an ErrorException thrown by Whoops,
// get rid of the last frame, which matches the handleError method,
@@ -74,10 +74,10 @@ class Inspector
$firstFrame = $this->getFrameFromException($this->exception);
array_unshift($frames, $firstFrame);
}
$this->framesIterator = new FrameIterator($frames);
$this->frames = new FrameCollection($frames);
}
return $this->framesIterator;
return $this->frames;
}
/**
+2 -2
View File
@@ -57,11 +57,11 @@ class InspectorTest extends TestCase
/**
* @covers Whoops\Exception\Inspector::getFrames
*/
public function testGetFramesReturnsIterator()
public function testGetFramesReturnsCollection()
{
$exception = $this->getException();
$inspector = $this->getInspectorInstance($exception);
$this->assertInstanceOf('Whoops\\Exception\\FrameIterator', $inspector->getFrames());
$this->assertInstanceOf('Whoops\\Exception\\FrameCollection', $inspector->getFrames());
}
}