• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

azjezz / psl / 18746347851

23 Oct 2025 11:07AM UTC coverage: 98.519% (-0.05%) from 98.572%
18746347851

push

github

web-flow
feat: add Tree component for hierarchical data structures (#546)

Introduces a new `Tree` component providing immutable tree data structures
and functional operations for hierarchical data manipulation.

Architecture:
- NodeInterface<T>: Base interface with getValue()
- LeafNode<T>: Immutable leaf nodes (no children)
- TreeNode<T>: Immutable tree nodes with getChildren()
- Pure constructor functions: tree() and leaf()

Features:
- Transformation operations: map, filter, reduce, fold
- Traversal algorithms: pre_order, post_order, level_order
- Search functions: find, any, all, contains
- Utility functions: count, depth, is_leaf, leaves
- Path operations: path_to, at_path
- Array conversion: from_array, to_array

Signed-off-by: azjezz <azjezz@protonmail.com>

190 of 194 new or added lines in 29 files covered. (97.94%)

2 existing lines in 1 file now uncovered.

5987 of 6077 relevant lines covered (98.52%)

50.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/src/Psl/Tree/depth.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Psl\Tree;
6

7
use Psl\Math;
8

9
/**
10
 * Returns the maximum depth of the tree.
11
 *
12
 * The depth is the number of edges from the root to the deepest leaf.
13
 * A leaf node has depth 0, a node with children has depth 1 + max(child depths).
14
 *
15
 * Example:
16
 *
17
 *      Tree\depth(Tree\leaf('value'))
18
 *      => 0
19
 *
20
 *      Tree\depth(Tree\tree('root', [Tree\leaf('child')]))
21
 *      => 1
22
 *
23
 * @template T
24
 *
25
 * @param NodeInterface<T> $tree
26
 *
27
 * @return int<0, max>
28
 *
29
 * @pure
30
 */
31
function depth(NodeInterface $tree): int
32
{
33
    if (!$tree instanceof TreeNode) {
4✔
34
        return 0;
4✔
35
    }
36

37
    $children = $tree->getChildren();
3✔
38
    if ([] === $children) {
3✔
NEW
39
        return 0;
×
40
    }
41

42
    $child_depths = [];
3✔
43
    foreach ($children as $child) {
3✔
44
        $child_depths[] = depth($child);
3✔
45
    }
46

47
    return 1 + Math\max($child_depths);
3✔
48
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc