Issues (6)

lib/DoctrineOrm/Association/AssociationTree.php (1 issue)

1
<?php
2
3
namespace Malef\Associate\DoctrineOrm\Association;
4
5
use Tree\Node\NodeInterface;
6
use Tree\Visitor\PreOrderVisitor;
7
8
class AssociationTree
9
{
10
    /**
11
     * @var NodeInterface
12
     */
13
    protected $rootNode;
14
15
    public function __construct(NodeInterface $rootNode)
16
    {
17
        $this->rootNode = $rootNode;
18
    }
19
20
    /**
21
     * @return AssociationPath[]
22
     */
23
    public function getAssociationPaths(): array
24
    {
25
        $associationPaths = [];
26
        foreach ($this->rootNode->getChildren() as $childNode) {
27
            $associationPaths[] = $this->getAssociationPath($childNode);
28
        }
29
30
        return $associationPaths;
31
    }
32
33
    /**
34
     * @return AssociationTreeNode[]
35
     */
36
    public function getPreOrderedNodes(): array
37
    {
38
        /* @var NodeInterface[] $nodes */
39
        // External library uses invalid typehint for return value.
40
        // @phpstan-ignore-next-line
41
        $nodes = $this->rootNode->accept(new PreOrderVisitor());
42
        $rootNode = array_shift($nodes);
43
        $associationTreeNodes = [];
44
        $nodeToAssociationTreeNodeMap = new \SplObjectStorage();
45
        // External library uses invalid typehint for return value.
46
        // @phpstan-ignore-next-line
47
        foreach ($nodes as $node) {
48
            $parentNode = $node->getParent();
49
            $associationTreeNode = new AssociationTreeNode(
50
                $node->getValue(),
51
                ($parentNode instanceof NodeInterface && $parentNode !== $rootNode)
52
                    ? $nodeToAssociationTreeNodeMap->offsetGet($parentNode)
53
                    : null
54
            );
55
            $nodeToAssociationTreeNodeMap->attach($node, $associationTreeNode);
56
            $associationTreeNodes[] = $associationTreeNode;
57
        }
58
59
        return $associationTreeNodes;
60
    }
61
62
    protected function getAssociationPath(NodeInterface $node): AssociationPath
63
    {
64
        $associations = [];
65
        do {
66
            $associations[] = $node->getValue();
67
            $childNodes = $node->getChildren();
68
            if ($node->isLeaf()) {
69
                return new AssociationPath($associations);
70
            }
71
            if (count($childNodes) > 1) {
72
                return new AssociationPath($associations, new self($node));
73
            }
74
            $node = reset($childNodes);
75
        } while (true);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Malef\Associate\Doctrine...ciation\AssociationPath. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
76
    }
77
}
78