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
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 5
level: 9
paths:
- src/
- tests/
Expand Down
9 changes: 8 additions & 1 deletion src/AbstractMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@
use Respect\Data\Collections\Collection;
use SplObjectStorage;

use function assert;

abstract class AbstractMapper
{
protected Styles\Stylable|null $style = null;

/** @var SplObjectStorage<object, mixed> */
protected SplObjectStorage $new;

/** @var SplObjectStorage<object, mixed> */
protected SplObjectStorage $tracked;

/** @var SplObjectStorage<object, mixed> */
protected SplObjectStorage $changed;

/** @var SplObjectStorage<object, mixed> */
protected SplObjectStorage $removed;

/** @var array<string, Collection> */
Expand All @@ -33,7 +39,7 @@ public function __construct()
public function getStyle(): Styles\Stylable
{
if ($this->style === null) {
$this->setStyle(new Styles\Standard());
$this->style = new Styles\Standard();
}

return $this->style;
Expand Down Expand Up @@ -125,6 +131,7 @@ public function __isset(string $alias): bool

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

Expand Down
14 changes: 10 additions & 4 deletions src/CollectionIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use RecursiveArrayIterator;
use RecursiveIteratorIterator;
use Respect\Data\Collections\Collection;

use function is_array;

/** @extends RecursiveArrayIterator<array-key, Collection> */
final class CollectionIterator extends RecursiveArrayIterator
{
/** @var array<string, int> */
Expand All @@ -19,17 +21,21 @@ public function __construct(mixed $target = [], array &$namesCounts = [])
{
$this->namesCounts = &$namesCounts;

parent::__construct(is_array($target) ? $target : [$target]);
/** @var array<Collection> $items */
$items = is_array($target) ? $target : [$target];

parent::__construct($items);
}

/** @return RecursiveIteratorIterator<CollectionIterator> */
public static function recursive(mixed $target): RecursiveIteratorIterator
{
return new RecursiveIteratorIterator(new static($target), 1);
return new RecursiveIteratorIterator(new self($target), 1);
}

public function key(): string|int|null
public function key(): string
{
$name = $this->current()->getName();
$name = $this->current()->getName() ?? '';

if (isset($this->namesCounts[$name])) {
return $name . ++$this->namesCounts[$name];
Expand Down
17 changes: 14 additions & 3 deletions src/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Respect\Data\AbstractMapper;
use RuntimeException;

use function assert;

/** @implements ArrayAccess<string, Collection> */
class Collection implements ArrayAccess
{
protected bool $required = true;
Expand Down Expand Up @@ -163,7 +166,9 @@ public function offsetExists(mixed $offset): bool

public function offsetGet(mixed $condition): mixed
{
$this->last->condition = $condition;
if ($this->last !== null) {
$this->last->condition = $condition;
}

return $this;
}
Expand Down Expand Up @@ -211,7 +216,10 @@ public function setRequired(bool $required): void

public function stack(Collection $collection): static
{
$this->last->setNext($collection);
if ($this->last !== null) {
$this->last->setNext($collection);
}

$this->last = $collection;

return $this;
Expand All @@ -228,7 +236,10 @@ public static function __callStatic(string $name, array $children): static
public function __get(string $name): static
{
if (isset($this->mapper) && isset($this->mapper->$name)) {
return $this->stack(clone $this->mapper->$name);
assert($this->mapper->$name instanceof Collection);
$cloned = clone $this->mapper->$name;

return $this->stack($cloned);
}

return $this->stack(new self($name));
Expand Down
8 changes: 4 additions & 4 deletions src/Styles/AbstractStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ abstract class AbstractStyle implements Stylable
{
protected function camelCaseToSeparator(string $name, string $separator = '_'): string
{
return preg_replace('/(?<=[a-z])([A-Z])/', $separator . '$1', $name);
return (string) preg_replace('/(?<=[a-z])([A-Z])/', $separator . '$1', $name);
}

protected function separatorToCamelCase(string $name, string $separator = '_'): string
{
$separator = preg_quote($separator, '/');

return preg_replace_callback(
return (string) preg_replace_callback(
'/' . $separator . '([a-zA-Z])/',
static fn($m) => strtoupper($m[1]),
$name,
Expand All @@ -36,7 +36,7 @@ protected function pluralToSingular(string $name): string
];
foreach ($replacements as $key => $value) {
if (preg_match($key, $name)) {
return preg_replace($key, $value, $name);
return (string) preg_replace($key, $value, $name);
}
}

Expand All @@ -51,7 +51,7 @@ protected function singularToPlural(string $name): string
];
foreach ($replacements as $key => $value) {
if (preg_match($key, $name)) {
return preg_replace($key, $value, $name);
return (string) preg_replace($key, $value, $name);
}
}

Expand Down
8 changes: 6 additions & 2 deletions tests/AbstractMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public function registerCollectionShouldAddCollectionToPool(): void

$ref = new ReflectionObject($this->mapper);
$prop = $ref->getProperty('collections');
$this->assertContains($coll, $prop->getValue($this->mapper));
/** @var array<string, Collection> $collections */
$collections = $prop->getValue($this->mapper);
$this->assertContains($coll, $collections);

$this->assertEquals($coll, $this->mapper->my_alias);
}
Expand All @@ -56,7 +58,9 @@ public function magicSetterShouldAddCollectionToPool(): void

$ref = new ReflectionObject($this->mapper);
$prop = $ref->getProperty('collections');
$this->assertContains($coll, $prop->getValue($this->mapper));
/** @var array<string, Collection> $collections */
$collections = $prop->getValue($this->mapper);
$this->assertContains($coll, $collections);

$this->assertEquals($coll, $this->mapper->my_alias);
}
Expand Down
9 changes: 6 additions & 3 deletions tests/Collections/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,19 @@ public function dynamicMethodCallShouldAcceptChildren(): void
Collection::children(),
Collection::here(),
);
$this->assertEquals(3, count($coll->getNext()->getChildren()));
$next = $coll->getNext();
$this->assertNotNull($next);
$this->assertEquals(3, count($next->getChildren()));
}

#[Test]
public function addChildShouldSetChildrenObjectProperties(): void
{
$coll = new Collection('foo_collection');
$coll->addChild(new Collection('bar_child'));
$child = $coll->getChildren();
$child = reset($child);
$children = $coll->getChildren();
$child = reset($children);
$this->assertInstanceOf(Collection::class, $child);
$this->assertEquals(false, $child->isRequired());
$this->assertEquals($coll->getName(), $child->getParentName());
}
Expand Down
9 changes: 6 additions & 3 deletions tests/InMemoryMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,13 @@ public function flush(): void
$pk = $this->getStyle()->identifier($tableName);
$pkValue = $entity->{$pk};

foreach ($this->tables[$tableName] as $index => $existing) {
$rows = $this->tables[$tableName];
foreach ($rows as $index => $existing) {
if (isset($existing[$pk]) && $existing[$pk] == $pkValue) {
unset($this->tables[$tableName][$index]);
$this->tables[$tableName] = array_values($this->tables[$tableName]);
unset($rows[$index]);
/** @var list<array<string, mixed>> $reindexed */
$reindexed = array_values($rows);
$this->tables[$tableName] = $reindexed;

break;
}
Expand Down
Loading