Passed
Push — master ( 56d9d0...3a16aa )
by Yuichi
04:59
created

TreeStructureTrait::mergeCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 21
ccs 5
cts 5
cp 1
crap 2
rs 9.3142
c 0
b 0
f 0
1
<?php
2
namespace Sonar\Common;
3
use Exception;
4
5
trait TreeStructureTrait
6
{
7
    protected $tree;
8
9 2
    public function getTree()
10
    {
11 2
        return $this->tree;
12
    }
13 1
    public function initTree()
14
    {
15 1
        $this->tree = [];
16 1
    }
17 8
    public function addTreeStructure($data)
18
    {
19 8
        $tree = isset($this->tree) ? $this->tree : [];
20
21 8
        foreach ($data as $rec) {
22 8
            if (is_object($rec) === false) {
23 1
                throw new Exception('data record must be object.');
24
            }
25 7
            if (isset($rec->id) === false) {
26 2
                throw new Exception('data record object does not have property => "id".');
27
            }
28 6
            if (isset($rec->parent_id) === false || ! $rec->parent_id) {
29 6
                $rec->children = [];
30 6
                $tree[$rec->id] = $rec;
31 6
            } else {
32 5
                $this->addTree($tree, $rec);
33
            }
34 6
        }
35 6
        $this->tree = $tree;
36 6
    }
37 5
    protected function addTree(&$tree, $data)
38
    {
39 5
        foreach ($tree as $id => &$rec) {
40 5
            if ($data->parent_id == $id) {
41 5
                $data->children = [];
42 5
                $rec->children[$data->id] = $data;
43 5
                return;
44 5
            } elseif (count($rec->children)) {
45 5
                $this->addTree($rec->children, $data);
46 5
            }
47 5
            unset($rec);
48 5
        }
49 5
    }
50 4
    public function mergeCount($count_data)
51
    {
52 4
        $tree = isset($this->tree) ? $this->tree : [];
53 4
        $this->addMergeCount($tree,$count_data);
54
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
55
        foreach ($tree as &$rec) {
56
            foreach ($count_data as $count) {
57
                if (isset($count->id) === false) {
58
                    throw new Exception('count data record object does not have property => "id".');
59
                }
60
                if (isset($count->count) === false) {
61
                    throw new Exception('count data record object does not have property => "count".');
62
                }
63
                if ($rec->id == $count->id) {
64
                    $rec->count = $count->count;
65
                }
66
            }
67
        }
68
         */
69 1
        $this->tree = $tree;
70 1
    }
71
72 4
    public function addMergeCount(&$tree,$count_data)
73
    {
74 4
        foreach ( $tree as &$rec ) {
75 4
            foreach ( $count_data as $count ) {
76 4
                if (isset($count->id) === false) {
77 2
                    throw new Exception('count data record object does not have property => "id".');
78
                }
79 2
                if (isset($count->count) === false) {
80 1
                    throw new Exception('count data record object does not have property => "count".');
81
                }
82 1
                if ($rec->id == $count->id) {
83 1
                    $rec->count = $count->count;
84 1
                }
85 1
                if ( count($rec->children) ) {
86 1
                    $this->addMergeCount($rec->children,$count_data);
87 1
                }
88 1
            }
89 1
        }
90 1
    }
91
}
92