Skip to content

Commit

Permalink
use generics for node value
Browse files Browse the repository at this point in the history
  • Loading branch information
smoench committed Dec 10, 2024
1 parent aaa43ba commit 3bf0846
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 66 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
'declare_strict_types' => false,
'final_class' => false,
'void_return' => false,
'phpdoc_to_property_type' => false, // when turned on it breaks generics phpdoc types
]));

$config = PhpCsFixer\Config\Factory::fromRuleSet($ruleSet);
Expand Down
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 31 additions & 42 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,70 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.12.0@f90118cdeacd0088e7215e64c0c99ceca819e176">
<files psalm-version="5.26.1@d747f6500b38ac4f7dfc5edbcae6e4b637d7add0">
<file src="src/Builder/NodeBuilder.php">
<MixedAssignment>
<code>$value</code>
<code><![CDATA[$value]]></code>
</MixedAssignment>
</file>
<file src="src/Builder/NodeBuilderInterface.php">
<PossiblyUnusedReturnValue>
<code>static</code>
<code>static</code>
<code><![CDATA[static]]></code>
<code><![CDATA[static]]></code>
</PossiblyUnusedReturnValue>
</file>
<file src="src/Node/NodeInterface.php">
<PossiblyUnusedReturnValue>
<code>mixed</code>
<code>static</code>
<code>static</code>
<code>static</code>
<code><![CDATA[static]]></code>
<code><![CDATA[static]]></code>
<code><![CDATA[static]]></code>
<code><![CDATA[static]]></code>
</PossiblyUnusedReturnValue>
</file>
<file src="src/Node/NodeTrait.php">
<ArgumentTypeCoercion>
<code>$heights</code>
<code><![CDATA[$heights]]></code>
</ArgumentTypeCoercion>
<LessSpecificImplementedReturnType>
<code>array</code>
<code>array</code>
<code>array</code>
<code><![CDATA[array]]></code>
<code><![CDATA[array]]></code>
<code><![CDATA[array]]></code>
</LessSpecificImplementedReturnType>
<MixedArgument>
<code>$child</code>
<code><![CDATA[$child]]></code>
</MixedArgument>
<MixedAssignment>
<code>$child</code>
<code>$child</code>
<code>$child</code>
<code>$child</code>
<code>$heights[]</code>
<code>$size</code>
<code><![CDATA[$child]]></code>
<code><![CDATA[$child]]></code>
<code><![CDATA[$child]]></code>
<code><![CDATA[$child]]></code>
<code><![CDATA[$heights[]]]></code>
<code><![CDATA[$size]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code>int</code>
<code>int</code>
<code><![CDATA[int]]></code>
<code><![CDATA[int]]></code>
</MixedInferredReturnType>
<MixedMethodCall>
<code>getHeight</code>
<code>getSize</code>
<code>setParent</code>
<code><![CDATA[getHeight]]></code>
<code><![CDATA[getSize]]></code>
<code><![CDATA[setParent]]></code>
</MixedMethodCall>
<MixedOperand>
<code><![CDATA[$child->getSize()]]></code>
<code>\max($heights)</code>
<code><![CDATA[\max($heights)]]></code>
</MixedOperand>
<MoreSpecificReturnType>
<code>?static</code>
</MoreSpecificReturnType>
<PossiblyNullReference>
<code>getDepth</code>
<code><![CDATA[getDepth]]></code>
</PossiblyNullReference>
</file>
<file src="src/Visitor/PostOrderVisitor.php">
<MixedArgument>
<code><![CDATA[$child->accept($this)]]></code>
</MixedArgument>
<MixedReturnTypeCoercion>
<code>$nodes</code>
<code><![CDATA[$nodes]]></code>
<code><![CDATA[array<int, NodeInterface>]]></code>
</MixedReturnTypeCoercion>
</file>
Expand All @@ -73,7 +70,7 @@
<code><![CDATA[$child->accept($this)]]></code>
</MixedArgument>
<MixedReturnTypeCoercion>
<code>$nodes</code>
<code><![CDATA[$nodes]]></code>
<code><![CDATA[array<int, NodeInterface>]]></code>
</MixedReturnTypeCoercion>
</file>
Expand All @@ -82,7 +79,7 @@
<code><![CDATA[$child->accept($this)]]></code>
</MixedArgument>
<MixedReturnTypeCoercion>
<code>$yield</code>
<code><![CDATA[$yield]]></code>
<code><![CDATA[array<int, NodeInterface>]]></code>
</MixedReturnTypeCoercion>
</file>
Expand All @@ -91,18 +88,10 @@
<code><![CDATA['Tree\Node\Node']]></code>
</ArgumentTypeCoercion>
<InvalidDocblock>
<code>array[Node]</code>
<code><![CDATA[array[Node]]]></code>
</InvalidDocblock>
<MissingClosureReturnType>
<code>static function (Node $node) {</code>
</MissingClosureReturnType>
<PossiblyNullReference>
<code>leaf</code>
<code><![CDATA[leaf]]></code>
</PossiblyNullReference>
</file>
<file src="test/Unit/Node/NodeTest.php">
<TypeDoesNotContainType>
<code>assertSame</code>
</TypeDoesNotContainType>
</file>
</files>
9 changes: 8 additions & 1 deletion src/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@

namespace Tree\Node;

/**
* @template TValue
*
* @template-implements NodeInterface<TValue>
*/
class Node implements NodeInterface
{
/** @use NodeTrait<TValue> */
use NodeTrait;

/**
* @param null|TValue $value
* @param array<int, NodeInterface> $children
*/
public function __construct(
mixed $value = null,
$value = null,
array $children = [],
) {
$this->setValue($value);
Expand Down
18 changes: 10 additions & 8 deletions src/Node/NodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,27 @@
* Interface for tree nodes.
*
* @author Nicolò Martini <[email protected]>
*
* @template TValue
*/
interface NodeInterface
{
/**
* Set the value of the current node.
*
* @param TValue $value
*/
public function setValue(mixed $value): static;
public function setValue($value): static;

/**
* Get the current node value.
*
* @return TValue
*/
public function getValue(): mixed;
public function getValue();

/**
* Add a child.
*
* @return mixed
*/
public function addChild(self $child): static;

Expand All @@ -58,8 +62,6 @@ public function getChildren(): array;
* Replace the children set with the given one.
*
* @param array<int, NodeInterface> $children
*
* @return mixed
*/
public function setChildren(array $children): static;

Expand All @@ -71,7 +73,7 @@ public function setParent(?self $parent = null): void;
/**
* Return the parent node.
*/
public function getParent(): ?static;
public function getParent(): ?self;

/**
* Retrieves all ancestors of node excluding current node.
Expand Down Expand Up @@ -115,7 +117,7 @@ public function isLeaf(): bool;
/**
* Find the root of the node.
*/
public function root(): static;
public function root(): self;

/**
* Return the distance from the current node to the root.
Expand Down
22 changes: 17 additions & 5 deletions src/Node/NodeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,36 @@

use Tree\Visitor\Visitor;

/**
* @template TValue
*/
trait NodeTrait
{
private mixed $value = null;
/**
* @var null|TValue
*/
private $value;
private ?NodeInterface $parent = null;

/**
* @var array<int, NodeInterface>
*/
private array $children = [];

public function setValue(mixed $value): static
/**
* @param null|TValue $value
*/
public function setValue($value): static
{
$this->value = $value;

return $this;
}

public function getValue(): mixed
/**
* @return null|TValue
*/
public function getValue()
{
return $this->value;
}
Expand Down Expand Up @@ -90,7 +102,7 @@ public function setParent(?NodeInterface $parent = null): void
$this->parent = $parent;
}

public function getParent(): ?static
public function getParent(): ?NodeInterface
{
return $this->parent;
}
Expand Down Expand Up @@ -153,7 +165,7 @@ public function isLeaf(): bool
return [] === $this->children;
}

public function root(): static
public function root(): NodeInterface
{
$node = $this;

Expand Down
2 changes: 1 addition & 1 deletion src/Visitor/PostOrderVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PostOrderVisitor implements Visitor
/**
* @return array<int, NodeInterface> $node
*/
public function visit(NodeInterface $node): mixed
public function visit(NodeInterface $node): array
{
$nodes = [];

Expand Down
2 changes: 1 addition & 1 deletion src/Visitor/YieldVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class YieldVisitor implements Visitor
/**
* @return array<int, NodeInterface>
*/
public function visit(NodeInterface $node): mixed
public function visit(NodeInterface $node): array
{
if ($node->isLeaf()) {
return [$node];
Expand Down
1 change: 1 addition & 0 deletions test/Unit/Node/NodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function testConstructorSetsChildren(): void

public function testSetValue(): void
{
/** @var Node<\stdClass|string> $node */
$node = new Node();

$node->setValue('string value');
Expand Down

0 comments on commit 3bf0846

Please sign in to comment.