Completed
Push — 2.0 ( ada449...a2425b )
by Vermeulen
01:55
created

Update   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 25 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 17
loc 68
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 17 17 2
A assembleRequest() 0 29 5

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\Actions;
4
5
use \Exception;
6
7
/**
8
 * Class to write UPDATE queries
9
 * 
10
 * @package bfw-sql
11
 * @author Vermeulen Maxime <[email protected]>
12
 * @version 2.0
13
 */
14
class Update extends AbstractActions
15
{
16
    /**
17
     * @const ERR_ASSEMBLE_NO_DATAS Exception code if no data to update.
18
     */
19
    const ERR_ASSEMBLE_NO_DATAS = 2305001;
20
    
21
    /**
22
     * Constructor
23
     * 
24
     * @param \BfwSql\SqlConnect $sqlConnect Instance of SGBD connexion
25
     * @param string             $tableName  The table name used for query
26
     * @param array              $columns    (default: null) Datas to add
27
     *  Format is array('columnName' => 'value', ...);
28
     * @param string $quoteStatus (default: QUOTE_ALL) Status to automatic
29
     *  quoted string value system.
30
     */
31 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...
32
        \BfwSql\SqlConnect $sqlConnect,
33
        $tableName,
34
        $columns = null,
35
        $quoteStatus = \BfwSql\Actions\AbstractActions::QUOTE_ALL
36
    ) {
37
        parent::__construct($sqlConnect);
38
        
39
        $this->quoteStatus = $quoteStatus;
40
        
41
        $prefix          = $sqlConnect->getConnectionInfos()->tablePrefix;
42
        $this->tableName = $prefix.$tableName;
43
        
44
        if (is_array($columns)) {
45
            $this->columns = $columns;
46
        }
47
    }
48
    
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function assembleRequest()
53
    {
54
        if (count($this->columns) === 0) {
55
            throw new Exception(
56
                'SqlUpdate : no datas to update.',
57
                self::ERR_ASSEMBLE_NO_DATAS
58
            );
59
        }
60
        
61
        $lstColumns = '';
62
63
        foreach ($this->columns as $columnName => $columnValue) {
64
            if ($lstColumns !== '') {
65
                $lstColumns .= ',';
66
            }
67
            
68
            if ($columnValue === null) {
69
                $fieldValue = 'null';
70
            } else {
71
                $fieldValue = $this->quoteValue($columnName, $columnValue);
72
            }
73
74
            $lstColumns .= '`'.$columnName.'`='.$fieldValue;
75
        }
76
        
77
        $this->assembledRequest = 'UPDATE '.$this->tableName
78
            .' SET '.$lstColumns
79
            .$this->generateWhere();
80
    }
81
}
82