QueryTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 6
c 5
b 0
f 0
lcom 0
cbo 1
dl 0
loc 40
ccs 14
cts 14
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A likeCondition() 0 10 3
A range() 0 10 3
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link http://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license http://vistart.name/license/
11
 */
12
13
namespace vistart\Models\traits;
14
15
use yii\db\ActiveQuery;
16
17
/**
18
 * This trait attach two base condition.
19
 *
20
 * @version 2.0
21
 * @author vistart <[email protected]>
22
 */
23
trait QueryTrait
24
{
25
26
    /**
27
     * Attach like condition.
28
     * @param mixed $value
29
     * @param string $attribute
30
     * @param string|false $like
31
     * @return $this
32
     */
33 21
    protected function likeCondition($value, $attribute, $like = false)
34
    {
35 21
        if (!is_string($attribute)) {
36 2
            return $this;
37
        }
38 21
        if ($like) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $like of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
39 5
            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...
40
        }
41 20
        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...
42
    }
43
44
    /**
45
     * Specify range wilth $attribute to $query.
46
     * @param ActiveQuery $query
47
     * @param string $attribute
48
     * @param string $start
49
     * @param string $end
50
     * @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...
51
     */
52 1
    protected static function range($query, $attribute, $start = null, $end = null)
53
    {
54 1
        if (!empty($start)) {
55 1
            $query = $query->andWhere(['>=', $attribute, $start]);
56 1
        }
57 1
        if (!empty($end)) {
58 1
            $query = $query->andWhere(['<', $attribute, $end]);
59 1
        }
60 1
        return $query;
61
    }
62
}
63