Skip to content
Permalink
Browse files

refactor: more clean up + fixed for psalm and phpstan errors.

  • Loading branch information...
tarlepp committed Oct 15, 2019
1 parent d8d0247 commit ef7ff39c865a3b9620869b5d61f91135968bf5e8
Showing with 28 additions and 29 deletions.
  1. +16 −17 src/Collection/CollectionTrait.php
  2. +6 −6 src/Resource/Collection.php
  3. +6 −6 src/Rest/ControllerCollection.php
@@ -11,22 +11,20 @@
use CallbackFilterIterator;
use Closure;
use InvalidArgumentException;
use Iterator;
use Traversable;
use IteratorAggregate;
use IteratorIterator;
use function iterator_count;
/**
* Trait CollectionTrait
*
* @package App\Collection
* @author TLe, Tarmo Leppänen <tarmo.leppanen@protacon.com>
*
* @method __construct(Traversable $items)
*/
trait CollectionTrait
{
/**
* @var Traversable
* @var IteratorAggregate
*/
private $items;
@@ -55,11 +53,7 @@ trait CollectionTrait
*/
public function get(string $className)
{
/** @var Iterator $items */
$items = $this->getFilteredIterator($className)->getAll();
$items->rewind();
$current = $items->current();
$current = $this->getFilteredItem($className);
if ($current === null) {
$this->error($className);
@@ -69,9 +63,9 @@ public function get(string $className)
}
/**
* @return Traversable
* @return IteratorAggregate
*/
public function getAll(): Traversable
public function getAll(): IteratorAggregate
{
return $this->items;
}
@@ -85,7 +79,7 @@ public function getAll(): Traversable
*/
public function has(?string $className = null): bool
{
return $this->getFilteredIterator((string)$className)->count() === 1;
return $this->getFilteredItem((string)$className) !== null;
}
/**
@@ -101,11 +95,16 @@ public function count(): int
/**
* @param string $className
*
* @return $this
* @return null|mixed
*/
private function getFilteredIterator(string $className): self
private function getFilteredItem(string $className)
{
/** @psalm-suppress UndefinedInterfaceMethod */
return new self(new CallbackFilterIterator($this->items->getIterator(), $this->filter($className)));
$filteredIterator = new CallbackFilterIterator(
new IteratorIterator($this->items->getIterator()),
$this->filter($className)
);
$filteredIterator->next();
return $filteredIterator->current();
}
}
@@ -13,7 +13,7 @@
use Closure;
use Countable;
use InvalidArgumentException;
use Traversable;
use IteratorAggregate;
use function sprintf;
/**
@@ -22,10 +22,10 @@
* @package App\Resource
* @author TLe, Tarmo Leppänen <tarmo.leppanen@protacon.com>
*
* @property Traversable|Traversable<int, RestResourceInterface> $items
* @property IteratorAggregate|IteratorAggregate<int, RestResourceInterface> $items
*
* @method RestResourceInterface get(string $className)
* @method Traversable<int, RestResourceInterface> getAll(): Traversable
* @method RestResourceInterface get(string $className)
* @method IteratorAggregate<int, RestResourceInterface> getAll(): IteratorAggregate
*/
class Collection implements Countable
{
@@ -35,9 +35,9 @@ class Collection implements Countable
/**
* Collection constructor.
*
* @param Traversable|Traversable<RestResourceInterface> $resources
* @param IteratorAggregate|IteratorAggregate<int, RestResourceInterface> $resources
*/
public function __construct(Traversable $resources)
public function __construct(IteratorAggregate $resources)
{
$this->items = $resources;
}
@@ -12,18 +12,18 @@
use Closure;
use Countable;
use InvalidArgumentException;
use Traversable;
use IteratorAggregate;
/**
* Class ControllerCollection
*
* @package App\Rest
* @author TLe, Tarmo Leppänen <tarmo.leppanen@protacon.com>
*
* @property Traversable|Traversable<int, ControllerInterface> $items
* @property IteratorAggregate|IteratorAggregate<int, ControllerInterface> $items
*
* @method ControllerInterface get(string $className)
* @method Traversable<int, ControllerInterface> getAll(): Traversable
* @method ControllerInterface get(string $className)
* @method IteratorAggregate<int, ControllerInterface> getAll(): IteratorAggregate
*/
class ControllerCollection implements Countable
{
@@ -33,9 +33,9 @@ class ControllerCollection implements Countable
/**
* Collection constructor.
*
* @param Traversable|Traversable<ControllerInterface> $controllers
* @param IteratorAggregate|IteratorAggregate<int, ControllerInterface> $controllers
*/
public function __construct(Traversable $controllers)
public function __construct(IteratorAggregate $controllers)
{
$this->items = $controllers;
}

0 comments on commit ef7ff39

Please sign in to comment.
You can’t perform that action at this time.