From dbc12e06016f832b8d0e17dbfd1c9e3c8ce0d4f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Fri, 27 Jan 2023 16:33:05 +0100 Subject: [PATCH] Fix: Add return type declarations --- CHANGELOG.md | 2 + psalm-baseline.xml | 121 ++++++++++----------------- src/Builder/NodeBuilder.php | 20 ++--- src/Builder/NodeBuilderInterface.php | 32 ++----- src/Node/NodeInterface.php | 66 +++++---------- src/Node/NodeTrait.php | 51 +++++------ src/Visitor/PostOrderVisitor.php | 5 +- src/Visitor/PreOrderVisitor.php | 5 +- src/Visitor/Visitor.php | 5 +- src/Visitor/YieldVisitor.php | 5 +- 10 files changed, 121 insertions(+), 191 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c1059b..4b2ed06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ For a full diff see [`0.5.0...master`][0.5.0...master]. ### Changed +- Added return type declarations ([#113]), by [@localheinz] - Added `getSize()` to `NodeInterface` ([#147]), by [@localheinz] - Added `root()` to `NodeInterface` ([#148]), by [@localheinz] @@ -201,6 +202,7 @@ For a full diff see [`fcfd14e...v0.1.1`][fcfd14e...0.1.1]. [#79]: https://github.com/nicmart/Tree/pull/79 [#105]: https://github.com/nicmart/Tree/pull/105 [#106]: https://github.com/nicmart/Tree/pull/106 +[#113]: https://github.com/nicmart/Tree/pull/113 [#125]: https://github.com/nicmart/Tree/pull/125 [#136]: https://github.com/nicmart/Tree/pull/136 [#137]: https://github.com/nicmart/Tree/pull/137 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index f9a8827..f5670de 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,61 +1,66 @@ - - emptyStack - pushNode - $value - - pushNode - $value1 - NodeBuilderInterface - NodeBuilderInterface + static + static - - accept - setParent - - - removeAllChildren - removeChild - - NodeInterface mixed + static + static + static $heights + + array + array + array + $value - - accept - removeParentFromChildren - setParent - $child $child + $child + $child + $child + $heights[] + $size - - getAncestorsAndSelf - + + int + int + + + getHeight + getSize + setParent + + + $child->getSize() + \max($heights) + + + ?static + getChildren getChildren @@ -66,16 +71,28 @@ $child->accept($this) + + $nodes + array<int, NodeInterface> + $child->accept($this) + + $nodes + array<int, NodeInterface> + $child->accept($this) + + $yield + array<int, NodeInterface> + @@ -101,54 +118,8 @@ - - (new Node('child'))->addChild($grandchild = new Node('grandchild')) - - - $root - - - addChild - addChild - addChild - addChild - addChild - addChild - addChild - addChild - addChild - addChild - addChild - addChild - addChild - addChild - addChild - addChild - addChild - addChild - addChild - addChild - addChild - addChild - removeAllChildren - removeAllChildren - removeChild - removeChild - - - $child2 - $child2 - $child2 - $child3 - - - - - addChild - addChild - - - $d - + + assertSame + diff --git a/src/Builder/NodeBuilder.php b/src/Builder/NodeBuilder.php index 2031c80..d65e7f9 100644 --- a/src/Builder/NodeBuilder.php +++ b/src/Builder/NodeBuilder.php @@ -29,7 +29,7 @@ public function __construct(?NodeInterface $node = null) $this->setNode($node ?: $this->nodeInstanceByValue()); } - public function setNode(NodeInterface $node) + public function setNode(NodeInterface $node): static { $this ->emptyStack() @@ -38,12 +38,12 @@ public function setNode(NodeInterface $node) return $this; } - public function getNode() + public function getNode(): NodeInterface { return $this->nodeStack[\count($this->nodeStack) - 1]; } - public function leaf($value = null) + public function leaf($value = null): static { $this->getNode()->addChild( $this->nodeInstanceByValue($value), @@ -52,7 +52,7 @@ public function leaf($value = null) return $this; } - public function leafs($value1 /* , $value2, ... */) + public function leafs($value1 /* , $value2, ... */): static { foreach (\func_get_args() as $value) { $this->leaf($value); @@ -61,7 +61,7 @@ public function leafs($value1 /* , $value2, ... */) return $this; } - public function tree($value = null) + public function tree($value = null): static { $node = $this->nodeInstanceByValue($value); $this->getNode()->addChild($node); @@ -70,33 +70,33 @@ public function tree($value = null) return $this; } - public function end() + public function end(): ?static { $this->popNode(); return $this; } - public function nodeInstanceByValue($value = null) + public function nodeInstanceByValue($value = null): NodeInterface { return new Node($value); } - public function value($value) + public function value($value): static { $this->getNode()->setValue($value); return $this; } - private function emptyStack() + private function emptyStack(): static { $this->nodeStack = []; return $this; } - private function pushNode(NodeInterface $node) + private function pushNode(NodeInterface $node): static { $this->nodeStack[] = $node; diff --git a/src/Builder/NodeBuilderInterface.php b/src/Builder/NodeBuilderInterface.php index a3f33b6..7f03c4f 100644 --- a/src/Builder/NodeBuilderInterface.php +++ b/src/Builder/NodeBuilderInterface.php @@ -22,68 +22,52 @@ interface NodeBuilderInterface { /** * Set the node the builder will manage. - * - * @return NodeBuilderInterface The current instance */ - public function setNode(NodeInterface $node); + public function setNode(NodeInterface $node): static; /** * Get the node the builder manages. - * - * @return NodeInterface */ - public function getNode(); + public function getNode(): NodeInterface; /** * Set the value of the underlaying node. * * @param mixed $value - * - * @return NodeBuilderInterface The current instance */ - public function value($value); + public function value($value): static; /** * Add a leaf to the node. * * @param mixed $value The value of the leaf node - * - * @return NodeBuilderInterface The current instance */ - public function leaf($value = null); + public function leaf($value = null): static; /** * Add several leafs to the node. * * @param mixed ...$value An arbitrary long list of values - * - * @return NodeBuilderInterface The current instance */ - public function leafs($value /* , $value2, ... */); + public function leafs($value /* , $value2, ... */): static; /** * Add a child to the node enter in its scope. * * @param null $value - * - * @return NodeBuilderInterface A NodeBuilderInterface instance linked to the child node */ - public function tree($value = null); + public function tree($value = null): static; /** * Goes up to the parent node context. - * - * @return null|NodeBuilderInterface A NodeBuilderInterface instanced linked to the parent node */ - public function end(); + public function end(): ?static; /** * Return a node instance set with the given value. Implementation can follow their own logic * in choosing the NodeInterface implmentation taking into account the value. * * @param mixed $value - * - * @return NodeInterface */ - public function nodeInstanceByValue($value = null); + public function nodeInstanceByValue($value = null): NodeInterface; } diff --git a/src/Node/NodeInterface.php b/src/Node/NodeInterface.php index 58a960f..280a432 100644 --- a/src/Node/NodeInterface.php +++ b/src/Node/NodeInterface.php @@ -24,45 +24,37 @@ interface NodeInterface * Set the value of the current node. * * @param mixed $value - * - * @return NodeInterface the current instance */ - public function setValue($value); + public function setValue($value): static; /** * Get the current node value. - * - * @return mixed */ - public function getValue(); + public function getValue(): mixed; /** * Add a child. * * @return mixed */ - public function addChild(self $child); + public function addChild(self $child): static; /** * Remove a node from children. - * - * @return NodeInterface the current instance */ - public function removeChild(self $child); + public function removeChild(self $child): static; /** * Remove all children. - * - * @return NodeInterface The current instance */ - public function removeAllChildren(); + public function removeAllChildren(): static; /** * Return the array of children. * * @return NodeInterface[] */ - public function getChildren(); + public function getChildren(): array; /** * Replace the children set with the given one. @@ -71,91 +63,73 @@ public function getChildren(); * * @return mixed */ - public function setChildren(array $children); + public function setChildren(array $children): static; /** * Set the parent node. * * @param NodeInterface $parent */ - public function setParent(?self $parent = null); + public function setParent(?self $parent = null): void; /** * Return the parent node. - * - * @return null|NodeInterface */ - public function getParent(); + public function getParent(): ?static; /** * Retrieves all ancestors of node excluding current node. - * - * @return array */ - public function getAncestors(); + public function getAncestors(): array; /** * Retrieves all ancestors of node as well as the node itself. * * @return Node[] */ - public function getAncestorsAndSelf(); + public function getAncestorsAndSelf(): array; /** * Retrieves all neighboring nodes, excluding the current node. - * - * @return array */ - public function getNeighbors(); + public function getNeighbors(): array; /** * Returns all neighboring nodes, including the current node. * * @return NodeInterface[] */ - public function getNeighborsAndSelf(); + public function getNeighborsAndSelf(): array; /** * Return true if the node is the root, false otherwise. - * - * @return bool */ - public function isRoot(); + public function isRoot(): bool; /** * Return true if the node is a child, false otherwise. - * - * @return bool */ - public function isChild(); + public function isChild(): bool; /** * Return true if the node has no children, false otherwise. - * - * @return bool */ - public function isLeaf(); + public function isLeaf(): bool; /** * Find the root of the node. - * - * @return NodeInterface */ - public function root(); + public function root(): self; /** * Return the distance from the current node to the root. - * - * @return int */ - public function getDepth(); + public function getDepth(): int; /** * Return the height of the tree whose root is this node. - * - * @return int */ - public function getHeight(); + public function getHeight(): int; /** * Return the number of nodes in a tree. @@ -167,5 +141,5 @@ public function getSize(); /** * Accept method for the visitor pattern (see http://en.wikipedia.org/wiki/Visitor_pattern). */ - public function accept(Visitor $visitor); + public function accept(Visitor $visitor): mixed; } diff --git a/src/Node/NodeTrait.php b/src/Node/NodeTrait.php index 8db5bcd..6232cef 100644 --- a/src/Node/NodeTrait.php +++ b/src/Node/NodeTrait.php @@ -26,19 +26,19 @@ trait NodeTrait */ private array $children = []; - public function setValue($value) + public function setValue($value): static { $this->value = $value; return $this; } - public function getValue() + public function getValue(): mixed { return $this->value; } - public function addChild(NodeInterface $child) + public function addChild(NodeInterface $child): static { $child->setParent($this); $this->children[] = $child; @@ -46,7 +46,7 @@ public function addChild(NodeInterface $child) return $this; } - public function removeChild(NodeInterface $child) + public function removeChild(NodeInterface $child): static { foreach ($this->children as $key => $myChild) { if ($child === $myChild) { @@ -61,19 +61,19 @@ public function removeChild(NodeInterface $child) return $this; } - public function removeAllChildren() + public function removeAllChildren(): static { $this->setChildren([]); return $this; } - public function getChildren() + public function getChildren(): array { return $this->children; } - public function setChildren(array $children) + public function setChildren(array $children): static { $this->removeParentFromChildren(); $this->children = []; @@ -85,17 +85,17 @@ public function setChildren(array $children) return $this; } - public function setParent(?NodeInterface $parent = null) + public function setParent(?NodeInterface $parent = null): void { $this->parent = $parent; } - public function getParent() + public function getParent(): ?static { return $this->parent; } - public function getAncestors() + public function getAncestors(): array { $parents = []; $node = $this; @@ -108,12 +108,12 @@ public function getAncestors() return $parents; } - public function getAncestorsAndSelf() + public function getAncestorsAndSelf(): array { return \array_merge($this->getAncestors(), [$this]); } - public function getNeighbors() + public function getNeighbors(): array { $neighbors = $this->getParent()->getChildren(); $current = $this; @@ -128,30 +128,27 @@ static function ($item) use ($current) { ); } - public function getNeighborsAndSelf() + public function getNeighborsAndSelf(): array { return $this->getParent()->getChildren(); } - /** - * @return bool - */ - public function isRoot() + public function isRoot(): bool { return null === $this->getParent(); } - public function isChild() + public function isChild(): bool { return null !== $this->getParent(); } - public function isLeaf() + public function isLeaf(): bool { return 0 === \count($this->children); } - public function root() + public function root(): static { $node = $this; @@ -166,10 +163,8 @@ public function root() * Return the distance from the current node to the root. * * Warning, can be expensive, since each descendant is visited - * - * @return int */ - public function getDepth() + public function getDepth(): int { if ($this->isRoot()) { return 0; @@ -180,10 +175,8 @@ public function getDepth() /** * Return the height of the tree whose root is this node. - * - * @return int */ - public function getHeight() + public function getHeight(): int { if ($this->isLeaf()) { return 0; @@ -198,7 +191,7 @@ public function getHeight() return \max($heights) + 1; } - public function getSize() + public function getSize(): int { $size = 1; @@ -209,12 +202,12 @@ public function getSize() return $size; } - public function accept(Visitor $visitor) + public function accept(Visitor $visitor): mixed { return $visitor->visit($this); } - private function removeParentFromChildren() + private function removeParentFromChildren(): void { foreach ($this->getChildren() as $child) { $child->setParent(null); diff --git a/src/Visitor/PostOrderVisitor.php b/src/Visitor/PostOrderVisitor.php index 885b23e..c392e0f 100644 --- a/src/Visitor/PostOrderVisitor.php +++ b/src/Visitor/PostOrderVisitor.php @@ -15,7 +15,10 @@ class PostOrderVisitor implements Visitor { - public function visit(NodeInterface $node) + /** + * @return array $node + */ + public function visit(NodeInterface $node): mixed { $nodes = []; diff --git a/src/Visitor/PreOrderVisitor.php b/src/Visitor/PreOrderVisitor.php index 8b36b33..d179a8f 100644 --- a/src/Visitor/PreOrderVisitor.php +++ b/src/Visitor/PreOrderVisitor.php @@ -15,7 +15,10 @@ class PreOrderVisitor implements Visitor { - public function visit(NodeInterface $node) + /** + * @return array $node + */ + public function visit(NodeInterface $node): array { $nodes = [ $node, diff --git a/src/Visitor/Visitor.php b/src/Visitor/Visitor.php index 6578e5d..fedcbaa 100644 --- a/src/Visitor/Visitor.php +++ b/src/Visitor/Visitor.php @@ -20,8 +20,5 @@ */ interface Visitor { - /** - * @return mixed - */ - public function visit(NodeInterface $node); + public function visit(NodeInterface $node): mixed; } diff --git a/src/Visitor/YieldVisitor.php b/src/Visitor/YieldVisitor.php index 8fb4146..34042ae 100644 --- a/src/Visitor/YieldVisitor.php +++ b/src/Visitor/YieldVisitor.php @@ -15,7 +15,10 @@ class YieldVisitor implements Visitor { - public function visit(NodeInterface $node) + /** + * @return array + */ + public function visit(NodeInterface $node): mixed { if ($node->isLeaf()) { return [$node];