Completed
Pull Request — newinternal (#285)
by Simon
06:16 queued 03:05
created

Ban   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 267
Duplicated Lines 8.61 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 23
loc 267
rs 10
c 0
b 0
f 0
ccs 0
cts 151
cp 0
wmc 26
lcom 1
cbo 3

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 4 1
A setType() 0 4 1
A getTarget() 0 4 1
A setTarget() 0 4 1
A setReason() 0 4 1
A getDate() 0 4 1
A getDuration() 0 4 1
A setDuration() 0 4 1
A setActive() 0 4 2
B getActiveBans() 0 26 3
A getActiveId() 0 20 2
A getBanByTarget() 23 23 2
B save() 0 50 5
A getReason() 0 4 1
A isActive() 0 4 1
A getUser() 0 4 1
A setUser() 0 4 1

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 PDO;
13
use Waca\DataObject;
14
use Waca\Exceptions\OptimisticLockFailedException;
15
use Waca\PdoDatabase;
16
17
/**
18
 * Ban data object
19
 */
20
class Ban extends DataObject
21
{
22
    private $type;
23
    private $target;
24
    private $user;
25
    private $reason;
26
    private $date;
27
    private $duration;
28
    private $active;
29
30
    /**
31
     * Gets all active bans, filtered by the optional target.
32
     *
33
     * @param string|null $target
34
     * @param PdoDatabase $database
35
     *
36
     * @return Ban[]
37
     */
38
    public static function getActiveBans($target, PdoDatabase $database)
39
    {
40
        if ($target !== null) {
41
            $query = <<<SQL
42
SELECT * FROM ban WHERE target = :target AND (duration > UNIX_TIMESTAMP() OR duration = -1) AND active = 1;
43
SQL;
44
            $statement = $database->prepare($query);
45
            $statement->bindValue(":target", $target);
46
        }
47
        else {
48
            $query = "SELECT * FROM ban WHERE (duration > UNIX_TIMESTAMP() OR duration = -1) AND active = 1;";
49
            $statement = $database->prepare($query);
50
        }
51
52
        $statement->execute();
53
54
        $result = array();
55
56
        /** @var Ban $v */
57
        foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) {
58
            $v->setDatabase($database);
59
            $result[] = $v;
60
        }
61
62
        return $result;
63
    }
64
65
    /**
66
     * Gets a ban by it's ID if it's currently active.
67
     *
68
     * @param     integer $id
69
     * @param PdoDatabase $database
70
     *
71
     * @return Ban
72
     */
73
    public static function getActiveId($id, PdoDatabase $database)
74
    {
75
        $statement = $database->prepare(<<<SQL
76
SELECT *
77
FROM ban
78
WHERE id = :id  AND (duration > UNIX_TIMESTAMP() OR duration = -1) AND active = 1;
79
SQL
80
        );
81
        $statement->bindValue(":id", $id);
82
83
        $statement->execute();
84
85
        $resultObject = $statement->fetchObject(get_called_class());
86
87
        if ($resultObject != false) {
88
            $resultObject->setDatabase($database);
89
        }
90
91
        return $resultObject;
92
    }
93
94
    /**
95
     * Get all active bans for a target and type.
96
     *
97
     * @param string      $target
98
     * @param string      $type
99
     * @param PdoDatabase $database
100
     *
101
     * @return Ban
102
     */
103 View Code Duplication
    public static function getBanByTarget($target, $type, PdoDatabase $database)
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...
104
    {
105
        $query = <<<SQL
106
SELECT * FROM ban
107
WHERE type = :type
108
	AND target = :target
109
	AND (duration > UNIX_TIMESTAMP() OR duration = -1)
110
	AND active = 1;
111
SQL;
112
        $statement = $database->prepare($query);
113
        $statement->bindValue(":target", $target);
114
        $statement->bindValue(":type", $type);
115
116
        $statement->execute();
117
118
        $resultObject = $statement->fetchObject(get_called_class());
119
120
        if ($resultObject != false) {
121
            $resultObject->setDatabase($database);
122
        }
123
124
        return $resultObject;
125
    }
126
127
    /**
128
     * @throws Exception
129
     */
130
    public function save()
131
    {
132
        if ($this->isNew()) {
133
            // insert
134
            $statement = $this->dbObject->prepare(<<<SQL
135
INSERT INTO `ban` (type, target, user, reason, date, duration, active)
136
VALUES (:type, :target, :user, :reason, CURRENT_TIMESTAMP(), :duration, :active);
137
SQL
138
            );
139
            $statement->bindValue(":type", $this->type);
140
            $statement->bindValue(":target", $this->target);
141
            $statement->bindValue(":user", $this->user);
142
            $statement->bindValue(":reason", $this->reason);
143
            $statement->bindValue(":duration", $this->duration);
144
            $statement->bindValue(":active", $this->active);
145
146
            if ($statement->execute()) {
147
                $this->id = (int)$this->dbObject->lastInsertId();
148
            }
149
            else {
150
                throw new Exception($statement->errorInfo());
151
            }
152
        }
153
        else {
154
            // update
155
            $statement = $this->dbObject->prepare(<<<SQL
156
UPDATE `ban`
157
SET duration = :duration, active = :active, user = :user, updateversion = updateversion + 1
158
WHERE id = :id AND updateversion = :updateversion
159
LIMIT 1;
160
SQL
161
            );
162
            $statement->bindValue(':id', $this->id);
163
            $statement->bindValue(':updateversion', $this->updateversion);
164
165
            $statement->bindValue(':duration', $this->duration);
166
            $statement->bindValue(':active', $this->active);
167
            $statement->bindValue(':user', $this->user);
168
169
            if (!$statement->execute()) {
170
                throw new Exception($statement->errorInfo());
171
            }
172
173
            if ($statement->rowCount() !== 1) {
174
                throw new OptimisticLockFailedException();
175
            }
176
177
            $this->updateversion++;
178
        }
179
    }
180
181
    /**
182
     * @return string
183
     */
184
    public function getType()
185
    {
186
        return $this->type;
187
    }
188
189
    /**
190
     * @param string $type
191
     */
192
    public function setType($type)
193
    {
194
        $this->type = $type;
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function getTarget()
201
    {
202
        return $this->target;
203
    }
204
205
    /**
206
     * @param string $target
207
     */
208
    public function setTarget($target)
209
    {
210
        $this->target = $target;
211
    }
212
213
    /**
214
     * @return string
215
     */
216
    public function getReason()
217
    {
218
        return $this->reason;
219
    }
220
221
    /**
222
     * @param string $reason
223
     */
224
    public function setReason($reason)
225
    {
226
        $this->reason = $reason;
227
    }
228
229
    /**
230
     * @return mixed
231
     */
232
    public function getDate()
233
    {
234
        return $this->date;
235
    }
236
237
    /**
238
     * @return mixed
239
     */
240
    public function getDuration()
241
    {
242
        return $this->duration;
243
    }
244
245
    /**
246
     * @param mixed $duration
247
     */
248
    public function setDuration($duration)
249
    {
250
        $this->duration = $duration;
251
    }
252
253
    /**
254
     * @return bool
255
     */
256
    public function isActive()
257
    {
258
        return $this->active == 1;
259
    }
260
261
    /**
262
     * @param bool $active
263
     */
264
    public function setActive($active)
265
    {
266
        $this->active = $active ? 1 : 0;
267
    }
268
269
    /**
270
     * @return int
271
     */
272
    public function getUser()
273
    {
274
        return $this->user;
275
    }
276
277
    /**
278
     * @param int $user UserID of user who is setting the ban
279
     *
280
     * @throws Exception
281
     */
282
    public function setUser($user)
283
    {
284
        $this->user = $user;
285
    }
286
}
287