Completed
Push — master ( 14d226...3a0e06 )
by Joas
10:50
created

BackupCodeMapper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 21.62 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 8
loc 37
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getBackupCodes() 0 16 1
A deleteCodes() 8 8 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
 * @author Christoph Wurst <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\TwoFactorBackupCodes\Db;
23
24
use OCP\AppFramework\Db\Mapper;
25
use OCP\DB\QueryBuilder\IQueryBuilder;
26
use OCP\IDb;
27
use OCP\IUser;
28
29
class BackupCodeMapper extends Mapper {
30
31
	public function __construct(IDb $db) {
32
		parent::__construct($db, 'twofactor_backup_codes');
33
	}
34
35
	/**
36
	 * @param IUser $user
37
	 * @return BackupCode[]
38
	 */
39
	public function getBackupCodes(IUser $user) {
40
		/* @var IQueryBuilder $qb */
41
		$qb = $this->db->getQueryBuilder();
42
43
		$qb->select('id', 'user_id', 'code', 'used')
0 ignored issues
show
Unused Code introduced by
The call to IQueryBuilder::select() has too many arguments starting with 'user_id'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
44
			->from('twofactor_backup_codes')
45
			->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID())));
46
		$result = $qb->execute();
47
48
		$rows = $result->fetchAll();
49
		$result->closeCursor();
50
51
		return array_map(function ($row) {
52
			return BackupCode::fromRow($row);
53
		}, $rows);
54
	}
55
56 View Code Duplication
	public function deleteCodes(IUser $user) {
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...
57
		/* @var IQueryBuilder $qb */
58
		$qb = $this->db->getQueryBuilder();
59
60
		$qb->delete('twofactor_backup_codes')
61
			->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID())));
62
		$qb->execute();
63
	}
64
65
}
66