Test Failed
Pull Request — master (#51)
by
unknown
05:51
created

prepareRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 2
eloc 7
c 3
b 1
f 1
nc 2
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Request. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
4
use Illuminate\Support\Facades\Config;
5
use Jenssegers\Mongodb\Eloquent\Model;
6
7
if (! function_exists('getTranslatedContent')) {
8
9
    /**
10
     * @param array $mlCollection
11
     *
12
     * @return string
13
     */
14
    function getTranslatedContent($mlCollection)
15
    {
16
        //Get current Lang
17
        $cl = Config::get('app.locale');
18
19
        if (is_array($mlCollection) && (array_key_exists('en_EN', $mlCollection) || array_key_exists('it_IT', $mlCollection) || ! is_null($mlCollection || ! isset($destination)))) {
0 ignored issues
show
introduced by
The condition is_null($mlCollection || ! IssetNode) is always false.
Loading history...
Comprehensibility Best Practice introduced by
The variable $destination seems to never exist and therefore isset should always be false.
Loading history...
Bug Best Practice introduced by
The expression $mlCollection of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
20
            return $mlCollection[$cl];
21
        } else {
22
            return '';
23
        }
24
    }
25
}
26
27
if (! function_exists('cl')) {
28
29
    /**
30
     * @return string current Lang
31
     */
32
    function cl()
33
    {
34
        //Get current Lang
35
        return Config::get('app.locale');
36
    }
37
}
38
39
if (! function_exists('ml')) {
40
    //save a localized field
41
    /**
42
     * @param array $destination
43
     * @param string $input
44
     *
45
     * @return array ready to be saved
46
     */
47
    function ml($destination, $input)
48
    {
49
        if (is_null($destination)) {
0 ignored issues
show
introduced by
The condition is_null($destination) is always false.
Loading history...
50
            $destination = [];
51
        }
52
53
        return array_merge($destination, [cl() => $input]);
54
    }
55
}
56
57
if (! function_exists('isML')) {
58
    function isML($value)
59
    {
60
        if (array_key_exists('is-ml', $value)) {
61
            return $value['is-ml'];
62
        } else {
63
            return false;
64
        }
65
    }
66
}
67
68
if (! function_exists('isMD')) {
69
    function isMD($value)
70
    {
71
        if (array_key_exists('is-md', $value)) {
72
            return $value['is-md'];
73
        } else {
74
            return false;
75
        }
76
    }
77
}
78
if (! function_exists('is_EM')) {
79
    function is_EM($value)
80
    {
81
        if ($value === 'EmbedsMany') {
82
            return true;
83
        } else {
84
            return false;
85
        }
86
    }
87
}
88
89
if (! function_exists('is_EO')) {
90
    function is_EO($value)
91
    {
92
        if ($value === 'EmbedsOne') {
93
            return true;
94
        } else {
95
            return false;
96
        }
97
    }
98
}
99
100
if (! function_exists('is_HM')) {
101
    function is_HM($value)
102
    {
103
        if ($value === 'HasMany') {
104
            return true;
105
        } else {
106
            return false;
107
        }
108
    }
109
}
110
111
if (! function_exists('is_HO')) {
112
    function is_HO($value)
113
    {
114
        if ($value === 'HasOne') {
115
            return true;
116
        } else {
117
            return false;
118
        }
119
    }
120
}
121
122
if (! function_exists('isEditable')) {
123
    function isEditable($value)
124
    {
125
        if (array_key_exists('is-editable', $value)) {
126
            return $value['is-editable'];
127
        } else {
128
            return true;
129
        }
130
    }
131
}
132
133
if (! function_exists('hasTarget')) {
134
    function hasTarget($value)
135
    {
136
        if (array_key_exists('has-target', $value)) {
137
            return $value['has-target'];
138
        } else {
139
            return true;
140
        }
141
    }
142
}
143
if (! function_exists('isFillable')) {
144
    function isFillable($value, $event)
145
    {
146
        if ($event === 'add') {
147
            return true;
148
        } else {
149
            return isEditable($value);
150
        }
151
    }
152
}
153
154
if (! function_exists('getRequestToBeSync')) {
155
    /**
156
     * @param $ref_id
157
     * @param $modelOnTarget
158
     * @param Request $request
159
     *
160
     * @return Request
161
     */
162
    function getRequestToBeSync($ref_id, $modelOnTarget, Request $request, $methodOnTarget)
163
    {
164
        $new_req_embeded = new stdClass();
165
        $model = new $modelOnTarget;
166
        $items = $model->getItems();
167
168
        foreach ($items as $key => $item) {
169
            if ($key == 'ref_id') {
170
                $new_req_embeded->$key = $ref_id;
171
            } else {
172
                $new_req_embeded->$key = $request->input($key);
173
            }
174
        }
175
176
        $new_req = [];
177
        $new_req[$methodOnTarget] = json_encode($new_req_embeded);
178
        $request = new Request;
179
        $request->merge($new_req);
180
181
        return $request;
182
    }
183
}
184
185
if (! function_exists('getPrimaryRequest')) {
186
    /**
187
     * @param string $request
188
     *
189
     * @return string
190
     */
191
    function getPrimaryRequest($request)
192
    {
193
        if (! is_null($request)) {
0 ignored issues
show
introduced by
The condition is_null($request) is always false.
Loading history...
194
            $arr = [];
195
            $categorylistdataJson = $request;
196
            $categorylistdataArr = json_decode($categorylistdataJson);
197
            if (empty($categorylistdataArr)) {
198
                return;
199
            } else {
200
                //extract first array  and create the primary category
201
                $primarycategory = ($categorylistdataArr[0]);
202
                $arr[] = $primarycategory;
203
204
                return json_encode($arr);
205
            }
206
        } else {
207
            return '';
208
        }
209
    }
210
}
211
212
if (! function_exists('getAID')) {
213
    /**
214
     * @param Model $model
215
     * @return string
216
     */
217
    function getAID(Model $model)
218
    {
219
        //Get Last Obj
220
        $obj = $model->orderBy('created_at', 'desc')->first();
221
222
        return is_null($obj) ? 1 : $obj->autoincrement_id + 1;
223
    }
224
}
225
226
if (! function_exists('processList')) {
227
    /**
228
     * @param array $array
229
     *
230
     * @return string
231
     */
232
    function processList($array)
233
    {
234
        $final = [];
235
        $n = count($array);
236
        for ($i = 0; $i < $n; $i++) {
237
            $obj = [];
238
            if ($array[$i] !== null) {
239
                $obj = ['label' => $array[$i], 'key' => $i];
240
                $final[] = $obj;
241
            } else {
242
                $final[] = $obj;
243
                array_pop($final);
244
            }
245
        }
246
247
        return json_encode($final);
248
    }
249
}
250
251
if (! function_exists('isRequestReadyToBeProcessed')) {
252
    function isRequestReadyToBeProcessed(Request $request)
253
    {
254
        $requests = $request->all();
255
        unset($requests['_token']);
256
257
        foreach ($requests as $key => $val) {
258
            $pos = strpos($key, '-');
259
            if ($pos === false) {
260
                return false;
261
            }
262
        }
263
264
        return true;
265
    }
266
}
267
268
if (! function_exists('removeSubCollectionInput')) {
269
    function removeSubCollectionInput(Request $request)
270
    {
271
        return $request;
272
    }
273
}
274
275
if (! function_exists('prepareRequest')) {
276
277
    /**
278
     * @param Request $request
279
     * @param array $additionalData
280
     *
281
     * @return Request
282
     */
283
    function prepareRequest(Request $request, array $additionalData)
284
    {
285
        $request = $request->merge($additionalData);
286
        $additionalData = removeSubCollectionInput($request);
287
        $request = new Request;
288
289
        if (isRequestReadyToBeProcessed($request)) {
290
            return $request->merge($additionalData);
0 ignored issues
show
Bug introduced by
$additionalData of type Illuminate\Http\Request is incompatible with the type array expected by parameter $input of Illuminate\Http\Request::merge(). ( Ignorable by Annotation )

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

290
            return $request->merge(/** @scrutinizer ignore-type */ $additionalData);
Loading history...
291
        } else {
292
            return prepareRequest($request, $additionalData);
0 ignored issues
show
Bug introduced by
$additionalData of type Illuminate\Http\Request is incompatible with the type array expected by parameter $additionalData of prepareRequest(). ( Ignorable by Annotation )

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

292
            return prepareRequest($request, /** @scrutinizer ignore-type */ $additionalData);
Loading history...
293
        }
294
    }
295
}
296
297
if (! function_exists('getArrayWithEmptyObj')) {
298
299
    /**
300
     * @param $model
301
     *
302
     * @return array
303
     */
304
    function getArrayWithEmptyObj($model, $is_EO, $is_EM)
0 ignored issues
show
Unused Code introduced by
The parameter $is_EM 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

304
    function getArrayWithEmptyObj($model, $is_EO, /** @scrutinizer ignore-unused */ $is_EM)

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...
305
    {
306
        $arr = [];
307
        if ($is_EO) {
308
            $obj = new stdClass();
309
            $embedObj = new $model;
310
            $EOitems = $embedObj->getItems();
311
312
            //Current Obj Create
313
            foreach ($EOitems as $EOkey => $item) {
314
                $obj->$EOkey = null;
315
            }
316
317
            $arr[] = $obj;
318
        }// $is_EM == empty array
319
320
        return $arr;
321
    }
322
}
323
324
if (! function_exists('getCounterForRelationships')) {
325
326
    /**
327
     * @param $method
328
     * @param $is_EO
329
     * @param $is_EM
330
     * @param $i
331
     *
332
     * @return string
333
     */
334
    function getCounterForRelationships($method, $is_EO, $is_EM, $i)
335
    {
336
        if ($method === '' || ($method !== '' && $is_EO) || ($method === '' && $is_EM)) {
337
            return '';
338
        } else {
339
            return '-'.$i;
340
        }
341
    }
342
}
343