WikiController::getPathTreeDetail()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 23

Duplication

Lines 4
Ratio 17.39 %

Importance

Changes 0
Metric Value
dl 4
loc 23
rs 9.2408
c 0
b 0
f 0
cc 5
nc 8
nop 2
1
<?php
2
3
namespace Fabrica\Http\Api;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Event;
7
8
use Fabrica\Http\Requests;
9
use Fabrica\Http\Api\Controller;
10
use Fabrica\Events\WikiEvent;
11
use Fabrica\Project\Eloquent\WikiFavorites;
12
use Fabrica\Utils\File;
13
use DB;
14
use MongoDB\BSON\ObjectID;
15
use Zipper;
16
17
class WikiController extends Controller
18
{
19
    /**
20
     * search path.
21
     *
22
     * @param  \Illuminate\Http\Request $request
23
     * @param  string                   $project_key
24
     * @return \Illuminate\Http\Response
25
     */
26 View Code Duplication
    public function searchPath(Request $request, $project_key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28
        $s =  $request->input('s');
29
        if (!$s) {
30
            return response()->json(['ecode' => 0, 'data' => []]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
31
        }
32
33
        if ($s === '/') {
34
            return response()->json(['ecode' => 0, 'data' => [ [ 'id' => '0', 'name' => '/' ] ] ]);
35
        }
36
37
        $query = DB::collection('wiki_' . $project_key)
38
            ->where('d', 1)
39
            ->where('del_flag', '<>', 1)
40
            ->where('name', 'like', '%' . $s . '%');
41
42
        $moved_path = $request->input('moved_path');
43
        if (isset($moved_path) && $moved_path) {
44
            $query->where('pt', '<>', $moved_path);
45
            $query->where('_id', '<>', $moved_path);
46
        }
47
48
        $directories = $query->take(20)->get(['name', 'pt']);
49
50
        $ret = [];
51
        foreach ($directories as $d)
52
        {
53
            $parents = [];
54
            $path = '';
55
            $ps = DB::collection('wiki_' . $project_key)
56
                ->whereIn('_id', $d['pt'])
57
                ->get([ 'name' ]);
58
            foreach ($ps as $val)
59
            {
60
                $parents[$val['_id']->__toString()] = $val['name'];
61
            }
62
63
            foreach ($d['pt'] as $pid)
64
            {
65
                if (isset($parents[$pid])) {
66
                    $path .= '/' . $parents[$pid];
67
                }
68
            }
69
            $path .= '/' . $d['name'];
70
            $ret[] = [ 'id' => $d['_id']->__toString(), 'name' => $path ];
71
        }
72
        return response()->json(['ecode' => 0, 'data' => parent::arrange($ret)]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of searchPath()). Are you sure this is correct? If so, you might want to change this to $this->arrange().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
73
    }
74
75
    /**
76
     * get the directory children.
77
     *
78
     * @param  string $project_key
79
     * @param  string $directory
80
     * @return \Illuminate\Http\Response
81
     */
82
    public function getDirChildren(Request $request, $project_key, $directory)
83
    {
84
        $sub_dirs = DB::collection('wiki_' . $project_key)
85
            ->where('parent', $directory)
86
            ->where('del_flag', '<>', 1)
87
            ->get();
88
89
        $res = [];
90 View Code Duplication
        foreach ($sub_dirs as $val)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
        {
92
            $res[] = [ 
93
                'id' => $val['_id']->__toString(), 
94
                'name' => $val['name'],
95
                'd' => isset($val['d']) ? $val['d'] : 0, 
96
                'parent' => isset($val['parent']) ? $val['parent'] : '' 
97
            ];
98
        }
99
100
        return response()->json([ 'ecode' => 0, 'data' => $res ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
101
    }
102
103
    /**
104
     * get the directory tree.
105
     *
106
     * @param  string $project_key
107
     * @return \Illuminate\Http\Response
108
     */
109
    public function getDirTree(Request $request, $project_key)
110
    {
111
        $dt = [ 'id' => '0', 'name' => '根目录', 'd' => 1 ];
112
113
        $curnode = $request->input('currentnode');
114
        if (!$curnode) {
115
            $curnode = '0';
116
        }
117
118
        $pt = [ '0' ];
119
        if ($curnode !== '0') {
120
            $node = DB::collection('wiki_' . $project_key)
121
                ->where('_id', $curnode)
122
                ->first();
123
124
            if ($node) {
125
                $pt = $node['pt'];
126
                if (isset($node['d']) && $node['d'] == 1) {
127
                    array_push($pt, $curnode);
128
                }
129
            }
130
        }
131
132 View Code Duplication
        foreach($pt as $val)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
        {
134
            $sub_dirs = DB::collection('wiki_' . $project_key)
135
                ->where('parent', $val)
136
                ->where('del_flag', '<>', 1)
137
                ->get();
138
139
            $this->addChildren2Tree($dt, $val, $sub_dirs);
140
        }
141
142
        return response()->json([ 'ecode' => 0, 'data' => $dt ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
143
    }
144
145
    /**
146
     * add children to tree.
147
     *
148
     * @param  array  $dt
149
     * @param  string $parent_id
150
     * @param  array  $sub_dirs
151
     * @return void
152
     */
153
    public function addChildren2Tree(&$dt, $parent_id, $sub_dirs)
154
    {
155
        $new_dirs = [];
156 View Code Duplication
        foreach($sub_dirs as $val)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
        {
158
            $new_dirs[] = [ 
159
                'id' => $val['_id']->__toString(), 
160
                'name' => $val['name'], 
161
                'd' => isset($val['d']) ? $val['d'] : 0, 
162
                'parent' => isset($val['parent']) ? $val['parent'] : '' 
163
            ];
164
        }
165
166 View Code Duplication
        if ($dt['id'] == $parent_id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
            $dt['children'] = $new_dirs;
168
            return true;
169
        }
170
        else
171
        {
172
            if (isset($dt['children']) && $dt['children']) {
173
                $children_num = count($dt['children']);
174
                for ($i = 0; $i < $children_num; $i++)
175
                {
176
                    $res = $this->addChildren2Tree($dt['children'][$i], $parent_id, $sub_dirs);
177
                    if ($res === true) {
178
                        return true;
179
                    }
180
                }
181
            }
182
            return false;
183
        }
184
    }
185
186
    /**
187
     * Display a listing of the resource.
188
     *
189
     * @return \Illuminate\Http\Response
190
     */
191
    public function index(Request $request, $project_key, $directory)
192
    {
193
        $documents = [];
0 ignored issues
show
Unused Code introduced by
$documents is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
194
        $mode = 'list';
195
        $query = DB::collection('wiki_' . $project_key);
196
197
        $name = $request->input('name');
198 View Code Duplication
        if (isset($name) && $name) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
            $mode = 'search';
200
            $query = $query->where('name', 'like', '%' . $name . '%');
201
        }
202
203
        $updated_at = $request->input('updated_at');
204
        if (isset($updated_at) && $updated_at) {
205
            $mode = 'search';
206
            $query->where(
207
                function ($query) use ($updated_at) {
208
                    $unitMap = [ 'w' => 'week', 'm' => 'month', 'y' => 'year' ];
209
                    $unit = substr($updated_at, -1);
210
                    $val = abs(substr($updated_at, 0, -1));
211
                    $query->where('created_at', '>=', strtotime(date('Ymd', strtotime('-' . $val . ' ' . $unitMap[$unit]))))
212
                        ->orwhere('updated_at', '>=', strtotime(date('Ymd', strtotime('-' . $val . ' ' . $unitMap[$unit]))));
213
                }
214
            );
215
        }
216
217
        $favorite_wikis = WikiFavorites::where('project_key', $project_key)
218
            ->where('user.id', $this->user->id)
219
            ->get()
220
            ->toArray();
221
        $favorite_dids = array_column($favorite_wikis, 'wid');
222
223
        $myfavorite = $request->input('myfavorite');
224 View Code Duplication
        if (isset($myfavorite) && $myfavorite == '1') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
225
            $mode = 'search';
226
            $favoritedIds = [];
227
            foreach ($favorite_dids as $did)
228
            {
229
                $favoritedIds[] = new ObjectID($did);
230
            }
231
232
            $query->whereIn('_id', $favoritedIds);
233
        }
234
235 View Code Duplication
        if ($directory !== '0') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
236
            $query = $query->where($mode === 'search' ? 'pt' : 'parent', $directory);
237
        }
238
        else
239
        {
240
            if ($mode === 'list') {
241
                $query = $query->where('parent', $directory);
242
            }
243
        }
244
245
        $query->where('del_flag', '<>', 1);
246
        $query->orderBy('d', 'desc')->orderBy('_id', 'desc');
247
248
        $limit = 1000; // fix me
249
        $query->take($limit);
250
        $documents = $query->get();
251
252
        $favorite_wikis = WikiFavorites::where('project_key', $project_key)
253
            ->where('user.id', $this->user->id)
254
            ->get()
255
            ->toArray();
256
        $favorite_wids = array_column($favorite_wikis, 'wid');
257
258 View Code Duplication
        foreach ($documents as $k => $d)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
259
        {
260
            if (in_array($d['_id']->__toString(), $favorite_wids)) {
261
                $documents[$k]['favorited'] = true;
262
            }
263
        }
264
265
        $path = [];
266
        $home = [];
267
        if ($directory === '0') {
268
            $path[] = [ 'id' => '0', 'name' => 'root' ];
269
            if ($mode === 'list') {
270
                foreach ($documents as $doc)
271
                {
272
                    if ((!isset($doc['d']) || $doc['d'] != 1) && strtolower($doc['name']) === 'home') {
273
                        $home = $doc;
274
                    }
275
                }
276
            }
277
        }
278
        else
279
        {
280
            $d = DB::collection('wiki_' . $project_key)
281
                ->where('_id', $directory)
282
                ->first();
283
            if ($d && isset($d['pt'])) {
284
                $path = $this->getPathTreeDetail($project_key, $d['pt']);
285
            }
286
            $path[] = [ 'id' => $directory, 'name' => $d['name'] ];
287
        }
288
289
        return response()->json([ 'ecode' => 0, 'data' => parent::arrange($documents), 'options' => [ 'path' => $path, 'home' => parent::arrange($home) ] ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of index()). Are you sure this is correct? If so, you might want to change this to $this->arrange().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
290
    }
291
292
    /**
293
     * Store a newly created resource in storage.
294
     *
295
     * @param  \Illuminate\Http\Request $request
296
     * @param  string                   $project_key
297
     * @return \Illuminate\Http\Response
298
     */
299
    public function create(Request $request, $project_key)
300
    {
301
        $d =  $request->input('d');
302
        if (isset($d) && $d == 1) {
303
            if (!$this->isPermissionAllowed($project_key, 'manage_project')) {
304
                return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
305
            }
306
            return $this->createFolder($request, $project_key);
307
        }
308
        else
309
        {
310
            return $this->createDoc($request, $project_key);
311
        }
312
    }
313
314
    /**
315
     * Store a newly created resource in storage.
316
     *
317
     * @param  \Illuminate\Http\Request $request
318
     * @param  string                   $project_key
319
     * @return \Illuminate\Http\Response
320
     */
321
    public function createDoc(Request $request, $project_key)
322
    {
323
        $insValues = [];
324
325
        $parent = $request->input('parent');
326
        if (!isset($parent)) {
327
            throw new \UnexpectedValueException('the parent directory can not be empty.', -11950);
328
        }
329
        $insValues['parent'] = $parent;
330
331
        if ($parent !== '0') {
332
            $isExists = DB::collection('wiki_' . $project_key)
333
                ->where('_id', $parent)
334
                ->where('d', 1)
335
                ->where('del_flag', '<>', 1)
336
                ->exists();
337
            if (!$isExists) {
338
                throw new \UnexpectedValueException('the parent directory does not exist.', -11951);
339
            }
340
        }
341
342
        $name = $request->input('name');
343
        if (!isset($name) || !$name) {
344
            throw new \UnexpectedValueException('the name can not be empty.', -11952);
345
        }
346
        $insValues['name'] = $name;
347
348
        $isExists = DB::collection('wiki_' . $project_key)
349
            ->where('parent', $parent)
350
            ->where('name', $name)
351
            ->where('d', '<>', 1)
352
            ->where('del_flag', '<>', 1)
353
            ->exists();
354
        if ($isExists) {
355
            throw new \UnexpectedValueException('the name cannot be repeated.', -11953);
356
        }
357
358
        $contents = $request->input('contents');
359
        if (isset($contents) && $contents) {
360
            $insValues['contents'] = $contents;
361
        }
362
363
        $insValues['pt'] = $this->getPathTree($project_key, $parent);
364
        $insValues['version'] = 1;
365
        $insValues['creator'] = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
366
        $insValues['created_at'] = time();
367
        $id = DB::collection('wiki_' . $project_key)->insertGetId($insValues);
368
369
        $isSendMsg = $request->input('isSendMsg') && true;
370
        Event::fire(new WikiEvent($project_key, $insValues['creator'], [ 'event_key' => 'create_wiki', 'isSendMsg' => $isSendMsg, 'data' => [ 'wiki_id' => $id->__toString() ] ]));
371
372
        return $this->show($request, $project_key, $id);
373
    }
374
375
    /**
376
     * Store a newly created resource in storage.
377
     *
378
     * @param  \Illuminate\Http\Request $request
379
     * @param  string                   $project_key
380
     * @return \Illuminate\Http\Response
381
     */
382 View Code Duplication
    public function createFolder(Request $request, $project_key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
383
    {
384
        $insValues = [];
385
386
        $parent = $request->input('parent');
387
        if (!isset($parent)) {
388
            throw new \UnexpectedValueException('the parent directory can not be empty.', -11950);
389
        }
390
        $insValues['parent'] = $parent;
391
392
        if ($parent !== '0') {
393
            $isExists = DB::collection('wiki_' . $project_key)
394
                ->where('_id', $parent)
395
                ->where('d', 1)
396
                ->where('del_flag', '<>', 1)
397
                ->exists();
398
            if (!$isExists) {
399
                throw new \UnexpectedValueException('the parent directory does not exist.', -11951);
400
            }
401
        }
402
403
        $name =  $request->input('name');
404
        if (!isset($name) || !$name) {
405
            throw new \UnexpectedValueException('the name can not be empty.', -11952);
406
        }
407
        $insValues['name'] = $name;
408
409
        $isExists = DB::collection('wiki_' . $project_key)
410
            ->where('parent', $parent)
411
            ->where('name', $name)
412
            ->where('d', 1)
413
            ->where('del_flag', '<>', 1)
414
            ->exists();
415
        if ($isExists) {
416
            throw new \UnexpectedValueException('the name cannot be repeated.', -11953);
417
        }
418
419
        $insValues['pt'] = $this->getPathTree($project_key, $parent);
420
        $insValues['d'] = 1;
421
        $insValues['creator'] = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
422
        $insValues['created_at'] = time();
423
424
        $id = DB::collection('wiki_' . $project_key)->insertGetId($insValues);
425
426
        $document = DB::collection('wiki_' . $project_key)->where('_id', $id)->first();
427
        return response()->json(['ecode' => 0, 'data' => parent::arrange($document)]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of createFolder()). Are you sure this is correct? If so, you might want to change this to $this->arrange().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
428
    }
429
430
    /**
431
     * check in the wiki.
432
     *
433
     * @param  string $project_key
434
     * @param  string $id
435
     * @return array
436
     */
437
    public function checkin(Request $request, $project_key, $id)
438
    {
439
        $document = DB::collection('wiki_' . $project_key)
440
            ->where('_id', $id)
441
            ->where('del_flag', '<>', 1)
442
            ->first();
443
        if (!$document) {
444
            throw new \UnexpectedValueException('the object does not exist.', -11954);
445
        }
446
447
        if (isset($document['checkin']) && $document['checkin']) {
448
            throw new \UnexpectedValueException('the object has been locked.', -11955);
449
        }
450
451
        $checkin = []; 
452
        $checkin['user'] = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
453
        $checkin['at'] = time();
454
455
        DB::collection('wiki_' . $project_key)->where('_id', $id)->update(['checkin' => $checkin]);
456
457
        return $this->show($request, $project_key, $id);
458
    }
459
460
    /**
461
     * check out the wiki.
462
     *
463
     * @param  string $project_key
464
     * @param  string $id
465
     * @return array
466
     */
467
    public function checkout(Request $request, $project_key, $id)
468
    {
469
        $document = DB::collection('wiki_' . $project_key)
470
            ->where('_id', $id)
471
            ->where('del_flag', '<>', 1)
472
            ->first();
473
        if (!$document) {
474
            throw new \UnexpectedValueException('the object does not exist.', -11954);
475
        }
476
477 View Code Duplication
        if (isset($document['checkin']) && !((isset($document['checkin']['user']) && $document['checkin']['user']['id'] == $this->user->id) || $this->isPermissionAllowed($project_key, 'manage_project'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
478
            throw new \UnexpectedValueException('the object cannot been unlocked.', -11956);
479
        }
480
481
        DB::collection('wiki_' . $project_key)->where('_id', $id)->unset('checkin');
482
483
        return $this->show($request, $project_key, $id);
484
    }
485
486
    /**
487
     * get parent treee.
488
     *
489
     * @param  string $project_key
490
     * @param  array  $pt
491
     * @return array
492
     */
493
    public function getPathTreeDetail($project_key, $pt)
494
    {
495
        $parents = [];
496
        $ps = DB::collection('wiki_' . $project_key)
497
            ->whereIn('_id', $pt)
498
            ->get([ 'name' ]);
499
        foreach ($ps as $val)
500
        {
501
            $parents[$val['_id']->__toString()] = $val['name'];
502
        }
503
504
        $path = [];
505
        foreach ($pt as $pid)
506
        {
507
            if ($pid === '0') {
508
                $path[] = [ 'id' => '0', 'name' => 'root' ];
509
            }
510 View Code Duplication
            else if (isset($parents[$pid])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
511
                $path[] = [ 'id' => $pid, 'name' => $parents[$pid] ];
512
            }
513
        }
514
        return $path;
515
    }
516
517
    /**
518
     * get parent treee.
519
     *
520
     * @param  string $project_key
521
     * @param  string $directory
522
     * @return array
523
     */
524 View Code Duplication
    public function getPathTree($project_key, $directory)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
525
    {
526
        $pt = [];
0 ignored issues
show
Unused Code introduced by
$pt is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
527
        if ($directory === '0') {
528
            $pt = [ '0' ];
529
        }
530
        else
531
        {
532
            $d = DB::collection('wiki_' . $project_key)
533
                ->where('_id', $directory)
534
                ->first();
535
            $pt = array_merge($d['pt'], [ $directory ]);
536
        }
537
        return $pt;
538
    }
539
540
    /**
541
     * get the specified resource.
542
     *
543
     * @param  \Illuminate\Http\Request $request
544
     * @param  string                   $project_key
545
     * @param  string                   $id
546
     * @return \Illuminate\Http\Response
547
     */
548
    public function show(Request $request, $project_key, $id)
549
    {
550
        $document = DB::collection('wiki_' . $project_key)
551
            ->where('_id', $id)
552
            ->where('del_flag', '<>', 1)
553
            ->first();
554
        if (!$document) {
555
            throw new \UnexpectedValueException('the object does not exist.', -11954);
556
        }
557
558
        if (WikiFavorites::where('wid', $id)->where('user.id', $this->user->id)->exists()) {
559
            $document['favorited'] = true;
560
        }
561
562
        $newest = [];
563
        $newest['name']       = $document['name'];
564
        $newest['editor']     = isset($document['editor']) ? $document['editor'] : $document['creator'];
565
        $newest['updated_at'] = isset($document['updated_at']) ? $document['updated_at'] : $document['created_at'];
566
        $newest['version']    = $document['version'];
567
568
        $v =  $request->input('v');
569
        if (isset($v) && intval($v) != $document['version']) {
570
            $w = DB::collection('wiki_version_' . $project_key)
571
                ->where('wid', $id)
572
                ->where('version', intval($v)) 
573
                ->first();
574
            if (!$w) {
575
                throw new \UnexpectedValueException('the version does not exist.', -11957);
576
            }
577
578
            $document['name']       = $w['name'];
579
            $document['contents']   = $w['contents'];
580
            $document['editor']     = $w['editor'];
581
            $document['updated_at'] = $w['updated_at'];
582
            $document['version']    = $w['version'];
583
        }
584
        
585
        $document['versions'] = DB::collection('wiki_version_' . $project_key)
586
            ->where('wid', $id)
587
            ->orderBy('_id', 'desc')
588
            ->get(['name', 'editor', 'updated_at', 'version']);
589
        array_unshift($document['versions'], $newest);
590
591
        $path = $this->getPathTreeDetail($project_key, $document['pt']);
592
593
        return response()->json(['ecode' => 0, 'data' => parent::arrange($document), 'options' => [ 'path' => $path ]]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of show()). Are you sure this is correct? If so, you might want to change this to $this->arrange().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
594
    }
595
596
    /**
597
     * Update the specified resource in storage.
598
     *
599
     * @param  \Illuminate\Http\Request $request
600
     * @param  string                   $project_key
601
     * @param  string                   $id
602
     * @return \Illuminate\Http\Response
603
     */
604
    public function update(Request $request, $project_key, $id)
605
    {
606
        $name =  $request->input('name');
607
        if (!isset($name) || !$name) {
608
            throw new \UnexpectedValueException('the name can not be empty.', -11952);
609
        }
610
611
        $old_document = DB::collection('wiki_' . $project_key)
612
            ->where('_id', $id)
613
            ->where('del_flag', '<>', 1)
614
            ->first();
615
        if (!$old_document) {
616
            throw new \UnexpectedValueException('the object does not exist.', -11954);
617
        }
618
619
        if (isset($old_document['d']) && $old_document['d'] === 1) {
620
            if (!$this->isPermissionAllowed($project_key, 'manage_project')) {
621
                return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
622
            }
623
        }
624 View Code Duplication
        else
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
625
        {
626
            if (isset($old_document['checkin']) && isset($old_document['checkin']['user']) && $old_document['checkin']['user']['id'] !== $this->user->id) {
627
                throw new \UnexpectedValueException('the object has been locked.', -11955);
628
            }
629
        }
630
631
        $updValues = [];
632 View Code Duplication
        if ($old_document['name'] !== $name) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
633
            $query = DB::collection('wiki_' . $project_key)
634
                ->where('parent', $old_document['parent'])
635
                ->where('name', $name)
636
                ->where('del_flag', '<>', 1);
637
638
            if (isset($old_document['d']) && $old_document['d'] === 1) {
639
                $query->where('d', 1);
640
            }
641
            else
642
            {
643
                $query->where('d', '<>', 1);
644
            }
645
646
            $isExists = $query->exists();
647
            if ($isExists) {
648
                throw new \UnexpectedValueException('the name cannot be repeated.', -11953);
649
            }
650
651
            $updValues['name'] = $name;
652
        }
653
654
        if (!isset($old_document['d']) || $old_document['d'] !== 1) {
655
            $contents = $request->input('contents');
656
            if (isset($contents) && $contents) {
657
                $updValues['contents'] = $contents;
658
            }
659
660
            if (isset($old_document['version']) && $old_document['version']) {
661
                $updValues['version'] = $old_document['version'] + 1;
662
            }
663
            else
664
            {
665
                $updValues['version'] = 2;
666
            }
667
        }
668
        
669
        $updValues['editor'] = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
670
        $updValues['updated_at'] = time();
671
        DB::collection('wiki_' . $project_key)->where('_id', $id)->update($updValues);
672
673
        // record the version
674
        if (!isset($old_document['d']) || $old_document['d'] !== 1) {
675
            // unlock the wiki
676
            DB::collection('wiki_' . $project_key)->where('_id', $id)->unset('checkin'); 
677
            // record versions 
678
            $this->recordVersion($project_key, $old_document);
679
680
            $isSendMsg = $request->input('isSendMsg') && true;
681
            Event::fire(new WikiEvent($project_key, $updValues['editor'], [ 'event_key' => 'edit_wiki', 'isSendMsg' => $isSendMsg, 'data' => [ 'wiki_id' => $id ] ]));
682
683
            return $this->show($request, $project_key, $id);
684
        }
685
        else
686
        {
687
            $document = DB::collection('wiki_' . $project_key)->where('_id', $id)->first();
688
            return response()->json(['ecode' => 0, 'data' => parent::arrange($document)]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of update()). Are you sure this is correct? If so, you might want to change this to $this->arrange().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
689
        }
690
    }
691
692
    /**
693
     * record the last version.
694
     *
695
     * @param  array $document
696
     * @return \Illuminate\Http\Response
697
     */
698
    public function recordVersion($project_key, $document)
699
    {
700
        $insValues = [];
701
702
        $insValues['wid']         = $document['_id']->__toString();
703
        $insValues['name']        = isset($document['name']) ? $document['name'] : '';
704
        $insValues['contents']    = isset($document['contents']) ? $document['contents'] : '';
705
        $insValues['version']     = isset($document['version']) ? $document['version'] : 1;
706
        $insValues['editor']      = isset($document['editor']) ? $document['editor'] : $document['creator'];
707
        $insValues['updated_at']  = isset($document['updated_at']) ? $document['updated_at'] : $document['created_at'];
708
709
        DB::collection('wiki_version_' . $project_key)->insert($insValues);
710
    }
711
712
    /**
713
     * copy the document.
714
     *
715
     * @param  \Illuminate\Http\Request $request
716
     * @param  string                   $project_key
717
     * @return \Illuminate\Http\Response
718
     */
719
    public function copy(Request $request, $project_key)
720
    {
721
        $id =  $request->input('id');
722
        if (!isset($id) || !$id) {
723
            throw new \UnexpectedValueException('the copy object can not be empty.', -11960);
724
        }
725
726
        $name =  $request->input('name');
727
        if (!isset($name) || !$name) {
728
            throw new \UnexpectedValueException('the name can not be empty.', -11952);
729
        }
730
731
        $dest_path =  $request->input('dest_path');
732
        if (!isset($dest_path)) {
733
            throw new \UnexpectedValueException('the dest directory can not be empty.', -11961);
734
        }
735
736
        $document = DB::collection('wiki_' . $project_key)
737
            ->where('_id', $id)
738
            ->where('d', '<>', 1)
739
            ->where('del_flag', '<>', 1)
740
            ->first();
741
        if (!$document) {
742
            throw new \UnexpectedValueException('the copy object does not exist.', -11962);
743
        }
744
745
        $dest_directory = [];
746
        if ($dest_path !== '0') {
747
            $dest_directory = DB::collection('wiki_' . $project_key)
748
                ->where('_id', $dest_path)
749
                ->where('d', 1)
750
                ->where('del_flag', '<>', 1)
751
                ->first();
752
            if (!$dest_directory) {
753
                throw new \UnexpectedValueException('the dest directory does not exist.', -11963);
754
            }
755
        }
756
757
        $isExists = DB::collection('wiki_' . $project_key)
758
            ->where('parent', $dest_path)
759
            ->where('name', $name)
760
            ->where('d', '<>', 1)
761
            ->where('del_flag', '<>', 1)
762
            ->exists();
763
        if ($isExists) {
764
            throw new \UnexpectedValueException('the name cannot be repeated.', -11953);
765
        }
766
767
        $insValues = [];
768
        $insValues['name'] = $name;
769
        $insValues['parent'] = $dest_path;
770
        $insValues['pt'] = array_merge(isset($dest_directory['pt']) ? $dest_directory['pt'] : [], [$dest_path]);
771
772
        //$insValues['size']    = $document['size'];
773
        //$insValues['type']    = $document['type'];
774
        //$insValues['index']   = $document['index'];
775
776
        $insValues['version'] = 1;
777
        $insValues['contents'] = isset($document['contents']) ? $document['contents'] : '';
778
        $insValues['attachments'] = isset($document['attachments']) ? $document['attachments'] : [];
779
            
780
        $insValues['creator'] = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
781
        $insValues['created_at'] = time();
782
783
        $new_id = DB::collection('wiki_' . $project_key)->insertGetId($insValues);         
784
785
        $document = DB::collection('wiki_' . $project_key)->where('_id', $new_id)->first();
786
        return response()->json(['ecode' => 0, 'data' => parent::arrange($document)]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of copy()). Are you sure this is correct? If so, you might want to change this to $this->arrange().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
787
    }
788
789
    /**
790
     * move the document.
791
     *
792
     * @param  \Illuminate\Http\Request $request
793
     * @param  string                   $project_key
794
     * @return \Illuminate\Http\Response
795
     */
796
    public function move(Request $request, $project_key)
797
    {
798
        $id =  $request->input('id');
799
        if (!isset($id) || !$id) {
800
            throw new \UnexpectedValueException('the move object can not be empty.', -11964);
801
        }
802
803
        $dest_path =  $request->input('dest_path');
804
        if (!isset($dest_path)) {
805
            throw new \UnexpectedValueException('the dest directory can not be empty.', -11965);
806
        }
807
808
        $document = DB::collection('wiki_' . $project_key)
809
            ->where('_id', $id)
810
            ->where('del_flag', '<>', 1)
811
            ->first();
812
        if (!$document) {
813
            throw new \UnexpectedValueException('the move object does not exist.', -11966);
814
        }
815
816 View Code Duplication
        if (isset($document['d']) && $document['d'] === 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
817
            if (!$this->isPermissionAllowed($project_key, 'manage_project')) {
818
                return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
819
            }
820
        }
821
822
        $dest_directory = [];
823
        if ($dest_path !== '0') {
824
            $dest_directory = DB::collection('wiki_' . $project_key)
825
                ->where('_id', $dest_path)
826
                ->where('d', 1)
827
                ->where('del_flag', '<>', 1)
828
                ->first();
829
            if (!$dest_directory) {
830
                throw new \UnexpectedValueException('the dest directory does not exist.', -11967);
831
            }
832
        }
833
834
        $isExists = DB::collection('wiki_' . $project_key)
835
            ->where('parent', $dest_path)
836
            ->where('name', $document['name'])
837
            ->where('d', isset($document['d']) && $document['d'] === 1 ? '=' : '<>', 1)
838
            ->where('del_flag', '<>', 1)
839
            ->exists();
840
        if ($isExists) {
841
            throw new \UnexpectedValueException('the name cannot be repeated.', -11953);
842
        }
843
844
        $updValues = [];
845
        $updValues['parent'] = $dest_path;
846
        $updValues['pt'] = array_merge(isset($dest_directory['pt']) ? $dest_directory['pt'] : [], [$dest_path]);
847
        DB::collection('wiki_' . $project_key)->where('_id', $id)->update($updValues);
848
849 View Code Duplication
        if (isset($document['d']) && $document['d'] === 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
850
            $subs = DB::collection('wiki_' . $project_key)
851
                ->where('pt', $id)
852
                ->where('del_flag', '<>', 1)
853
                ->get();
854
            foreach ($subs as $sub)
855
             {
856
                $pt = isset($sub['pt']) ? $sub['pt'] : [];
857
                $pind = array_search($id, $pt);
858
                if ($pind !== false) {
859
                    $tail = array_slice($pt, $pind);
860
                    $pt = array_merge($updValues['pt'], $tail);
861
                    DB::collection('wiki_' . $project_key)->where('_id', $sub['_id']->__toString())->update(['pt' => $pt]);
862
                }
863
            }
864
        }
865
866
        $document = DB::collection('wiki_' . $project_key)->where('_id', $id)->first();
867
        return response()->json(['ecode' => 0, 'data' => parent::arrange($document)]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of move()). Are you sure this is correct? If so, you might want to change this to $this->arrange().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
868
    }
869
870
    /**
871
     * Remove the specified resource from storage.
872
     *
873
     * @param  string $project_key
874
     * @param  string $id
875
     * @return \Illuminate\Http\Response
876
     */
877
    public function destroy($project_key, $id)
878
    {
879
        $document = DB::collection('wiki_' . $project_key)
880
            ->where('_id', $id)
881
            ->first();
882
        if (!$document) {
883
            throw new \UnexpectedValueException('the object does not exist.', -11954);
884
        }
885
886 View Code Duplication
        if (isset($document['d']) && $document['d'] === 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
887
            if (!$this->isPermissionAllowed($project_key, 'manage_project')) {
888
                return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
889
            }
890
        }
891
892
        DB::collection('wiki_' . $project_key)->where('_id', $id)->update([ 'del_flag' => 1 ]);
893
894 View Code Duplication
        if (isset($document['d']) && $document['d'] === 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
895
            DB::collection('wiki_' . $project_key)->whereRaw([ 'pt' => $id ])->update([ 'del_flag' => 1 ]);
896
        }
897
898
        $user = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
899
        Event::fire(new WikiEvent($project_key, $user, [ 'event_key' => 'delete_wiki', 'wiki_id' => $id ]));
900
901
        return response()->json(['ecode' => 0, 'data' => [ 'id' => $id ]]);
902
    }
903
904
    /**
905
     * Upload file.
906
     *
907
     * @param  \Illuminate\Http\Request $request
908
     * @param  String                   $project_key
909
     * @param  String                   $wid
910
     * @param  String                   $fid
0 ignored issues
show
Bug introduced by
There is no parameter named $fid. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
911
     * @return \Illuminate\Http\Response
912
     */
913
    public function upload(Request $request, $project_key, $wid)
914
    {
915
        set_time_limit(0);
916
917
        if (!is_writable(config('filesystems.disks.local.root', '/tmp'))) {
918
            throw new \UnexpectedValueException('the user has not the writable permission to the directory.', -15103);
919
        }
920
921
        $fields = array_keys($_FILES);
922
        $field = array_pop($fields);
923 View Code Duplication
        if (empty($_FILES) || $_FILES[$field]['error'] > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
924
            throw new \UnexpectedValueException('upload file errors.', -11959);
925
        }
926
927
        $document = DB::collection('wiki_' . $project_key)
928
            ->where('_id', $wid)
929
            ->where('del_flag', '<>', 1)
930
            ->first();
931
        if (!$document) {
932
            throw new \UnexpectedValueException('the object does not exist.', -11954);
933
        }
934
935
        $basename = md5(microtime() . $_FILES[$field]['name']);
936
        $sub_save_path = config('filesystems.disks.local.root', '/tmp') . '/' . substr($basename, 0, 2) . '/';
937
        if (!is_dir($sub_save_path)) {
938
            @mkdir($sub_save_path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
939
        }
940
        move_uploaded_file($_FILES[$field]['tmp_name'], $sub_save_path . $basename);
941
942
        $data = [];
943
944
        $data['name'] = $_FILES[$field]['name'];;
945
        $data['size']    = $_FILES[$field]['size'];
946
        $data['type']    = $_FILES[$field]['type'];
947
        $data['id'] = $data['index']   = $basename;
948
949
        $data['uploader'] = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
950
        $data['uploaded_at'] = time();
951
952
        $attachments = [];
953
        if (isset($document['attachments']) && $document['attachments']) {
954
            $attachments = $document['attachments'];
955
        }
956
957
        $attachments[] = $data;
958
        DB::collection('wiki_' . $project_key)->where('_id', $wid)->update([ 'attachments' => $attachments ]);
959
960
        return response()->json(['ecode' => 0, 'data' => $data]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
961
    }
962
963
    /**
964
     * remove file.
965
     *
966
     * @param  \Illuminate\Http\Request $request
967
     * @param  String                   $project_key
968
     * @param  String                   $wid
969
     * @return \Illuminate\Http\Response
970
     */
971
    public function remove(Request $request, $project_key, $wid, $fid)
972
    {
973
        $document = DB::collection('wiki_' . $project_key)
974
            ->where('_id', $wid)
975
            ->where('del_flag', '<>', 1)
976
            ->first();
977
        if (!$document) {
978
            throw new \UnexpectedValueException('the object does not exist.', -11954);
979
        }
980
981 View Code Duplication
        if (!isset($document['attachments']) || !$document['attachments']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
982
            throw new \UnexpectedValueException('the file does not exist.', -11958);
983
        }
984
985
        $new_attachments = [];
986
        foreach ($document['attachments'] as $a)
987
        {
988
            if ($a['id'] !== $fid) {
989
                $new_attachments[] = $a;
990
            }
991
        }
992
993
        DB::collection('wiki_' . $project_key)->where('_id', $wid)->update([ 'attachments' => $new_attachments ]);
994
        return response()->json(['ecode' => 0, 'data' => [ 'id' => $fid ]]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
995
    }
996
997
    /**
998
     * Download file or directory.
999
     *
1000
     * @param  \Illuminate\Http\Request $request
1001
     * @param  String                   $project_key
1002
     * @param  String                   $wid
1003
     * @return \Illuminate\Http\Response
1004
     */
1005
    public function download2(Request $request, $project_key, $wid)
1006
    {
1007
        set_time_limit(0);
1008
1009
        $document = DB::collection('wiki_' . $project_key)
1010
            ->where('_id', $wid)
1011
            ->where('del_flag', '<>', 1)
1012
            ->first();
1013
        if (!$document) {
1014
            throw new \UnexpectedValueException('the object does not exist.', -11954);
1015
        }
1016
1017 View Code Duplication
        if (!isset($document['attachments']) || !$document['attachments']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1018
            throw new \UnexpectedValueException('the file does not exist.', -11958);
1019
        }
1020
1021
        $this->downloadFolder($document['name'], $document['attachments']);
1022
    }
1023
1024
    /**
1025
     * Download file or directory.
1026
     *
1027
     * @param  \Illuminate\Http\Request $request
1028
     * @param  String                   $project_key
1029
     * @param  String                   $wid
1030
     * @param  String                   $fid
1031
     * @return \Illuminate\Http\Response
1032
     */
1033
    public function download(Request $request, $project_key, $wid, $fid)
1034
    {
1035
        set_time_limit(0);
1036
1037
        $document = DB::collection('wiki_' . $project_key)
1038
            ->where('_id', $wid)
1039
            ->where('del_flag', '<>', 1)
1040
            ->first();
1041
        if (!$document) {
1042
            throw new \UnexpectedValueException('the object does not exist.', -11954);
1043
        }
1044
1045 View Code Duplication
        if (!isset($document['attachments']) || !$document['attachments']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1046
            throw new \UnexpectedValueException('the file does not exist.', -11958);
1047
        }
1048
1049
        $isExists = false;
1050
        foreach ($document['attachments'] as $file)
1051
        {
1052
            if (isset($file['id']) && $file['id'] === $fid) {
1053
                $isExists = true;
1054
                break;
1055
            }
1056
        }
1057
1058
        if (!$isExists) {
1059
            throw new \UnexpectedValueException('the file does not exist.', -11958);
1060
        }
1061
1062
        $this->downloadFile($file['name'], $file['index']);
0 ignored issues
show
Bug introduced by
The variable $file seems to be defined by a foreach iteration on line 1050. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
1063
    }
1064
1065
    /**
1066
     * Download file.
1067
     *
1068
     * @param  String $name
1069
     * @param  array  $attachments
1070
     * @return \Illuminate\Http\Response
1071
     */
1072
    public function downloadFolder($name, $attachments)
1073
    {
1074
        setlocale(LC_ALL, 'zh_CN.UTF-8'); 
1075
1076
        $basepath = '/tmp/' . md5($this->user->id . microtime());
1077
        @mkdir($basepath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1078
1079
        $fullpath = $basepath . '/' . $name;
1080
        @mkdir($fullpath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1081
1082
        foreach ($attachments as $attachment)
1083
        {
1084
            $filepath = config('filesystems.disks.local.root', '/tmp') . '/' . substr($attachment['index'], 0, 2);
1085
            $filename = $filepath . '/' . $attachment['index'];
1086
            if (file_exists($filename)) {
1087
                @copy($filename, $fullpath . '/' . $attachment['name']);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1088
            }
1089
        }
1090
1091
        $fname = $basepath . '/' . $name . '.zip';
1092
        Zipper::make($fname)->folder($name)->add($basepath . '/' . $name);
1093
        Zipper::close();
1094
1095
        File::download($fname, $name . '.zip');
1096
1097
        exec('rm -rf ' . $basepath);
1098
    }
1099
1100
    /**
1101
     * Download file.
1102
     *
1103
     * @param  String $name
1104
     * @param  String $index
1105
     * @return \Illuminate\Http\Response
1106
     */
1107 View Code Duplication
    public function downloadFile($name, $index)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1108
    {
1109
        $filepath = config('filesystems.disks.local.root', '/tmp') . '/' . substr($index, 0, 2);
1110
        $filename = $filepath . '/' . $index;
1111
        if (!file_exists($filename)) {
1112
            throw new \UnexpectedValueException('file does not exist.', -11958);
1113
        }
1114
1115
        File::download($filename, $name);
1116
    }
1117
1118
    /**
1119
     * favorite action.
1120
     *
1121
     * @param  string $project_key
1122
     * @param  string $id
1123
     * @return \Illuminate\Http\Response
1124
     */
1125 View Code Duplication
    public function favorite(Request $request, $project_key, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1126
    {
1127
        $document = DB::collection('wiki_' . $project_key)
1128
            ->where('_id', $id)
1129
            ->where('del_flag', '<>', 1)
1130
            ->first();
1131
        if (!$document) {
1132
            throw new \UnexpectedValueException('the object does not exist.', -11954);
1133
        }
1134
1135
        WikiFavorites::where('wid', $id)->where('user.id', $this->user->id)->delete();
1136
1137
        $cur_user = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
1138
1139
        $flag = $request->input('flag');
1140
        if (isset($flag) && $flag) {
1141
            WikiFavorites::create([ 'project_key' => $project_key, 'wid' => $id, 'user' => $cur_user ]);
1142
        }
1143
1144
        return response()->json(['ecode' => 0, 'data' => ['id' => $id, 'user' => $cur_user, 'favorited' => $flag]]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
1145
    }
1146
}
1147