Passed
Push — master ( f42972...200c28 )
by Supun
02:29
created

Json::addForeign()   C

Complexity

Conditions 11
Paths 42

Size

Total Lines 42
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 22
nc 42
nop 3
dl 0
loc 42
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Lifeeka\JSQL\Helpers;
4
5
use Lifeeka\JSQL\Extractor\JsonExtractor;
6
7
/**
8
 * Class MysqlExtractor.
9
 */
10
class Json
11
{
12
13
    private $json_text;
14
    private $json_array;
0 ignored issues
show
introduced by
The private property $json_array is not used, and could be removed.
Loading history...
15
    private $main_table_name = "main";
16
    private $foreign_keys = [];
17
    private $increment = 0;
18
19
    private $t = 0;
0 ignored issues
show
introduced by
The private property $t is not used, and could be removed.
Loading history...
20
21
22
    /**
23
     * Json constructor.
24
     *
25
     * @param string $json
26
     */
27
    public function __construct(String $json)
28
    {
29
        $this->json_text = $json;
30
        $this->json_text = json_encode($this->addID($this->json_text));
31
        $this->json_text = json_encode($this->addForeign($this->json_text));
32
    }
33
34
    /**
35
     * @return mixed
36
     */
37
    public function toArray()
38
    {
39
        return json_decode($this->json_text, true);
40
    }
41
42
    /**
43
     * @return mixed
44
     */
45
    public function toObject()
46
    {
47
        $json_object = $this->validate(json_decode($this->json_text));
48
        return $json_object;
49
    }
50
51
    /**
52
     * @return mixed
53
     */
54
    public function getForeignKeys()
55
    {
56
        return $this->foreign_keys;
57
    }
58
59
60
    /**
61
     * @param $json
62
     * @return object
63
     */
64
    public function validate($json)
65
    {
66
        if (is_array($json)) {
67
            return (object)[
68
                $this->main_table_name => $json
69
            ];
70
        }
71
        return $json;
72
    }
73
74
75
    /**
76
     * @param $array
77
     * @param bool $parent_table
78
     * @param bool $last_increment
79
     * @return array|mixed
80
     */
81
    private function addID($array, $parent_table = false, $last_increment = false)
0 ignored issues
show
Unused Code introduced by
The parameter $last_increment is not used and could be removed. ( Ignorable by Annotation )

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

81
    private function addID($array, $parent_table = false, /** @scrutinizer ignore-unused */ $last_increment = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
84
        if (!is_array($array))
85
            $array = json_decode($array, true);//if this is not the first time decode the text
86
87
        $return_array = $array;//return array
88
89
        foreach ($array ?? [] as $key => $array_item) {
90
91
            if (!is_numeric($key) && $parent_table)//single array table, no column
92
                $table_name = $parent_table . '_' . $key;
0 ignored issues
show
Bug introduced by
Are you sure $parent_table of type true can be used in concatenation? ( Ignorable by Annotation )

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

92
                $table_name = /** @scrutinizer ignore-type */ $parent_table . '_' . $key;
Loading history...
93
            elseif ($parent_table)//multiple array items
94
                $table_name = $parent_table;
95
            else {//first time loop
96
                $table_name = $this->main_table_name;
97
            }
98
99
            if (is_array($array_item)) {//if this is a array
100
                $array_item = $this->addID($array_item, $table_name, $this->increment);//recursive the array
0 ignored issues
show
Bug introduced by
$this->increment of type integer is incompatible with the type boolean expected by parameter $last_increment of Lifeeka\JSQL\Helpers\Json::addID(). ( Ignorable by Annotation )

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

100
                $array_item = $this->addID($array_item, $table_name, /** @scrutinizer ignore-type */ $this->increment);//recursive the array
Loading history...
Bug introduced by
It seems like $table_name can also be of type string; however, parameter $parent_table of Lifeeka\JSQL\Helpers\Json::addID() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

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

100
                $array_item = $this->addID($array_item, /** @scrutinizer ignore-type */ $table_name, $this->increment);//recursive the array
Loading history...
101
                if (empty($array_item['id']) && empty($array_item[0])) {//if this is a single array with no child
102
                    $array_item = ['id' => $this->increment++] + $array_item;//add id
103
                }
104
105
                if(isset($array_item[0]) && !is_array($array_item[0]) && !is_object($array_item[0])){//reference table
106
107
108
                    $array_item = (object)array_map(function ($item){
109
                        return (object)[
110
                            'id' => $this->increment++,
111
                            'value'=>$item
112
                        ];
113
                    },$array_item); 
114
                }
115
116
                $return_array[$key] = $array_item;
117
            }
118
        }
119
120
        return $return_array;
121
122
    }
123
124
    /**
125
     * @param $array
126
     * @param bool $parent_table
127
     * @param bool $parent_key
128
     * @return array|mixed
129
     */
130
    private function addForeign($array, $parent_table = null, $parent_key = false)
131
    {
132
        if (!is_array($array))
133
            $array = json_decode($array, true);
134
135
        $return_array = $array;
136
        foreach ($array ?? [] as $key => $array_item) {
137
138
139
140
            if (!is_numeric($key) && $parent_table)//single array table, no column
141
                $table_name = $parent_table . '_' . $key;
0 ignored issues
show
Bug introduced by
Are you sure $parent_table of type true can be used in concatenation? ( Ignorable by Annotation )

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

141
                $table_name = /** @scrutinizer ignore-type */ $parent_table . '_' . $key;
Loading history...
142
            elseif ($parent_table)//multiple array items
143
                $table_name = $parent_table;
144
            elseif (!is_numeric($key))  //first time loop
145
                $table_name = $key;
146
            else {//first time loop
147
                $table_name = $this->main_table_name;
148
            }
149
150
151
            if (is_array($array_item)) {
152
                $array_item = $this->addForeign($array_item, $table_name, $array_item['id'] ?? ($parent_key ?? false));
153
                $return_array[$key] = $array_item;
154
155
156
                if ($parent_key && empty($array_item[0])) {
157
                    $return_array[$key] = $array_item + ['foreign_key' => $parent_key];
158
                }
159
160
                if (!is_numeric($key)) {
161
                    $this->foreign_keys[JsonExtractor::snakeCase($table_name)] = [
162
                        'ref' => $parent_table,
163
                        'name' => JsonExtractor::snakeCase('foreign_key')
164
                    ];
165
                }
166
167
168
            }
169
        }
170
171
        return $return_array;
172
173
    }
174
}
175