Completed
Push — master ( 63793c...96fdaa )
by vistart
06:04
created

QueryTrait::likeCondition()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 3
eloc 6
nc 3
nop 3
crap 3.0416
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
/**
16
 * This trait attach two base condition.
17
 *
18
 * @version 2.0
19
 * @author vistart <[email protected]>
20
 */
21
trait QueryTrait
22
{
23
24
    /**
25
     * Attach like condition.
26
     * @param mixed $value
27
     * @param string $attribute
28
     * @param string|false $like
29
     * @return $this
30
     */
31 15
    protected function likeCondition($value, $attribute, $like = false)
32
    {
33 15
        if (!is_string($attribute)) {
34
            return $this;
35
        }
36 15
        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...
37 3
            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...
38
        }
39 14
        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...
40
    }
41
42
    /**
43
     * Specify range wilth $attribute to $query.
44
     * @param \yii\db\ActiveQuery $query
45
     * @param string $attribute
46
     * @param string $start
47
     * @param string $end
48
     * @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...
49
     */
50 1
    protected static function range($query, $attribute, $start = null, $end = null)
51
    {
52 1
        if (!empty($start)) {
53 1
            $query = $query->andWhere(['>=', $attribute, $start]);
54 1
        }
55 1
        if (!empty($end)) {
56 1
            $query = $query->andWhere(['<', $attribute, $end]);
57 1
        }
58 1
        return $query;
59
    }
60
}
61