Failed Conditions
Pull Request — newinternal (#527)
by Simon
17:20 queued 07:22
created

Credential   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 146
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 146
loc 146
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 3

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserId() 4 4 1
A setUserId() 4 4 1
A getFactor() 4 4 1
A setFactor() 4 4 1
A getType() 4 4 1
A setType() 4 4 1
A getData() 4 4 1
A setData() 4 4 1
A getVersion() 4 4 1
A setVersion() 4 4 1
B save() 52 52 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
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\DataObjects;
10
11
use Exception;
12
use Waca\DataObject;
13
use Waca\Exceptions\OptimisticLockFailedException;
14
15 View Code Duplication
class Credential extends DataObject
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...
16
{
17
    /** @var int */
18
    private $user;
19
    /** @var int */
20
    private $factor;
21
    /** @var string */
22
    private $type;
23
    /** @var string */
24
    private $data;
25
    /** @var int */
26
    private $version;
27
28
    /**
29
     * @return int
30
     */
31
    public function getUserId()
32
    {
33
        return $this->user;
34
    }
35
36
    /**
37
     * @param int $user
38
     */
39
    public function setUserId($user)
40
    {
41
        $this->user = $user;
42
    }
43
44
    /**
45
     * @return int
46
     */
47
    public function getFactor()
48
    {
49
        return $this->factor;
50
    }
51
52
    /**
53
     * @param int $factor
54
     */
55
    public function setFactor($factor)
56
    {
57
        $this->factor = $factor;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getType()
64
    {
65
        return $this->type;
66
    }
67
68
    /**
69
     * @param string $type
70
     */
71
    public function setType($type)
72
    {
73
        $this->type = $type;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getData()
80
    {
81
        return $this->data;
82
    }
83
84
    /**
85
     * @param string $data
86
     */
87
    public function setData($data)
88
    {
89
        $this->data = $data;
90
    }
91
92
    /**
93
     * @return int
94
     */
95
    public function getVersion()
96
    {
97
        return $this->version;
98
    }
99
100
    /**
101
     * @param int $version
102
     */
103
    public function setVersion($version)
104
    {
105
        $this->version = $version;
106
    }
107
108
    public function save()
109
    {
110
        if ($this->isNew()) {
111
            // insert
112
            $statement = $this->dbObject->prepare(<<<SQL
113
INSERT INTO credential ( updateversion, user, factor, type, data, version )
114
VALUES ( 0, :user, :factor, :type, :data, :version );
115
SQL
116
            );
117
            $statement->bindValue(":user", $this->user);
118
            $statement->bindValue(":factor", $this->factor);
119
            $statement->bindValue(":type", $this->type);
120
            $statement->bindValue(":data", $this->data);
121
            $statement->bindValue(":version", $this->version);
122
123
            if ($statement->execute()) {
124
                $this->id = (int)$this->dbObject->lastInsertId();
125
            }
126
            else {
127
                throw new Exception($statement->errorInfo());
128
            }
129
        }
130
        else {
131
            // update
132
            $statement = $this->dbObject->prepare(<<<SQL
133
                UPDATE credential
134
                SET   factor = :factor
135
                    , data = :data
136
                    , version = :version                 
137
                    , updateversion = updateversion + 1
138
                WHERE id = :id AND updateversion = :updateversion;
139
SQL
140
            );
141
142
            $statement->bindValue(':id', $this->id);
143
            $statement->bindValue(':updateversion', $this->updateversion);
144
145
            $statement->bindValue(":factor", $this->factor);
146
            $statement->bindValue(":data", $this->data);
147
            $statement->bindValue(":version", $this->version);
148
149
            if (!$statement->execute()) {
150
                throw new Exception($statement->errorInfo());
151
            }
152
153
            if ($statement->rowCount() !== 1) {
154
                throw new OptimisticLockFailedException();
155
            }
156
157
            $this->updateversion++;
158
        }
159
    }
160
}