Passed
Pull Request — master (#106)
by Arman
02:51
created

Reducer::selectPatch()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 17
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.8.0
13
 */
14
15
namespace Quantum\Libraries\Database\Sleekdb\Statements;
16
17
use Quantum\Libraries\Database\DbalInterface;
18
use RecursiveIteratorIterator;
19
use RecursiveArrayIterator;
20
21
/**
22
 * Trait Modifier
23
 * @package Quantum\Libraries\Database\Idiorm\Statements
24
 */
25
trait Reducer
26
{
27
28
    /**
29
     * @inheritDoc
30
     */
31
    public function select(...$columns): DbalInterface
32
    {
33
        array_walk($columns, function (&$column) {
34
            if (is_array($column)) {
35
                $column = array_flip($column);
36
            }
37
        });
38
39
        $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($columns));
40
        $columns = iterator_to_array($iterator, true);
41
42
        $this->selected = $this->selectPatch($columns);
0 ignored issues
show
Bug Best Practice introduced by
The property selected does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
44
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...ekdb\Statements\Reducer which is incompatible with the type-hinted return Quantum\Libraries\Database\DbalInterface.
Loading history...
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public function groupBy(string $column): DbalInterface
51
    {
52
        $this->grouped[] = $column;
0 ignored issues
show
Bug Best Practice introduced by
The property grouped does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
53
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...ekdb\Statements\Reducer which is incompatible with the type-hinted return Quantum\Libraries\Database\DbalInterface.
Loading history...
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59
    public function orderBy(string $column, string $direction): DbalInterface
60
    {
61
        $this->ordered[$column] = $direction;
0 ignored issues
show
Bug Best Practice introduced by
The property ordered does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
62
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...ekdb\Statements\Reducer which is incompatible with the type-hinted return Quantum\Libraries\Database\DbalInterface.
Loading history...
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function offset(int $offset): DbalInterface
69
    {
70
        $this->offset = $offset;
0 ignored issues
show
Bug Best Practice introduced by
The property offset does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
71
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...ekdb\Statements\Reducer which is incompatible with the type-hinted return Quantum\Libraries\Database\DbalInterface.
Loading history...
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77
    public function limit(int $limit): DbalInterface
78
    {
79
        $this->limit = $limit;
0 ignored issues
show
Bug Best Practice introduced by
The property limit does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
80
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...ekdb\Statements\Reducer which is incompatible with the type-hinted return Quantum\Libraries\Database\DbalInterface.
Loading history...
81
    }
82
83
    /**
84
     * @param array $columns
85
     * @return array
86
     */
87
    private function selectPatch(array $columns): array
88
    {
89
        foreach ($columns as &$column) {
90
            $exploded = explode('.', $column);
91
92
            if (count($exploded) == 1) {
93
                $column = $exploded[0];
94
            } else {
95
                if ($exploded[0] == $this->getTable()) {
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
                if ($exploded[0] == $this->/** @scrutinizer ignore-call */ getTable()) {
Loading history...
96
                    $column = $exploded[1];
97
                } else {
98
                    $column = $exploded[0] . '.0.' . $exploded[1];
99
                }
100
            }
101
        }
102
103
        return $columns;
104
    }
105
106
107
}