Completed
Push — master ( be2280...4a4169 )
by Andrii
01:43
created

Specification::requestRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * HiAPI Yii2 base project for building API
4
 *
5
 * @link      https://github.com/hiqdev/hiapi
6
 * @package   hiapi
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiapi\query;
12
13
use yii\db\QueryTrait;
14
15
class Specification
16
{
17
    use QueryTrait;
18
19
    public $requestedRelations = [];
20
21
    public function requestRelation($name)
22
    {
23
        $this->requestedRelations[$name] = true; // todo: specification for relation
24
    }
25
26
    /**
27
     * @param Query $query
28
     * @return Query
29
     */
30
    public function applyTo($query)
31
    {
32
        if ($this->where) {
33
            $this->applyWhereTo($query);
34
        }
35
36
        if ($this->limit) {
37
            $query->limit($this->limit);
38
        }
39
40
        return $query;
41
    }
42
43
    /**
44
     * @param Query $query
45
     */
46
    public function applyWhereTo($query)
47
    {
48
        foreach ($this->where as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $this->where of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
49
            foreach ($query->getFields() as $field) {
50
                if ($field->isApplicable($key)) {
51
                    $query->andWhere($field->buildCondition($value));
52
                }
53
            }
54
        }
55
    }
56
}
57