BaseCriteria::apply()   C
last analyzed

Complexity

Conditions 21
Paths 60

Size

Total Lines 72
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 47
nc 60
nop 2
dl 0
loc 72
rs 5.4212
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
namespace PHPHub\Repositories\Criteria;
4
5
use Prettus\Repository\Contracts\RepositoryInterface;
6
use Prettus\Repository\Criteria\RequestCriteria;
7
8
/**
9
 * Created by PhpStorm.
10
 * User: xuan
11
 * Date: 9/22/15
12
 * Time: 8:05 AM.
13
 */
14
abstract class BaseCriteria extends RequestCriteria
15
{
16
    public function apply($builder, RepositoryInterface $repository)
17
    {
18
        $fieldsSearchable = $repository->getFieldsSearchable();
19
        $search = $this->request->get(config('repository.criteria.params.search', 'search'), null);
20
        $searchFields = $this->request->get(config('repository.criteria.params.searchFields', 'searchFields'), null);
21
        $filter = $this->request->get(config('repository.criteria.params.filter', 'filter'), null);
22
        $orderBy = $this->request->get(config('repository.criteria.params.orderBy', 'orderBy'), null);
23
        $sortedBy = $this->request->get(config('repository.criteria.params.sortedBy', 'sortedBy'), 'asc');
24
        $sortedBy = ! empty($sortedBy) ? $sortedBy : 'asc';
25
26
        if ($search && is_array($fieldsSearchable) && count($fieldsSearchable)) {
27
            $searchFields = is_array($searchFields) || is_null($searchFields) ? $searchFields : explode(';',
28
                $searchFields);
29
            $fields = $this->parserFieldsSearch($fieldsSearchable, $searchFields);
30
            $isFirstField = true;
31
            $searchData = $this->parserSearchData($search);
32
            $search = $this->parserSearchValue($search);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $search is correct as $this->parserSearchValue($search) (which targets Prettus\Repository\Crite...ia::parserSearchValue()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
33
            $modelForceAndWhere = false;
34
35
            $builder = $builder->where(function ($query) use (
36
                $fields,
37
                $search,
38
                $searchData,
39
                $isFirstField,
40
                $modelForceAndWhere
41
            ) {
42
                foreach ($fields as $field => $condition) {
43
                    if (is_numeric($field)) {
44
                        $field = $condition;
45
                        $condition = '=';
46
                    }
47
48
                    $value = null;
49
50
                    $condition = trim(strtolower($condition));
51
52
                    if (isset($searchData[$field])) {
53
                        $value = $condition === 'like' ? "%{$searchData[$field]}%" : $searchData[$field];
54
                    } else {
55
                        if (! is_null($search)) {
56
                            $value = $condition === 'like' ? "%{$search}%" : $search;
57
                        }
58
                    }
59
60
                    if ($isFirstField || $modelForceAndWhere) {
61
                        if (! is_null($value)) {
62
                            $query->where($field, $condition, $value);
63
                            $isFirstField = false;
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $isFirstField, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
64
                        }
65
                    } else {
66
                        if (! is_null($value)) {
67
                            $query->orWhere($field, $condition, $value);
68
                        }
69
                    }
70
                }
71
            });
72
        }
73
74
        if (isset($orderBy) && in_array(strtolower($sortedBy), ['asc', 'desc'])) {
75
            $builder = $builder->orderBy($orderBy, $sortedBy);
76
        }
77
78
        foreach (FilterManager::get() as $filter) {
79
            // eg. filter 'hot' 会调用方法 'filterHot'
80
            $method_name = camel_case('filter_'.$filter);
81
            if (method_exists($this, $method_name)) {
82
                $builder = call_user_func([$this, $method_name], $builder);
83
            }
84
        }
85
86
        return $builder;
87
    }
88
}
89