QueryTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 39
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A range() 0 10 3
A likeCondition() 0 10 4
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 yii\db\ActiveQuery;
16
17
/**
18
 * This trait attach two base conditions.
19
 * 
20
 * @version 1.0
21
 * @author vistart <[email protected]>
22
 */
23
trait QueryTrait
24
{
25
    /**
26
     * Attach like condition.
27
     * @param mixed $value
28
     * @param string $attribute
29
     * @param string|false $like false, 'like', 'or like', 'not like', 'or not like'.
30
     * @return $this
31
     */
32 30
    protected function likeCondition($value, $attribute, $like = false)
33
    {
34 30
        if (!is_string($attribute) || empty($attribute)) {
35 2
            return $this;
36
        }
37 30
        if ($like !== false) {
38 8
            return $this->andWhere([$like, $attribute, $value]);
0 ignored issues
show
Bug introduced by
It seems like andWhere() 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...
39
        }
40 30
        return $this->andWhere([$attribute => $value]);
0 ignored issues
show
Bug introduced by
It seems like andWhere() 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...
41
    }
42
43
    /**
44
     * Specify range with $attribute to $query.
45
     * @param ActiveQuery $query
46
     * @param string $attribute
47
     * @param string $start
48
     * @param string $end
49
     * @return $query
0 ignored issues
show
Documentation introduced by
The doc-type $query could not be parsed: Unknown type name "$query" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
50
     */
51 4
    protected static function range($query, $attribute, $start = null, $end = null)
52
    {
53 4
        if (!empty($start)) {
54 4
            $query = $query->andWhere(['>=', $attribute, $start]);
55
        }
56 4
        if (!empty($end)) {
57 4
            $query = $query->andWhere(['<', $attribute, $end]);
58
        }
59 4
        return $query;
60
    }
61
}