CardService   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 120
Duplicated Lines 30.83 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 20
lcom 1
cbo 3
dl 37
loc 120
rs 10
c 2
b 1
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A rename() 9 9 2
A archive() 6 6 1
A unarchive() 6 6 1
A assignLabel() 8 8 2
A removeLabel() 8 8 2
A __construct() 0 5 1
A find() 0 4 1
A create() 0 11 1
A delete() 0 4 1
A update() 0 14 2
B reorder() 0 27 6

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
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Deck\Service;
25
26
use OCA\Deck\Db\Card;
27
use OCA\Deck\Db\CardMapper;
28
use OCA\Deck\Db\Acl;
29
use OCA\Deck\CardArchivedException;
30
use OCA\Deck\Db\StackMapper;
31
32
33
class CardService {
34
35
	private $cardMapper;
36
37
	public function __construct(CardMapper $cardMapper, StackMapper $stackMapper, PermissionService $permissionService) {
38
		$this->cardMapper = $cardMapper;
39
		$this->stackMapper = $stackMapper;
0 ignored issues
show
Bug introduced by
The property stackMapper 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...
40
		$this->permissionService = $permissionService;
0 ignored issues
show
Bug introduced by
The property permissionService 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...
41
	}
42
43
	public function find($cardId) {
44
		$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_READ);
45
		return $this->cardMapper->find($cardId);
46
	}
47
48
	/**
49
	 * @param integer $order
50
	 */
51
	public function create($title, $stackId, $type, $order, $owner) {
52
		$this->permissionService->checkPermission($this->stackMapper, $stackId, Acl::PERMISSION_EDIT);
53
		$card = new Card();
54
		$card->setTitle($title);
55
		$card->setStackId($stackId);
56
		$card->setType($type);
57
		$card->setOrder($order);
58
		$card->setOwner($owner);
59
		return $this->cardMapper->insert($card);
60
61
	}
62
63
	public function delete($id) {
64
		$this->permissionService->checkPermission($this->cardMapper, $id, Acl::PERMISSION_EDIT);
65
		return $this->cardMapper->delete($this->cardMapper->find($id));
66
	}
67
68
	public function update($id, $title, $stackId, $type, $order, $description, $owner) {
69
		$this->permissionService->checkPermission($this->cardMapper, $id, Acl::PERMISSION_EDIT);
70
		$card = $this->cardMapper->find($id);
71
		if ($card->getArchived()) {
72
			throw new CardArchivedException();
73
		}
74
		$card->setTitle($title);
75
		$card->setStackId($stackId);
76
		$card->setType($type);
77
		$card->setOrder($order);
78
		$card->setOwner($owner);
79
		$card->setDescription($description);
80
		return $this->cardMapper->update($card);
81
	}
82
83 View Code Duplication
	public function rename($id, $title) {
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...
84
		$this->permissionService->checkPermission($this->cardMapper, $id, Acl::PERMISSION_EDIT);
85
		$card = $this->cardMapper->find($id);
86
		if ($card->getArchived()) {
87
			throw new CardArchivedException();
88
		}
89
		$card->setTitle($title);
90
		return $this->cardMapper->update($card);
91
	}
92
93
	public function reorder($id, $stackId, $order) {
94
		$this->permissionService->checkPermission($this->cardMapper, $id, Acl::PERMISSION_EDIT);
95
		$cards = $this->cardMapper->findAll($stackId);
96
		$result = [];
97
		$i = 0;
98
		foreach ($cards as $card) {
99
			if ($card->getArchived()) {
100
				throw new CardArchivedException();
101
			}
102
			if ($card->id === $id) {
103
				$card->setOrder($order);
104
				$card->setLastModified(time());
105
			}
106
107
			if ($i === $order) {
108
				$i++;
109
			}
110
111
			if ($card->id !== $id) {
112
				$card->setOrder($i++);
113
			}
114
			$this->cardMapper->update($card);
115
			$result[$card->getOrder()] = $card;
116
		}
117
118
		return $result;
119
	}
120
121 View Code Duplication
	public function archive($id) {
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...
122
		$this->permissionService->checkPermission($this->cardMapper, $id, Acl::PERMISSION_EDIT);
123
		$card = $this->cardMapper->find($id);
124
		$card->setArchived(true);
125
		return $this->cardMapper->update($card);
126
	}
127
128 View Code Duplication
	public function unarchive($id) {
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...
129
		$this->permissionService->checkPermission($this->cardMapper, $id, Acl::PERMISSION_EDIT);
130
		$card = $this->cardMapper->find($id);
131
		$card->setArchived(false);
132
		return $this->cardMapper->update($card);
133
	}
134
135 View Code Duplication
	public function assignLabel($cardId, $labelId) {
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...
136
		$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);
137
		$card = $this->cardMapper->find($cardId);
138
		if ($card->getArchived()) {
139
			throw new CardArchivedException();
140
		}
141
		$this->cardMapper->assignLabel($cardId, $labelId);
142
	}
143
144 View Code Duplication
	public function removeLabel($cardId, $labelId) {
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...
145
		$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);
146
		$card = $this->cardMapper->find($cardId);
147
		if ($card->getArchived()) {
148
			throw new CardArchivedException();
149
		}
150
		$this->cardMapper->removeLabel($cardId, $labelId);
151
	}
152
}