Completed
Push — master ( 439406...4b2726 )
by Hong
03:09
created

OrderByTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 15.29 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 0
dl 13
loc 85
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A orderBy() 0 4 1
A orderByDesc() 0 4 1
A orderByTpl() 0 4 1
A orderByRaw() 0 4 1
A realOrderBy() 13 13 2
A buildOrderby() 0 11 3
isRaw() 0 1 ?
clauseTpl() 0 1 ?
getClause() 0 1 ?
quoteItem() 0 1 ?
joinClause() 0 6 ?

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Interfaces\Clause\OrderByInterface;
18
19
/**
20
 * OrderByTrait
21
 *
22
 * Implementation of OrderByInterface
23
 *
24
 * @package Phossa2\Query
25
 * @author  Hong Zhang <[email protected]>
26
 * @see     OrderByInterface
27
 * @version 2.0.0
28
 * @since   2.0.0 added
29
 */
30
trait OrderByTrait
31
{
32
    /**
33
     * {@inheritDoc}
34
     */
35
    public function orderBy($col)
36
    {
37
        return $this->realOrderBy($col, 'ASC');
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function orderByDesc($col)
44
    {
45
        return $this->realOrderBy($col, 'DESC');
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function orderByTpl(/*# string */ $template, $col)
52
    {
53
        return $this->realOrderBy($this->clauseTpl($template, $col), '', true);
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    public function orderByRaw(/*# string */ $rawString)
60
    {
61
        return $this->realOrderBy($rawString, '',  true);
62
    }
63
64
    /**
65
     * @param  string|string[] $col
66
     * @param  string $suffix 'ASC' or 'DESC'
67
     * @param  bool $rawMode
68
     * @return $this
69
     * @access protected
70
     */
71 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...
72
        $col,
73
        /*# sting */ $suffix = 'ASC',
74
        /*# bool */ $rawMode = false
75
    ) {
76
        if (is_array($col)) {
77
            $this->multipleOrderBy($col);
0 ignored issues
show
Bug introduced by
The method multipleOrderBy() does not exist on Phossa2\Query\Traits\Clause\OrderByTrait. Did you maybe mean orderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
78
        } else {
79
            $clause = &$this->getClause('ORDER BY');
80
            $clause[] = [$col, $suffix, $this->isRaw($col, $rawMode)];
81
        }
82
        return $this;
83
    }
84
85
    /**
86
     * Build ORDER BY
87
     *
88
     * @param  array $settings
89
     * @return string
90
     * @access protected
91
     */
92
    protected function buildOrderby(array $settings)/*# : string */
93
    {
94
        $result = [];
95
        $clause = &$this->getClause('ORDER BY');
96
        foreach ($clause as $ord) {
97
            $item = $this->quoteItem($ord[0], $ord[2]);
98
            $ordr = $ord[1] ? (' ' . $ord[1]) : '';
99
            $result[] =  $item . $ordr;
100
        }
101
        return $this->joinClause('ORDRE BY', ',', $result, $settings);
102
    }
103
104
    abstract protected function isRaw($str, /*# bool */ $rawMode)/*# : bool */;
105
    abstract protected function clauseTpl(/*# string */ $template, $col)/*# : string */;
106
    abstract protected function &getClause(/*# string */ $clauseName)/*# : array */;
107
    abstract protected function quoteItem($item, /*# bool */ $rawMode = false)/*# : string */;
108
    abstract protected function joinClause(
109
        /*# : string */ $prefix,
110
        /*# : string */ $seperator,
111
        array $clause,
112
        array $settings
113
    )/*# : string */;
114
}
115