Completed
Push — master ( 1857e6...d96bec )
by Joao
03:02
created

DBMySQLFunctions   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 181
Duplicated Lines 20.99 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 32
c 6
b 1
f 0
lcom 1
cbo 4
dl 38
loc 181
rs 9.6

9 Methods

Rating   Name   Duplication   Size   Complexity  
A limit() 8 8 2
A top() 0 4 1
A hasTop() 0 4 1
A hasLimit() 0 4 1
C sqlDate() 8 68 20
A toDate() 0 4 1
A fromDate() 0 4 1
A executeAndGetInsertedId() 11 11 2
A concat() 11 11 3

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
namespace ByJG\AnyDataset\Database;
4
5
use ByJG\AnyDataset\Repository\DBDataset;
6
7
class DBMySQLFunctions extends DBBaseFunctions
8
{
9
10
    private $sysTimeStamp = 'NOW()';
11
12 View Code Duplication
    function concat($s1, $s2 = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
13
    {
14
        $sql = "concat(";
15
        for ($i = 0, $numArgs = func_num_args(); $i < $numArgs; $i++) {
16
            $var = func_get_arg($i);
17
            $sql .= ($i == 0 ? "" : ",") . $var;
18
        }
19
        $sql .= ")";
20
21
        return $sql;
22
    }
23
24
    /**
25
     * Given a SQL returns it with the proper LIMIT or equivalent method included
26
     * @param string $sql
27
     * @param int $start
28
     * @param int $qty
29
     * @return string
30
     */
31 View Code Duplication
    function limit($sql, $start, $qty)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
32
    {
33
        if (strpos($sql, ' LIMIT ') === false) {
34
            return $sql . " LIMIT $start, $qty ";
35
        } else {
36
            return $sql;
37
        }
38
    }
39
40
    /**
41
     * Given a SQL returns it with the proper TOP or equivalent method included
42
     * @param string $sql
43
     * @param int $qty
44
     * @return string
45
     */
46
    function top($sql, $qty)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
47
    {
48
        return $this->limit($sql, 0, $qty);
49
    }
50
51
    /**
52
     * Return if the database provider have a top or similar function
53
     * @return bool
54
     */
55
    function hasTop()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
56
    {
57
        return true;
58
    }
59
60
    /**
61
     * Return if the database provider have a limit function
62
     * @return bool
63
     */
64
    function hasLimit()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
65
    {
66
        return true;
67
    }
68
69
    /**
70
     * Format date column in sql string given an input format that understands Y M D
71
     * @param string $fmt
72
     * @param string|bool $col
73
     * @return string
74
     * @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao")
75
     */
76
    function sqlDate($fmt, $col = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
77
    {
78
        if (!$col) $col = $this->sysTimeStamp;
79
        $s = 'DATE_FORMAT(' . $col . ",'";
80
        $concat = false;
81
        $len = strlen($fmt);
82
        for ($i = 0; $i < $len; $i++) {
83
            $ch = $fmt[$i];
84
            switch ($ch) {
85
                case 'Y':
86
                case 'y':
87
                    $s .= '%Y';
88
                    break;
89
                case 'Q':
90
                case 'q':
91
                    $s .= "'),Quarter($col)";
92
93
                    if ($len > $i + 1) $s .= ",DATE_FORMAT($col,'";
94
                    else $s .= ",('";
95
                    $concat = true;
96
                    break;
97
                case 'M':
98
                    $s .= '%b';
99
                    break;
100
101
                case 'm':
102
                    $s .= '%m';
103
                    break;
104
                case 'D':
105
                case 'd':
106
                    $s .= '%d';
107
                    break;
108
109
                case 'H':
110
                    $s .= '%H';
111
                    break;
112
113
                case 'h':
114
                    $s .= '%I';
115
                    break;
116
117
                case 'i':
118
                    $s .= '%i';
119
                    break;
120
121
                case 's':
122
                    $s .= '%s';
123
                    break;
124
125
                case 'a':
126
                case 'A':
127
                    $s .= '%p';
128
                    break;
129
130 View Code Duplication
                default:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
131
132
                    if ($ch == '\\') {
133
                        $i++;
134
                        $ch = substr($fmt, $i, 1);
135
                    }
136
                    $s .= $ch;
137
                    break;
138
            }
139
        }
140
        $s.="')";
141
        if ($concat) $s = "CONCAT($s)";
142
        return $s;
143
    }
144
145
    /**
146
     * Format a string date to a string database readable format.
147
     *
148
     * @param string $date
149
     * @param string $dateFormat
150
     * @return string
151
     */
152
    function toDate($date, $dateFormat)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
153
    {
154
        return parent::toDate($date, $dateFormat);
155
    }
156
157
    /**
158
     * Format a string database readable format to a string date in a free format.
159
     *
160
     * @param string $date
161
     * @param string $dateFormat
162
     * @return string
163
     */
164
    function fromDate($date, $dateFormat)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
165
    {
166
        return parent::fromDate($date, $dateFormat);
167
    }
168
169
    /**
170
     *
171
     * @param DBDataset $dbdataset
172
     * @param string $sql
173
     * @param array $param
174
     * @return int
175
     */
176 View Code Duplication
    function executeAndGetInsertedId($dbdataset, $sql, $param)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
177
    {
178
        $id = parent::executeAndGetInsertedId($dbdataset, $sql, $param);
179
        $it = $dbdataset->getIterator("select LAST_INSERT_ID() id");
180
        if ($it->hasNext()) {
181
            $sr = $it->moveNext();
182
            $id = $sr->getField("id");
183
        }
184
185
        return $id;
186
    }
187
}
188