Completed
Push — master ( 4b2726...162df7 )
by Hong
03:18
created

OrderByTrait::multipleOrderBy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Query
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Query\Traits\Clause;
16
17
use Phossa2\Query\Misc\Template;
18
use Phossa2\Query\Interfaces\Clause\OrderByInterface;
19
20
/**
21
 * OrderByTrait
22
 *
23
 * Implementation of OrderByInterface
24
 *
25
 * @package Phossa2\Query
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     OrderByInterface
28
 * @version 2.0.0
29
 * @since   2.0.0 added
30
 */
31
trait OrderByTrait
32
{
33
    /**
34
     * {@inheritDoc}
35
     */
36
    public function orderBy($col)
37
    {
38
        return $this->realOrderBy($col, 'ASC');
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    public function orderByDesc($col)
45
    {
46
        return $this->realOrderBy($col, 'DESC');
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function orderByTpl(/*# string */ $template, $col)
53
    {
54
        return $this->realOrderBy(new Template($template, $col), '', true);
0 ignored issues
show
Documentation introduced by
new \Phossa2\Query\Misc\Template($template, $col) is of type object<Phossa2\Query\Misc\Template>, but the function expects a string|array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function orderByRaw(/*# string */ $rawString)
61
    {
62
        return $this->realOrderBy($rawString, '',  true);
63
    }
64
65
    /**
66
     * @param  string|string[] $col
67
     * @param  string $suffix 'ASC' or 'DESC'
68
     * @param  bool $rawMode
69
     * @return $this
70
     * @access protected
71
     */
72 View Code Duplication
    protected function realOrderBy(
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
        $col,
74
        /*# sting */ $suffix = 'ASC',
75
        /*# bool */ $rawMode = false
76
    ) {
77
        if (is_array($col)) {
78
            $this->multipleOrderBy($col, $suffix);
79
        } else {
80
            $clause = &$this->getClause('ORDER BY');
81
            $clause[] = [$col, $suffix, $this->isRaw($col, $rawMode)];
82
        }
83
        return $this;
84
    }
85
86
    /**
87
     * Multitple orderbys
88
     *
89
     * @param  array $cols
90
     * @param  string $suffix 'ASC' or 'DESC'
91
     * @access protected
92
     */
93
    protected function multipleOrderBy(array $cols, /*# sting */ $suffix)
94
    {
95
        foreach ($cols as $col) {
96
            $this->realOrderBy($col, $suffix);
97
        }
98
    }
99
100
    /**
101
     * Build ORDER BY
102
     *
103
     * @param  array $settings
104
     * @return string
105
     * @access protected
106
     */
107
    protected function buildOrderby(array $settings)/*# : string */
108
    {
109
        $result = [];
110
        $clause = &$this->getClause('ORDER BY');
111
        foreach ($clause as $ord) {
112
            $item = $this->quoteItem($ord[0], $settings, $ord[2]);
113
            $ordr = $ord[1] ? (' ' . $ord[1]) : '';
114
            $result[] =  $item . $ordr;
115
        }
116
        return $this->joinClause('ORDRE BY', ',', $result, $settings);
117
    }
118
119
    abstract protected function isRaw($str, /*# bool */ $rawMode)/*# : bool */;
120
    abstract protected function &getClause(/*# string */ $clauseName)/*# : array */;
121
    abstract protected function quoteItem($item, array $settings, /*# bool */ $rawMode = false)/*# : string */;
122
    abstract protected function joinClause(
123
        /*# : string */ $prefix,
124
        /*# : string */ $seperator,
125
        array $clause,
126
        array $settings
127
    )/*# : string */;
128
}
129