Passed
Push — master ( bd06cb...6b2bf4 )
by vistart
09:16
created

EntityQueryTrait::guidOrId()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.2
cc 4
eloc 5
nc 2
nop 2
crap 20
1
<?php
2
3
/**
4
 *   _   __ __ _____ _____ ___  ____  _____
5
 *  | | / // // ___//_  _//   ||  __||_   _|
6
 *  | |/ // /(__  )  / / / /| || |     | |
7
 *  |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\base\models\traits;
14
15
use rhosocial\base\helpers\Number;
16
17
/**
18
 * This trait is used for building entity query class for entity model.
19
 *
20
 * @version 1.0
21
 * @author vistart <[email protected]>
22
 */
23
trait EntityQueryTrait
24
{
25
    use QueryTrait;
26
27
    public $noInitModel;
28
29
    /**
30
     * Build model without any initializations.
31
     */
32 359
    public function buildNoInitModel()
33
    {
34 359
        if (empty($this->noInitModel) && is_string($this->modelClass)) {
35
            $modelClass = $this->modelClass;
0 ignored issues
show
Bug introduced by
The property modelClass does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
            $this->noInitModel = $modelClass::buildNoInitModel();
37
        }
38 359
    }
39
40
    /**
41
     * Specify guid attribute.
42
     * @param string|array $guid
43
     * @param false|string $like false, 'like', 'or like', 'not like', 'or not like'.
44
     * @return $this
45
     */
46 16
    public function guid($guid, $like = false)
47
    {
48 16
        $model = $this->noInitModel;
49 16
        return $this->likeCondition((string)$guid, $model->guidAttribute, $like);
50
    }
51
52
    /**
53
     * Specify id attribute.
54
     * @param string|integer|array $id
55
     * @param false|string $like false, 'like', 'or like', 'not like', 'or not like'.
56
     * @return $this
57
     */
58 9
    public function id($id, $like = false)
59
    {
60 9
        $model = $this->noInitModel;
61 9
        return $this->likeCondition($id, $model->idAttribute, $like);
62
    }
63
64
    /**
65
     * Specify GUID or ID attribute.
66
     * Scalar parameter is acceptable only.
67
     * Please do not pass an array to the first parameter.
68
     * @param string|integer $param
69
     * @param bool|string $like false, 'like', 'or like', 'not like', 'or not like'.
70
     * @return $this
71
     */
72
    public function guidOrId($param, $like = false)
73
    {
74
        $model = $this->noInitModel;
0 ignored issues
show
Unused Code introduced by
$model 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...
75
        if (is_string($param) && (preg_match(Number::GUID_REGEX, $param) || strlen($param) == 16)) {
76
            return $this->guid($param, $like);
0 ignored issues
show
Bug introduced by
It seems like $like defined by parameter $like on line 72 can also be of type boolean; however, rhosocial\base\models\tr...ntityQueryTrait::guid() does only seem to accept false|string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
77
        }
78
        return $this->id($param, $like);
0 ignored issues
show
Bug introduced by
It seems like $like defined by parameter $like on line 72 can also be of type boolean; however, rhosocial\base\models\tr...\EntityQueryTrait::id() does only seem to accept false|string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
79
    }
80
81
    /**
82
     * Specify create time range.
83
     * @param string $start
84
     * @param string $end
85
     * @return $this
86
     */
87 6
    public function createdAt($start = null, $end = null)
88
    {
89 6
        $model = $this->noInitModel;
90
        /* @var $this yii\db\ActiveQuery */
91 6
        if (!is_string($model->createdAtAttribute) || empty($model->createdAtAttribute)) {
92
            return $this;
93
        }
94 6
        return static::range($this, $model->createdAtAttribute, $start, $end);
95
    }
96
97
    /**
98
     * Specify order by creation time.
99
     * @param string $sort only 'SORT_ASC' and 'SORT_DESC' are acceptable.
100
     * @return $this
101
     */
102 3
    public function orderByCreatedAt($sort = SORT_ASC)
103
    {
104 3
        $model = $this->noInitModel;
105
        /* @var $this yii\db\ActiveQuery */
106 3
        if (!is_string($model->createdAtAttribute) || empty($model->createdAtAttribute)) {
107
            return $this;
108
        }
109 3
        return $this->addOrderBy([$model->createdAtAttribute => $sort]);
110
    }
111
112
    /**
113
     * Specify update time range.
114
     * @param string $start 
115
     * @param string $end
116
     * @return $this
117
     */
118 7
    public function updatedAt($start = null, $end = null)
119
    {
120 7
        $model = $this->noInitModel;
121
        /* @var $this yii\db\ActiveQuery */
122 7
        if (!is_string($model->updatedAtAttribute) || empty($model->updatedAtAttribute)) {
123
            return $this;
124
        }
125 7
        return static::range($this, $model->updatedAtAttribute, $start, $end);
126
    }
127
128
    /**
129
     * Specify order by update time.
130
     * @param string $sort only 'SORT_ASC' and 'SORT_DESC' are acceptable.
131
     * @return $this
132
     */
133 2
    public function orderByUpdatedAt($sort = SORT_ASC)
134
    {
135 2
        $model = $this->noInitModel;
136
        /* @var $this yii\db\ActiveQuery */
137 2
        if (!is_string($model->updatedAtAttribute) || empty($model->updatedAtAttribute)) {
138
            return $this;
139
        }
140 2
        return $this->addOrderBy([$model->updatedAtAttribute => $sort]);
141
    }
142
143
    public static $pageAll = 'all';
144
    public static $defaultPageSize = 10;
145
    
146
    /**
147
     * Specify page condition.
148
     * @param string|int $pageSize It will return all models if it is 'all',
149
     * or it will be regarded as sum of models.
150
     * @param int $currentPage The current page number if it is integer begun with 0.
151
     * @return $this
152
     */
153 3
    public function page($pageSize = 10, $currentPage = 0)
154
    {
155 3
        if ($pageSize === static::$pageAll) {
156 2
            return $this;
157
        }
158
        /* normalize $currentPage and $currentPage */
159 1
        if (!is_numeric($currentPage) || $currentPage < 0) {
160 1
            $currentPage = 0;
161
        }
162 1
        $currentPage = (int) $currentPage;
163 1
        if (!is_numeric($pageSize) || $pageSize < 1) {
164 1
            $pageSize = static::$defaultPageSize;
165
        }
166 1
        $pageSize = (int) $pageSize;
167 1
        return $this->limit($pageSize)->offset($pageSize * $currentPage);
0 ignored issues
show
Bug introduced by
It seems like limit() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
168
    }
169
}
170