Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
parameters:
level: 2
level: 5
paths:
- src/
- tests/
ignoreErrors:
- message: '/Call to an undefined (static )?method Respect\\Data\\(AbstractMapper|Collections\\(Collection|Filtered|Mix|Typed))::\w+\(\)\./'
- message: '/Access to an undefined property Respect\\Data\\Collections\\Collection::\$\w+\./'
- message: '/Unsafe usage of new static\(\)\./'
-
message: '/Expression .+ on a separate line does not do anything\./'
path: tests/
53 changes: 27 additions & 26 deletions src/AbstractMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,25 @@

abstract class AbstractMapper
{
protected $style;
protected $new;
protected $tracked;
protected $changed;
protected $removed;
protected $collections = array();
protected ?Styles\Stylable $style = null;
protected SplObjectStorage $new;
protected SplObjectStorage $tracked;
protected SplObjectStorage $changed;
protected SplObjectStorage $removed;
/** @var array<string, Collection> */
protected array $collections = [];

abstract protected function createStatement(Collection $fromCollection, $withExtra = null);
abstract protected function createStatement(Collection $fromCollection, mixed $withExtra = null): mixed;

protected function parseHydrated(SplObjectStorage $hydrated)
protected function parseHydrated(SplObjectStorage $hydrated): mixed
{
$this->tracked->addAll($hydrated);
$hydrated->rewind();

return $hydrated->current();
}

public function getStyle()
public function getStyle(): Styles\Stylable
{
if (null === $this->style) {
$this->setStyle(new Styles\Standard());
Expand All @@ -35,7 +36,7 @@ public function getStyle()
return $this->style;
}

public function setStyle(Styles\Stylable $style)
public function setStyle(Styles\Stylable $style): static
{
$this->style = $style;

Expand All @@ -50,23 +51,23 @@ public function __construct()
$this->new = new SplObjectStorage();
}

abstract public function flush();
abstract public function flush(): void;

public function reset()
public function reset(): void
{
$this->changed = new SplObjectStorage();
$this->removed = new SplObjectStorage();
$this->new = new SplObjectStorage();
}

public function markTracked($entity, Collection $collection)
public function markTracked(object $entity, Collection $collection): bool
{
$this->tracked[$entity] = $collection;

return true;
}

public function fetch(Collection $fromCollection, $withExtra = null)
public function fetch(Collection $fromCollection, mixed $withExtra = null): mixed
{
$statement = $this->createStatement($fromCollection, $withExtra);
$hydrated = $this->fetchHydrated($fromCollection, $statement);
Expand All @@ -77,10 +78,10 @@ public function fetch(Collection $fromCollection, $withExtra = null)
return $this->parseHydrated($hydrated);
}

public function fetchAll(Collection $fromCollection, $withExtra = null)
public function fetchAll(Collection $fromCollection, mixed $withExtra = null): array
{
$statement = $this->createStatement($fromCollection, $withExtra);
$entities = array();
$entities = [];

while ($hydrated = $this->fetchHydrated($fromCollection, $statement)) {
$entities[] = $this->parseHydrated($hydrated);
Expand All @@ -89,7 +90,7 @@ public function fetchAll(Collection $fromCollection, $withExtra = null)
return $entities;
}

public function persist($object, Collection $onCollection)
public function persist(object $object, Collection $onCollection): bool
{
$this->changed[$object] = true;

Expand All @@ -103,7 +104,7 @@ public function persist($object, Collection $onCollection)
return true;
}

public function remove($object, Collection $fromCollection)
public function remove(object $object, Collection $fromCollection): bool
{
$this->changed[$object] = true;
$this->removed[$object] = true;
Expand All @@ -117,12 +118,12 @@ public function remove($object, Collection $fromCollection)
return true;
}

public function isTracked($entity)
public function isTracked(object $entity): bool
{
return $this->tracked->contains($entity);
}

protected function fetchHydrated(Collection $collection, $statement)
protected function fetchHydrated(Collection $collection, mixed $statement): SplObjectStorage|false
{
if (!$collection->hasMore()) {
return $this->fetchSingle($collection, $statement);
Expand All @@ -131,7 +132,7 @@ protected function fetchHydrated(Collection $collection, $statement)
}
}

public function __get($name)
public function __get(string $name): Collection
{
if (isset($this->collections[$name])) {
return $this->collections[$name];
Expand All @@ -143,25 +144,25 @@ public function __get($name)
return $coll;
}

public function __isset($alias)
public function __isset(string $alias): bool
{
return isset($this->collections[$alias]);
}

public function __set($alias, $collection)
public function __set(string $alias, mixed $collection): void
{
return $this->registerCollection($alias, $collection);
$this->registerCollection($alias, $collection);
}

public function __call($name, $children)
public function __call(string $name, array $children): Collection
{
$collection = Collection::__callstatic($name, $children);
$collection->setMapper($this);

return $collection;
}

public function registerCollection($alias, Collection $collection)
public function registerCollection(string $alias, Collection $collection): void
{
$collection->setMapper($this);
$this->collections[$alias] = $collection;
Expand Down
15 changes: 8 additions & 7 deletions src/CollectionIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
use RecursiveArrayIterator;
use RecursiveIteratorIterator;

class CollectionIterator extends RecursiveArrayIterator
final class CollectionIterator extends RecursiveArrayIterator
{
protected $namesCounts = array();
/** @var array<string, int> */
protected array $namesCounts = [];

public static function recursive($target)
public static function recursive(mixed $target): RecursiveIteratorIterator
{
return new RecursiveIteratorIterator(new static($target), 1);
}

public function __construct($target = array(), &$namesCounts = array())
public function __construct(mixed $target = [], array &$namesCounts = [])
{
$this->namesCounts = &$namesCounts;
parent::__construct(is_array($target) ? $target : array($target));
parent::__construct(is_array($target) ? $target : [$target]);
}

public function key(): string|int|null
Expand All @@ -40,10 +41,10 @@ public function hasChildren(): bool
return $this->current()->hasMore();
}

public function getChildren(): ?RecursiveArrayIterator
public function getChildren(): RecursiveArrayIterator
{
$c = $this->current();
$pool = array();
$pool = [];

if ($c->hasChildren()) {
$pool = $c->getChildren();
Expand Down
Loading
Loading