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

use generic for node value #333

Open
wants to merge 1 commit 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
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<null|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 TValue
*/
private $value;
private ?NodeInterface $parent = null;

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

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

return $this;
}

public function getValue(): mixed
/**
* @return 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