SearchDocumentsRequest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 31.75 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 20
loc 63
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B sanitize() 20 35 7
A authorize() 0 4 1
A rules() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Colligator\Http\Requests;
4
5
class SearchDocumentsRequest extends Request
6
{
7
    public $warnings = [];
8
9
    /**
10
     * Sanitize/standardize input.
11
     */
12
    public function sanitize()
13
    {
14
15
        // TODO: Move to some config file
16
        $maxResultsPerRequest = 1000;
17
        $maxPaginationDepth = 10000;
18
19
        $input = $this->all();
20
21 View Code Duplication
        if ($this->has('offset')) {
22
            $input['offset'] = intval($input['offset']);
23
            if ($input['offset'] < 0) {
24
                unset($input['offset']);
25
                $this->warnings[] = 'Offset cannot be negative.';
26
            } elseif ($input['offset'] > $maxPaginationDepth) {
27
                $input['offset'] = $maxPaginationDepth;
28
                $this->warnings[] = 'Pagination depth is limited to ' . $maxPaginationDepth . ' results.';
29
            }
30
        }
31
32 View Code Duplication
        if ($this->has('limit')) {
33
            $input['limit'] = intval($input['limit']);
34
            if ($input['limit'] < 1) {
35
                unset($input['limit']);
36
                $this->warnings[] = 'Limit cannot be negative.';
37
            } elseif ($input['limit'] > $maxResultsPerRequest) {
38
                $input['limit'] = $maxResultsPerRequest;
39
                $this->warnings[] = 'Limiting to max ' . $maxResultsPerRequest . ' results per request.';
40
            }
41
        }
42
43
        $this->replace($input);
44
45
        return $this->all();
46
    }
47
48
    /**
49
     * Determine if the user is authorized to make this request.
50
     *
51
     * @return bool
52
     */
53
    public function authorize()
54
    {
55
        return true;
56
    }
57
58
    /**
59
     * Get the validation rules that apply to the request.
60
     *
61
     * @return array
62
     */
63
    public function rules()
64
    {
65
        return [];
66
    }
67
}
68