Skip to content

Commit

Permalink
Merge pull request #113 from nicmart/fix/return-type
Browse files Browse the repository at this point in the history
Fix: Add return type declarations
  • Loading branch information
localheinz committed Jan 27, 2023
2 parents 140d85c + dbc12e0 commit 1885085
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 191 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand Down
121 changes: 46 additions & 75 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,61 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.6.0@e784128902dfe01d489c4123d69918a9f3c1eac5">
<file src="src/Builder/NodeBuilder.php">
<MissingReturnType>
<code>emptyStack</code>
<code>pushNode</code>
</MissingReturnType>
<MixedAssignment>
<code>$value</code>
</MixedAssignment>
<MixedMethodCall>
<code>pushNode</code>
</MixedMethodCall>
<ParamNameMismatch>
<code>$value1</code>
</ParamNameMismatch>
</file>
<file src="src/Builder/NodeBuilderInterface.php">
<PossiblyUnusedReturnValue>
<code>NodeBuilderInterface</code>
<code>NodeBuilderInterface</code>
<code>static</code>
<code>static</code>
</PossiblyUnusedReturnValue>
</file>
<file src="src/Node/NodeInterface.php">
<MissingReturnType>
<code>accept</code>
<code>setParent</code>
</MissingReturnType>
<PossiblyUnusedMethod>
<code>removeAllChildren</code>
<code>removeChild</code>
</PossiblyUnusedMethod>
<PossiblyUnusedReturnValue>
<code>NodeInterface</code>
<code>mixed</code>
<code>static</code>
<code>static</code>
<code>static</code>
</PossiblyUnusedReturnValue>
</file>
<file src="src/Node/NodeTrait.php">
<ArgumentTypeCoercion>
<code>$heights</code>
</ArgumentTypeCoercion>
<LessSpecificImplementedReturnType>
<code>array</code>
<code>array</code>
<code>array</code>
</LessSpecificImplementedReturnType>
<MissingParamType>
<code>$value</code>
</MissingParamType>
<MissingReturnType>
<code>accept</code>
<code>removeParentFromChildren</code>
<code>setParent</code>
</MissingReturnType>
<MixedArgument>
<code>$child</code>
</MixedArgument>
<MixedAssignment>
<code>$child</code>
<code>$child</code>
<code>$child</code>
<code>$child</code>
<code>$heights[]</code>
<code>$size</code>
</MixedAssignment>
<MixedReturnTypeCoercion>
<code>getAncestorsAndSelf</code>
</MixedReturnTypeCoercion>
<MixedInferredReturnType>
<code>int</code>
<code>int</code>
</MixedInferredReturnType>
<MixedMethodCall>
<code>getHeight</code>
<code>getSize</code>
<code>setParent</code>
</MixedMethodCall>
<MixedOperand>
<code>$child-&gt;getSize()</code>
<code>\max($heights)</code>
</MixedOperand>
<MoreSpecificReturnType>
<code>?static</code>
</MoreSpecificReturnType>
<PossiblyNullReference>
<code>getChildren</code>
<code>getChildren</code>
Expand All @@ -66,16 +71,28 @@
<MixedArgument>
<code>$child-&gt;accept($this)</code>
</MixedArgument>
<MixedReturnTypeCoercion>
<code>$nodes</code>
<code>array&lt;int, NodeInterface&gt;</code>
</MixedReturnTypeCoercion>
</file>
<file src="src/Visitor/PreOrderVisitor.php">
<MixedArgument>
<code>$child-&gt;accept($this)</code>
</MixedArgument>
<MixedReturnTypeCoercion>
<code>$nodes</code>
<code>array&lt;int, NodeInterface&gt;</code>
</MixedReturnTypeCoercion>
</file>
<file src="src/Visitor/YieldVisitor.php">
<MixedArgument>
<code>$child-&gt;accept($this)</code>
</MixedArgument>
<MixedReturnTypeCoercion>
<code>$yield</code>
<code>array&lt;int, NodeInterface&gt;</code>
</MixedReturnTypeCoercion>
</file>
<file src="test/Unit/Builder/NodeBuilderTest.php">
<ArgumentTypeCoercion>
Expand All @@ -101,54 +118,8 @@
</PossiblyNullReference>
</file>
<file src="test/Unit/Node/NodeTest.php">
<MixedArgument>
<code>(new Node('child'))-&gt;addChild($grandchild = new Node('grandchild'))</code>
</MixedArgument>
<MixedAssignment>
<code>$root</code>
</MixedAssignment>
<MixedMethodCall>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>addChild</code>
<code>removeAllChildren</code>
<code>removeAllChildren</code>
<code>removeChild</code>
<code>removeChild</code>
</MixedMethodCall>
<UnusedVariable>
<code>$child2</code>
<code>$child2</code>
<code>$child2</code>
<code>$child3</code>
</UnusedVariable>
</file>
<file src="test/Unit/Visitor/YieldVisitorTest.php">
<MixedMethodCall>
<code>addChild</code>
<code>addChild</code>
</MixedMethodCall>
<UnusedVariable>
<code>$d</code>
</UnusedVariable>
<TypeDoesNotContainType>
<code>assertSame</code>
</TypeDoesNotContainType>
</file>
</files>
20 changes: 10 additions & 10 deletions src/Builder/NodeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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),
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;

Expand Down
32 changes: 8 additions & 24 deletions src/Builder/NodeBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading

0 comments on commit 1885085

Please sign in to comment.