GroupTrait   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 102
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 102
loc 102
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A group() 8 8 2
A groupDesc() 8 8 2
A groupTpl() 5 5 1
A groupRaw() 5 5 1
A realGroup() 18 18 3
A multipleGroup() 6 6 2
A buildGroup() 6 6 1

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\Misc\Template;
18
use Phossa2\Query\Interfaces\Clause\GroupInterface;
19
20
/**
21
 * GroupTrait
22
 *
23
 * @package Phossa2\Query
24
 * @author  Hong Zhang <[email protected]>
25
 * @see     GroupInterface
26
 * @version 2.0.0
27
 * @since   2.0.0 added
28
 */
29 View Code Duplication
trait GroupTrait
0 ignored issues
show
Duplication introduced by
This class 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...
30
{
31
    use AbstractTrait;
32
33
    /**
34
     * {@inheritDoc}
35
     */
36
    public function group($col)
37
    {
38
        // support multiple group by
39
        if (func_num_args() > 1) {
40
            $col = func_get_args();
41
        }
42
        return $this->realGroup($col);
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    public function groupDesc($col)
49
    {
50
        // support multiple group by
51
        if (func_num_args() > 1) {
52
            $col = func_get_args();
53
        }
54
        return $this->realGroup($col, 'DESC');
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function groupTpl(/*# string */ $template, $col, array $params = [])
61
    {
62
        $template = $this->positionedParam($template, $params);
63
        return $this->realGroup(new Template($template, $col), '', true);
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69
    public function groupRaw(/*# string */ $rawString, array $params = [])
70
    {
71
        $rawString = $this->positionedParam($rawString, $params);
72
        return $this->realGroup($rawString, '', true);
73
    }
74
75
    /**
76
     * real group by
77
     * @param  string|string[]|Template $col column[s]
78
     * @param  string $suffix ''|ASC|DESC
79
     * @param  bool $rawMode
80
     * @return $this
81
     * @access protected
82
     */
83
    protected function realGroup(
84
        $col,
85
        /*# sting */ $suffix = '',
86
        /*# bool */ $rawMode = false
87
    ) {
88
89
        if (is_array($col)) {
90
            $this->multipleGroup($col, $suffix);
91
        } else {
92
            $clause = &$this->getClause('GROUP BY');
93
            $part = [$col, $this->isRaw($col, $rawMode)];
94
            if (!empty($suffix)) {
95
                $part[] = $suffix;
96
            }
97
            $clause[] = $part;
98
        }
99
        return $this;
100
    }
101
102
    /**
103
     * Multitple groupbys
104
     *
105
     * @param  array $cols
106
     * @param  string $suffix
107
     * @access protected
108
     */
109
    protected function multipleGroup(array $cols, /*# string */ $suffix)
110
    {
111
        foreach ($cols as $col) {
112
            $this->realGroup($col, $suffix);
113
        }
114
    }
115
116
    /**
117
     * Build GROUP BY
118
     *
119
     * @param  string $prefix
120
     * @param  array $settings
121
     * @return string
122
     * @access protected
123
     */
124
    protected function buildGroup(
125
        /*# string */ $prefix,
126
        array $settings
127
    )/*# : string */ {
128
        return $this->buildClause('GROUP BY', $prefix, $settings);
129
    }
130
}
131