| 1 |  |  | <?php
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Sonar\Common;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Exception;
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 4 |  |  | 
 | 
            
                                                                        
                            
            
                                    
            
            
                | 5 |  |  | trait TreeStructureTrait
 | 
            
                                                                        
                            
            
                                    
            
            
                | 6 |  |  | {
 | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  |     protected $tree;
 | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 | 3 |  |     public function getTree()
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 | 3 |  |         return $this->tree;
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 | 1 |  |     public function initTree()
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 | 1 |  |         $this->tree = [];
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 | 1 |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 9 |  |     public function addTreeStructure($data)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 9 |  |         $tree = isset($this->tree) ? $this->tree : [];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 9 |  |         foreach ($data as $rec) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 9 |  |             if (is_object($rec) === false) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 1 |  |                 throw new Exception('data record must be object.');
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 8 |  |             if (isset($rec->id) === false) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 2 |  |                 throw new Exception('data record object does not have property => "id".');
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 7 |  |             if (isset($rec->parent_id) === false || ! $rec->parent_id) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 7 |  |                 $rec->children = [];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 7 |  |                 $tree[$rec->id] = $rec;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 7 |  |             } else {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 6 |  |                 $this->addTree($tree, $rec);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 7 |  |         }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 7 |  |         $this->tree = $tree;
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 | 7 |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 6 |  |     protected function addTree(&$tree, $data)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 6 |  |         foreach ($tree as $id => &$rec) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 6 |  |             if ($data->parent_id == $id) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 6 |  |                 $data->children = [];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 6 |  |                 $rec->children[$data->id] = $data;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 6 |  |                 return;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 6 |  |             } elseif (count($rec->children)) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 6 |  |                 $this->addTree($rec->children, $data);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 6 |  |             }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 6 |  |             unset($rec);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 6 |  |         }
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 | 6 |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 5 |  |     public function mergeCount($count_data,$is_merge=false)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 5 |  |         $tree = isset($this->tree) ? $this->tree : [];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 5 |  |         foreach ( $count_data as $count ) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 5 |  |             if (isset($count->id) === false) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |                 throw new Exception('count data record object does not have property => "id".');
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 3 |  |             if (isset($count->count) === false) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |                 throw new Exception('count data record object does not have property => "count".');
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |             $this->addCount($this->tree,$count->id,$count->count,$is_merge);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  |         }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |         $this->tree = $tree;
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 | 2 |  |     }
 | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | /*
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     public function addMergeCount(&$tree,$id,$count,$is_merge)
 | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     {
 | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         foreach ( $tree as &$rec ) {
 | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             foreach ( $count_data as $count ) {
 | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |                 if (isset($count->id) === false) {
 | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |                     throw new Exception('count data record object does not have property => "id".');
 | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |                 }
 | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |                 if (isset($count->count) === false) {
 | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |                     throw new Exception('count data record object does not have property => "count".');
 | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |                 }
 | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |                 if ($rec->id == $count->id) {
 | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |                     $rec->count = $count->count;
 | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                     if ( isset($rec->parent_id) && $is_merge ) {
 | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                         $this->addCount($this->tree,$rec->parent_id,$count->count);
 | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                     }
 | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |                 } elseif ( count($rec->children) ) {
 | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |                     $this->addMergeCount($rec->children,$count_data,$is_merge);
 | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |                 }
 | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |             }
 | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             unset($rec);
 | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         }
 | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     }
 | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  |     protected function addCount(&$tree,$id,$count,$is_merge)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 2 |  |         foreach ( $tree as $rec ) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  |             if ( $rec->id == $id ) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 2 |  |                 if( isset($rec->count) ) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |                     $rec->count += $count;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |                 } else {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 2 |  |                     $rec->count = $count;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 } 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 2 |  |                 if ( isset($rec->parent_id) && $rec->parent_id && $is_merge ) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |                     $this->addCount($this->tree,$rec->parent_id,$count,$is_merge);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |                 }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 2 |  |             } elseif ( count($rec->children) ) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 2 |  |                 $this->addCount($rec->children,$id,$count,$is_merge);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |             }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 2 |  |         }
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 105 | 2 |  |     }
 | 
            
                                                        
            
                                    
            
            
                | 106 |  |  | }
 | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |  | 
            
                        
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.