Completed
Push — 2.0 ( bddf1c )
by Vermeulen
02:18
created

SqlUpdate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 28.3 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 15 15 2
B assembleRequest() 0 23 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 UPDATE queries
7
 * 
8
 * @package bfw-sql
9
 * @author Vermeulen Maxime <[email protected]>
10
 * @version 2.0
11
 */
12
class SqlUpdate 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
    {
28
        parent::__construct($sqlConnect);
29
        
30
        $prefix      = $sqlConnect->getConnectionInfos()->tablePrefix;
31
        $this->table = $prefix.$tableName;
0 ignored issues
show
Bug introduced by
The property table does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
        
33
        if (is_array($columns)) {
34
            $this->columns = $columns;
35
        }
36
    }
37
    
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function assembleRequest()
42
    {
43
        if (count($this->champs) === 0) {
0 ignored issues
show
Bug introduced by
The property champs does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
            return;
45
        }
46
        
47
        $lstColumns = '';
48
49
        foreach ($this->columns as $columnName => $columnValue) {
50
            if ($lstColumns !== '') {
51
                $lstColumns .= ', ';
52
            }
53
54
            $lstColumns .= $columnName.'='.$columnValue;
55
        }
56
57
        
58
        $this->assembledRequest = 'UPDATE '.$this->table
59
            .' SET '.$lstColumns
60
            .$this->generateWhere();
61
62
        $this->callObserver();
63
    }
64
}
65