Completed
Push — master ( c81f6c...1650a0 )
by mw
07:33
created

OutlineTree   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 80
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addItem() 0 4 1
A categorizeItem() 0 11 3
A addProperty() 0 13 4
1
<?php
2
3
namespace SRF\Outline;
4
5
/**
6
 * A tree structure for holding the outline data
7
 *
8
 * @license GNU GPL v2+
9
 * @since 3.1
10
 */
11
class OutlineTree {
12
13
	/**
14
	 * @var []
15
	 */
16
	public $tree;
17
18
	/**
19
	 * @var []
20
	 */
21
	public $items;
22
23
	/**
24
	 * @var integer
25
	 */
26
	public $itemCount = 0;
27
28
	/**
29
	 * @var integer
30
	 */
31
	public $leafCount = 0;
32
33
	/**
34
	 * @since 3.1
35
	 *
36
	 * @param array $items
37
	 */
38 4
	public function __construct( $items = [] ) {
39 4
		$this->tree = [];
40 4
		$this->items = $items;
41 4
	}
42
43
	/**
44
	 * @since 3.1
45
	 *
46
	 * @param $item
47
	 */
48 2
	public function addItem( $item ) {
49 2
		$this->items[] = $item;
50 2
		$this->itemCount++;
51 2
	}
52
53
	/**
54
	 * @since 3.1
55
	 *
56
	 * @param $vals
57
	 * @param $item
58
	 */
59 2
	public function categorizeItem( $vals, $item ) {
60 2
		foreach ( $vals as $val ) {
61 2
			if ( array_key_exists( $val, $this->tree ) ) {
62 2
				$this->tree[$val]->items[] = $item;
63 2
				$this->tree[$val]->leafCount++;
64
			} else {
65 2
				$this->tree[$val] = new self( [ $item ] );
66 2
				$this->tree[$val]->leafCount++;
67
			}
68
		}
69 2
	}
70
71
	/**
72
	 * @since 3.1
73
	 *
74
	 * @param $property
75
	 */
76 2
	public function addProperty( $property ) {
77 2
		if ( count( $this->items ) > 0 ) {
78 2
			foreach ( $this->items as $item ) {
79 2
				$cur_vals = $item->getFieldValues( $property );
80 2
				$this->categorizeItem( $cur_vals, $item );
81
			}
82 2
			$this->items = null;
83
		} else {
84 2
			foreach ( $this->tree as $i => $node ) {
85 2
				$this->tree[$i]->addProperty( $property );
86
			}
87
		}
88 2
	}
89
90
}
91