Passed
Branch master (d51fdb)
by Joao
05:45 queued 02:33
created

DbPgsqlFunctions   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 150
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 75.61%

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 2
dl 150
loc 150
ccs 31
cts 41
cp 0.7561
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A concat() 4 4 1
A top() 4 4 1
A hasTop() 4 4 1
A hasLimit() 4 4 1
A toDate() 4 4 1
A fromDate() 4 4 1
A executeAndGetInsertedId() 7 7 1
A hasForUpdate() 4 4 1
A limit() 16 16 3
B sqlDate() 29 29 2

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\Store\Helpers;
4
5
use ByJG\AnyDataset\DbDriverInterface;
6
7 View Code Duplication
class DbPgsqlFunctions extends DbBaseFunctions
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...
8
{
9
10 8
    public function __construct()
11
    {
12 8
        $this->deliFieldLeft = '"';
13 8
        $this->deliFieldRight = '"';
14 8
        $this->deliTableLeft = '"';
15 8
        $this->deliTableRight = '"';
16 8
    }
17
18 1
    public function concat($str1, $str2 = null)
19
    {
20 1
        return implode(func_get_args(), ' || ');
21
    }
22
23
    /**
24
     * Given a SQL returns it with the proper LIMIT or equivalent method included
25
     * @param string $sql
26
     * @param int $start
27
     * @param int $qty
28
     * @return string
29
     */
30 2
    public function limit($sql, $start, $qty = null)
31
    {
32 2
        if (is_null($qty)) {
33 1
            $qty = 50;
34
        }
35
36 2
        if (stripos($sql, ' LIMIT ') === false) {
37 2
            $sql = $sql . " LIMIT x OFFSET y";
38
        }
39
40 2
        return preg_replace(
41 2
            '~(\s[Ll][Ii][Mm][Ii][Tt])\s.*?\s([Oo][Ff][Ff][Ss][Ee][Tt])\s.*~',
42 2
            '$1 ' . $qty .' $2 ' .$start,
43 2
            $sql
44
        );
45
    }
46
47
    /**
48
     * Given a SQL returns it with the proper TOP or equivalent method included
49
     * @param string $sql
50
     * @param int $qty
51
     * @return string
52
     */
53 1
    public function top($sql, $qty)
54
    {
55 1
        return $this->limit($sql, 0, $qty);
56
    }
57
58
    /**
59
     * Return if the database provider have a top or similar function
60
     * @return bool
61
     */
62 1
    public function hasTop()
63
    {
64 1
        return true;
65
    }
66
67
    /**
68
     * Return if the database provider have a limit function
69
     * @return bool
70
     */
71 1
    public function hasLimit()
72
    {
73 1
        return true;
74
    }
75
76
    /**
77
     * Format date column in sql string given an input format that understands Y M D
78
     *
79
     * @param string $format
80
     * @param string|null $column
81
     * @return string
82
     * @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao")
83
     */
84 1
    public function sqlDate($format, $column = null)
85
    {
86 1
        if (is_null($column)) {
87 1
            $column = 'current_timestamp';
88
        }
89
90
        $pattern = [
91 1
            'Y' => "YYYY",
92
            'y' => "YY",
93
            'M' => "Mon",
94
            'm' => "MM",
95
            'Q' => "Q",
96
            'q' => "Q",
97
            'D' => "DD",
98
            'd' => "DD",
99
            'h' => "HH",
100
            'H' => "HH24",
101
            'i' => "MI",
102
            's' => "SS",
103
            'a' => "AM",
104
            'A' => "AM",
105
        ];
106
107 1
        return sprintf(
108 1
            "TO_CHAR(%s,'%s')",
109 1
            $column,
110 1
            implode('', $this->prepareSqlDate($format, $pattern, ''))
111
        );
112
    }
113
114
    /**
115
     * Format a string date to a string database readable format.
116
     *
117
     * @param string $date
118
     * @param string $dateFormat
119
     * @return string
120
     */
121
    public function toDate($date, $dateFormat)
122
    {
123
        return parent::toDate($date, $dateFormat);
124
    }
125
126
    /**
127
     * Format a string database readable format to a string date in a free format.
128
     *
129
     * @param string $date
130
     * @param string $dateFormat
131
     * @return string
132
     */
133
    public function fromDate($date, $dateFormat)
134
    {
135
        return parent::fromDate($date, $dateFormat);
136
    }
137
138
    /**
139
     * @param DbDriverInterface $dbdataset
140
     * @param string $sql
141
     * @param array $param
142
     * @return int
143
     */
144
    public function executeAndGetInsertedId(DbDriverInterface $dbdataset, $sql, $param)
145
    {
146
        parent::executeAndGetInsertedId($dbdataset, $sql, $param);
147
        $idInserted = $dbdataset->getScalar('select lastval()');
148
149
        return $idInserted;
150
    }
151
152
    public function hasForUpdate()
153
    {
154
        return true;
155
    }
156
}
157