Completed
Pull Request — master (#324)
by
unknown
04:34
created

PassmanImporter.padlock.readFile   C

Complexity

Conditions 11
Paths 49

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
nc 49
nop 0
dl 0
loc 57
rs 6.4824
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like PassmanImporter.padlock.readFile often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @copyright Copyright (c) 2017, Bingen Eguzkitza ([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
// TODO: Allow for new lines and commas in values
25
26
// Importers should always start with this
27
/** global: PassmanImporter */
28
var PassmanImporter = PassmanImporter || {};
29
(function(window, $, PassmanImporter) {
30
	'use strict';
31
	// Define the importer
32
	PassmanImporter.padlock = {
33
		info: {
34
			name: 'Padlock',
35
			id: 'padlock',
36
			exportSteps: ['Create a csv export. Go to Menu -> Settings -> Export Data and copy text into a .csv file']
37
		}
38
	};
39
40
	PassmanImporter.padlock.readFile = function (file_data) {
41
		/** global: C_Promise */
42
		return new C_Promise(function(){
43
			var rows = file_data.split('\n');
44
			var credential_list = [];
45
			var titles = rows[0].split(',');
46
			for (var i = 1; i < rows.length; i++) {
47
				var row = rows[i];
48
				var row_data = row.split(',');
49
				if (row_data[0].charAt(0) === '"') {
50
					row_data[0] = row_data[0].substring(1);
51
				}
52
53
				if (row_data[row_data.length-1].toString().charAt(row_data[row_data.length - 1].length - 1) === '"') {
54
					row_data[row_data.length - 1] = row_data[row_data.length -1].substring(0, row_data[row_data.length - 1].length - 1);
55
				}
56
57
				var _credential = PassmanImporter.newCredential();
58
				_credential.label = row_data[0];
59
				_credential.username = row_data[2];
60
				_credential.password = row_data[3];
61
				var k = 0;
62
				for (var j = 4; j < titles.length; j++) {
63
					if (!row_data[j])
64
						continue;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
65
					if (titles[j].toLowerCase() == 'url') {
66
						_credential.url = row_data[j];
67
						continue;
68
					}
69
					if (titles[j].toLowerCase() == 'e-mail'
70
					    || titles[j].toLowerCase() == 'email') {
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before ||.
Loading history...
71
						_credential.email = row_data[j];
72
						continue;
73
					}
74
					if (titles[j].toLowerCase() == 'description') {
75
						_credential.description = row_data[j];
76
						continue;
77
					}
78
					_credential.custom_fields[k] = {
79
						'label' : titles[j],
80
						'value' : row_data[j],
81
						'secret' : true,
82
						'field_type' : 'text'
83
					};
84
					k++;
85
				}
86
				if(_credential.label){
87
					credential_list.push(_credential);
88
				}
89
90
				var progress = {
91
					percent: i/rows.length*100,
92
					loaded: i,
93
					total: rows.length
94
				};
95
				this.call_progress(progress);
96
			}
97
			this.call_then(credential_list);
98
		});
99
	};
100
})(window, $, PassmanImporter);
101