Completed
Push — 2.0 ( c11079...9c6898 )
by Vermeulen
02:10
created

SqlInsert   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 27.27 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 15
loc 55
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 14 14 2
B assembleRequest() 0 26 4

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 BfwSql;
4
5
/**
6
 * Class to write INSERT INTO queries
7
 * 
8
 * @package bfw-sql
9
 * @author Vermeulen Maxime <[email protected]>
10
 * @version 2.0
11
 */
12
class SqlInsert extends SqlActions
13
{
14
    /**
15
     * Constructor
16
     * 
17
     * @param \BfwSql\SqlConnect $sqlConnect Instance of SGBD connexion
18
     * @param string             $tableName  The table name used for query
19
     * @param array              $columns    (default: null) Datas to add
20
     *  Format is array('columnName' => 'value', ...);
21
     */
22 View Code Duplication
    public function __construct(
0 ignored issues
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...
23
        SqlConnect $sqlConnect,
24
        $tableName,
25
        $columns = null
26
    ) {
27
        parent::__construct($sqlConnect);
28
        
29
        $prefix          = $sqlConnect->getConnectionInfos()->tablePrefix;
30
        $this->tableName = $prefix.$tableName;
31
        
32
        if (is_array($columns)) {
33
            $this->columns = $columns;
34
        }
35
    }
36
    
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function assembleRequest()
41
    {
42
        $lstColumns    = '';
43
        $lstValues     = '';
44
        $indexReadList = -1;
45
        
46
        foreach ($this->columns as $columnName => $columnValue) {
47
            $indexReadList++;
48
            if ($indexReadList > 0) {
49
                $lstColumns .= ',';
50
                $lstValues  .= ',';
51
            }
52
            
53
            $lstColumns .= '`'.$columnName.'`';
54
            $lstValues  .= $columnValue;
55
        }
56
        
57
        $this->assembledRequest = 'INSERT INTO '.$this->tableName;
58
        
59
        if ($this->columns !== []) {
60
            $this->assembledRequest .= ' ('.$lstColumns.')'
61
                .' VALUES ('.$lstValues.')';
62
        }
63
        
64
        $this->callObserver();
65
    }
66
} 
67