Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: remove the need to keep a reference to the parent node #582

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 13 additions & 16 deletions src/Mapper/Tree/Shell.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use CuyZ\Valinor\Type\Type;
use CuyZ\Valinor\Type\Types\UnresolvableType;

use function array_unshift;
use function assert;
use function implode;

Expand All @@ -30,19 +29,24 @@ final class Shell

private Attributes $attributes;

private self $parent;
/** @var list<string> */
private array $path;

/** @var list<string> */
private array $allowedSuperfluousKeys = [];

private function __construct(Settings $settings, Type $type)
/**
* @param list<string> $path
*/
private function __construct(Settings $settings, Type $type, array $path = [])
{
if ($type instanceof UnresolvableType) {
throw new UnresolvableShellType($type);
}

$this->settings = $settings;
$this->type = $type;
$this->path = $path;
}

public static function root(
Expand All @@ -55,9 +59,10 @@ public static function root(

public function child(string $name, Type $type, ?Attributes $attributes = null): self
{
$instance = new self($this->settings, $type);
$path = $this->path;
$path[] = $name;
$instance = new self($this->settings, $type, $path);
$instance->name = $name;
$instance->parent = $this;

if ($attributes) {
$instance->attributes = $attributes;
Expand All @@ -73,7 +78,7 @@ public function name(): string

public function isRoot(): bool
{
return ! isset($this->parent);
return ! isset($this->name);
}

public function withType(Type $newType): self
Expand Down Expand Up @@ -152,19 +157,11 @@ public function allowedSuperfluousKeys(): array

public function path(): string
{
if (! isset($this->parent)) {
if ($this->isRoot()) {
return '*root*';
}

$node = $this;
$path = [];

while (isset($node->parent)) {
array_unshift($path, $node->name);
$node = $node->parent;
}

return implode('.', $path);
return implode('.', $this->path);
}

private static function castCompatibleValue(Type $type, mixed $value): mixed
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Mapper/Tree/ShellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function test_shell_child_values_can_be_retrieved(): void
$shell = Shell::root(new Settings(), new FakeType(), []);
$child = $shell->child('foo', $type, $attributes)->withValue($value);

self::assertFalse($child->isRoot());
self::assertSame('foo', $child->name());
self::assertSame('foo', $child->path());
self::assertSame($type, $child->type());
Expand Down
Loading