Failed Conditions
Pull Request — master (#38)
by
unknown
01:17
created

NextNoteMapper   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 197
Duplicated Lines 22.84 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 2
dl 45
loc 197
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B find() 0 31 4
B findNotesFromUser() 0 24 4
B create() 24 24 4
A updateNote() 21 21 4
A createNotePart() 0 4 1
A deleteNoteParts() 0 4 1
A getNoteParts() 0 4 1
A deleteNote() 0 5 1
A makeEntityFromDBResult() 0 10 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
 * Nextcloud - NextNote
4
 *
5
 * @copyright Copyright (c) 2015, Ben Curtis <[email protected]>
6
 * @copyright Copyright (c) 2017, Sander Brand ([email protected])
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\NextNote\Db;
25
26
use \OCA\NextNote\Utility\Utils;
27
use OCP\AppFramework\Db\Entity;
28
use OCP\IDBConnection;
29
use OCP\AppFramework\Db\Mapper;
30
31
class NextNoteMapper extends Mapper {
32
	private $utils;
33
	private $maxNoteFieldLength = 2621440;
34
35
	public function __construct(IDBConnection $db, Utils $utils) {
36
		parent::__construct($db, 'ownnote');
37
		$this->utils = $utils;
38
	}
39
40
41
    /**
42
     * @param $note_id
43
     * @param null $user_id
44
     * @param int|bool $deleted
45
     * @return NextNote if not found
46
     */
47
	public function find($note_id, $user_id = null, $deleted = false) {
48
		$params = [$note_id];
49
		$uidSql = '';
50
		if ($user_id) {
51
			$params[] = $user_id;
52
			$uidSql = 'and n.uid = ?';
53
		}
54
55
		$deletedSql = '';
56
		if ($deleted !== false) {
57
			$params[] = $deleted;
58
			$deletedSql = 'and n.deleted = ?';
59
		}
60
		$sql = "SELECT id, uid, name, grouping, shared, mtime, deleted, note FROM *PREFIX*ownnote n WHERE n.id= ? $uidSql $deletedSql";
61
		$results = [];
62
		foreach ($this->execute($sql, $params)->fetchAll() as $item) {
63
			/**
64
			 * @var $note NextNote
65
			 */
66
			$note = $this->makeEntityFromDBResult($item);
67
			/* fetch note parts */
68
			$noteParts = $this->getNoteParts($note);
69
			$partsTxt = implode('', array_map(function ($part) {
70
				return $part['note'];
71
			}, $noteParts));
72
			$note->setNote($arr['note'] . $partsTxt);
0 ignored issues
show
Bug introduced by
The variable $arr does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
73
74
			$results[] = $note;
75
		}
76
		return array_shift($results);
77
	}
78
79
80
	/**
81
	 * @param $userId
82
	 * @param int|bool $deleted
83
	 * @param string|bool $group
84
	 * @return NextNote[] if not found
85
	 */
86
	public function findNotesFromUser($userId, $deleted = 0, $group = false) {
87
		$params = [$userId];
88
		$groupSql = '';
89
		if ($group) {
90
			$groupSql = 'and n.grouping = ?';
91
			$params[] = $group;
92
		}
93
		$deletedSql = '';
94
		if ($deleted !== false) {
95
			$deleted = (int) $deleted;
96
			$deletedSql = 'and n.deleted = ?';
97
			$params[] = $deleted;
98
		}
99
		$sql = "SELECT id, uid, name, grouping, shared, mtime, deleted, note FROM *PREFIX*ownnote n WHERE `uid` = ? $groupSql $deletedSql";
100
		$results = [];
101
		foreach ($this->execute($sql, $params)->fetchAll() as $item) {
102
			/**
103
			 * @var $note NextNote
104
			 */
105
			$note = $this->makeEntityFromDBResult($item);
106
			$results[] = $note;
107
		}
108
		return $results;
109
	}
110
111
	
112
113
	/**
114
	 * Creates a note
115
	 *
116
	 * @param NextNote $note
117
	 * @return NextNote|Entity
118
	 * @internal param $userId
119
	 */
120 View Code Duplication
	public function create($note) {
121
		$len = mb_strlen($note->getNote());
122
		$parts = false;
123
		if ($len > $this->maxNoteFieldLength) {
124
			$parts = $this->utils->splitContent($note->getNote());
125
			$note->setNote('');
126
		}
127
128
		$note->setShared(false);
129
		/**
130
		 * @var $note NextNote
131
		 */
132
		$note = parent::insert($note);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (insert() instead of create()). Are you sure this is correct? If so, you might want to change this to $this->insert().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
133
134
		if ($parts) {
135
			foreach ($parts as $part) {
136
				$this->createNotePart($note, $part);
137
			}
138
			$note->setNote(implode('', $parts));
139
		}
140
141
142
		return $note;
143
	}
144
145
	/**
146
	 * Update note
147
	 *
148
	 * @param NextNote $note
149
	 * @return NextNote|Entity
150
	 */
151 View Code Duplication
	public function updateNote($note) {
152
		$len = mb_strlen($note->getNote());
153
		$parts = false;
154
		$this->deleteNoteParts($note);
155
156
		if ($len > $this->maxNoteFieldLength) {
157
			$parts = $this->utils->splitContent($note->getNote());
158
			$note->setNote('');
159
		}
160
		/**
161
		 * @var $note NextNote
162
		 */
163
		$note = parent::update($note);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (update() instead of updateNote()). Are you sure this is correct? If so, you might want to change this to $this->update().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
164
		if ($parts) {
165
			foreach ($parts as $part) {
166
				$this->createNotePart($note, $part);
167
			}
168
			$note->setNote(implode('', $parts));
169
		}
170
		return $note;
171
	}
172
173
	/**
174
	 * @param NextNote $note
175
	 * @param $content
176
	 */
177
	public function createNotePart(NextNote $note, $content) {
178
		$sql = "INSERT INTO *PREFIX*ownnote_parts VALUES (NULL, ?, ?);";
179
		$this->execute($sql, array($note->getId(), $content));
180
	}
181
182
	/**
183
	 * Delete the note parts
184
	 *
185
	 * @param NextNote $note
186
	 */
187
	public function deleteNoteParts(NextNote $note) {
188
		$sql = 'DELETE FROM *PREFIX*ownnote_parts where id = ?';
189
		$this->execute($sql, array($note->getId()));
190
	}
191
192
	/**
193
	 * Get the note parts
194
	 *
195
	 * @param NextNote $note
196
	 * @return array
197
	 */
198
	public function getNoteParts(NextNote $note) {
199
		$sql = 'SELECT * from *PREFIX*ownnote_parts where id = ?';
200
		return $this->execute($sql, array($note->getId()))->fetchAll();
201
	}
202
203
	/**
204
	 * @param NextNote $note
205
	 * @return bool
206
	 */
207
	public function deleteNote(NextNote $note) {
208
		$this->deleteNoteParts($note);
209
		parent::delete($note);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (delete() instead of deleteNote()). Are you sure this is correct? If so, you might want to change this to $this->delete().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
210
		return true;
211
	}
212
213
	/**
214
	 * @param $arr
215
	 * @return NextNote
216
	 */
217
	public function makeEntityFromDBResult($arr) {
218
		$note = new NextNote();
219
		$note->setId($arr['id']);
220
		$note->setName($arr['name']);
221
		$note->setGrouping($arr['grouping']);
222
		$note->setMtime($arr['mtime']);
223
		$note->setDeleted($arr['deleted']);
224
		$note->setUid($arr['uid']);
225
		return $note;
226
	}
227
}
228