Passed
Push — master ( 92bcbd...28ff2f )
by Curtis
06:11
created

TreeController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Tree;
4
5
use App\Http\Controllers\Controller;
6
use App\Individual;
7
use App\Family;
8
9
class TreeController extends Controller
10
{
11
    public function index(Individual $individual)
12
    {
13
        return $individual->get(['individuals.id', 'individuals.first_name', 'individuals.last_name']);
14
    }
15
16
    public function links()
17
    {
18
        return $individual = \DB::table('child_parent')->select(\DB::raw('child_id as sid'), (\DB::raw('parent_id as tid')))->get('sid', 'tid');
0 ignored issues
show
Unused Code introduced by
The call to Illuminate\Database\Query\Builder::get() has too many arguments starting with 'tid'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

18
        return $individual = \DB::table('child_parent')->select(\DB::raw('child_id as sid'), (\DB::raw('parent_id as tid')))->/** @scrutinizer ignore-call */ get('sid', 'tid');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Unused Code introduced by
The assignment to $individual is dead and can be removed.
Loading history...
Bug introduced by
'sid' of type string is incompatible with the type array expected by parameter $columns of Illuminate\Database\Query\Builder::get(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

18
        return $individual = \DB::table('child_parent')->select(\DB::raw('child_id as sid'), (\DB::raw('parent_id as tid')))->get(/** @scrutinizer ignore-type */ 'sid', 'tid');
Loading history...
19
    }
20
21
    /**
22
     * Get fathers family
23
     *
24
     * @param  Integer $father_id [Father ID]
25
     * @return Array | Json
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Tree\Json was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
     */
27
    public function pedigree($father_id = 1)
28
    {
29
        $father = Family::with(['info','spouse'])->where('type_id','>=',1)->where('father_id',$father_id)->first();
30
        $data = [];
31
        if($father){
32
            $data[] = array(
33
                'name' => $father->info->getNameAttribute(),
34
                'class' => 'node',
35
                'textClass' => 'nodeText',
36
                'depthOffset' => 1,
37
                'marriages' => array(
38
                    ['spouse' => array(
39
                        'name' => $father->spouse->getNameAttribute()
40
                    ),
41
                        'children' => $father->info->children->map(function ($item) {
42
                            return ['name' => $item['first_name'].' '.$item['last_name']];
43
                        })->all()],
44
45
                ),
46
                'extra' => []
47
            );
48
        }
49
50
51
        return $data;
52
53
    }
54
55
    /**
56
     * Get individuals with its children
57
     *
58
     * @param  integer $parent_id [Parent ID / Father ID]
59
     * @param  integer $nest      [Set how many nested children]
60
     * @return Array | Json
61
     */
62
    public function show($parent_id = 1, $nest = 1){
63
        $this->nest = 1; // initialize nesting to 1
0 ignored issues
show
Bug Best Practice introduced by
The property nest does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
64
65
        $parents = Individual::where('is_active', 1)->where('id', $parent_id)->get();
66
67
        $tree = $this->getChildren($parents, (int)$nest);
0 ignored issues
show
Bug introduced by
$parents of type Illuminate\Database\Eloquent\Collection is incompatible with the type integer expected by parameter $parents of App\Http\Controllers\Tre...ntroller::getChildren(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        $tree = $this->getChildren(/** @scrutinizer ignore-type */ $parents, (int)$nest);
Loading history...
68
69
        return $tree;
70
    }
71
72
73
    /**
74
     * Loop nested children
75
     *
76
     *
77
     * @param  integer $parents [description]
78
     * @param  integer $nest    [description]
79
     * @param  boolean $show    [description]
80
     * @return array           [description]
81
     */
82
    public function getChildren($parents = 1, $nest = 1, $show = true){
83
        $data = [];
84
        if($this->nest <= $nest){
85
            $this->nest++;
86
87
            foreach($parents as $key => $parent)
0 ignored issues
show
Bug introduced by
The expression $parents of type integer is not traversable.
Loading history...
88
            {
89
                if($show === true){
90
                    $data[] = [
91
                        'id' => $parent->id,
92
                        'text' => $parent->getNameAttribute(),
93
                        'children' => $this->getChildren($parent->children,$nest,$show)
94
                    ];
95
                }else{
96
                    $data[] = [
97
                        'id' => $parent->id,
98
                        'name' => $parent->getNameAttribute(),
99
                        'children' => $this->getChildren($parent->children,$nest,$show)
100
                    ];
101
                }
102
103
            }
104
        }
105
106
        return $data;
107
108
    }
109
110
    /**
111
     * Get individuals with its children
112
     *
113
     * @param  integer $parent_id [Parent ID / Father ID]
114
     * @param  integer $nest      [Set how many nested children]
115
     * @return Array | Json
116
     */
117
    public function edge($parent_id = 1, $nest = 1){
118
        $this->nest = 1; // initialize nesting to 1
0 ignored issues
show
Bug Best Practice introduced by
The property nest does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
119
120
        $parents = Individual::where('is_active', 1)->where('id', $parent_id)->get();
121
122
        $tree = $this->getChildren($parents, (int)$nest, false);
0 ignored issues
show
Bug introduced by
$parents of type Illuminate\Database\Eloquent\Collection is incompatible with the type integer expected by parameter $parents of App\Http\Controllers\Tre...ntroller::getChildren(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

122
        $tree = $this->getChildren(/** @scrutinizer ignore-type */ $parents, (int)$nest, false);
Loading history...
123
124
        return $tree;
125
    }
126
}
127