src/atom/AtomSource.js   B
last analyzed

Complexity

Total Complexity 44
Complexity/F 1.29

Size

Lines of Code 400
Function Count 34

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 16
dl 0
loc 400
rs 8.3396
wmc 44
mnd 2
bc 43
fnc 34
bpm 1.2647
cpm 1.2941
noi 3

How to fix   Complexity   

Complexity

Complex classes like src/atom/AtomSource.js 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
module.exports = function(GedcomX){
2
3
  var utils = require('../utils'),
4
      AtomCommon = require('./AtomCommon');
5
  
6
  /**
7
   * Information about the originating feed if an entry is copied or aggregated
8
   * from another feed.
9
   * 
10
   * @see {@link https://github.com/FamilySearch/gedcomx-rs/blob/master/specifications/atom-model-specification.md#atom-json-media-type|GEDCOM X Atom JSON Spec}
11
   * @see {@link https://tools.ietf.org/html/rfc4287#section-4.2.11|RFC 4287}
12
   * 
13
   * @class AtomSource
14
   * @extends AtomCommon
15
   * @param {Object} [json]
16
   */
17
  var AtomSource = function(json){
18
    
19
    // Protect against forgetting the new keyword when calling the constructor
20
    if(!(this instanceof AtomSource)){
21
      return new AtomSource(json);
22
    }
23
    
24
    // If the given object is already an instance then just return it. DON'T copy it.
25
    if(AtomSource.isInstance(json)){
26
      return json;
27
    }
28
    
29
    this.init(json);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
30
  };
31
  
32
  AtomSource.prototype = Object.create(AtomCommon.prototype);
33
  
34
  AtomSource._gedxClass = AtomSource.prototype._gedxClass = 'GedcomX.AtomSource';
35
  
36
  AtomSource.jsonProps = [
37
    'authors',
38
    'contributors',
39
    'categories',
40
    'generator',
41
    'icon',
42
    'id',
43
    'links',
44
    'logo',
45
    'rights',
46
    'subtitle',
47
    'title',
48
    'updated'
49
  ];
50
  
51
  /**
52
   * Check whether the given object is an instance of this class.
53
   * 
54
   * @param {Object} obj
55
   * @returns {Boolean}
56
   */
57
  AtomSource.isInstance = function(obj){
58
    return utils.isInstance(obj, this._gedxClass);
59
  };
60
61
  /**
62
   * Initialize from JSON
63
   * 
64
   * @param {Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
65
   * @return {AtomSource} this
66
   */
67
  AtomSource.prototype.init = function(json){
68
    
69
    AtomCommon.prototype.init.call(this, json);
70
    
71
    if(json){
72
      this.setAuthors(json.authors);
73
      this.setContributors(json.contributors);
74
      this.setCategories(json.categories);
75
      this.setGenerator(json.generator);
76
      this.setIcon(json.icon);
77
      this.setId(json.id);
78
      this.setLinks(json.links);
79
      this.setLogo(json.logo);
80
      this.setRights(json.rights);
81
      this.setSubtitle(json.subtitle);
82
      this.setTitle(json.title);
83
      this.setUpdated(json.updated);
84
    }
85
    return this;
86
  };
87
  
88
  /**
89
   * Set the authors
90
   * 
91
   * @param {AtomPerson[]} authors
92
   * @return {AtomSource} this
93
   */
94
  AtomSource.prototype.setAuthors = function(authors){
95
    return this._setArray(authors, 'authors', 'addAuthor');
96
  };
97
  
98
  /**
99
   * Add an author
100
   * 
101
   * @param {AtomPerson} author
102
   * @return {AtomSource} this
103
   */
104
  AtomSource.prototype.addAuthor = function(author){
105
    return this._arrayPush(author, 'authors', GedcomX.AtomPerson);
106
  };
107
  
108
  /**
109
   * Get the authors
110
   * 
111
   * @return {AtomPerson[]} authors
112
   */
113
  AtomSource.prototype.getAuthors = function(){
114
    return this.authors || [];
115
  };
116
  
117
  /**
118
   * Set the contributors
119
   * 
120
   * @param {AtomPerson[]} contributors
121
   * @return {AtomSource} this
122
   */
123
  AtomSource.prototype.setContributors = function(contributors){
124
    return this._setArray(contributors, 'contributors', 'addContributor');
125
  };
126
  
127
  /**
128
   * Add a contributor
129
   * 
130
   * @param {AtomPerson} contributor
131
   * @return {AtomSource} this
132
   */
133
  AtomSource.prototype.addContributor = function(contributor){
134
    return this._arrayPush(contributor, 'contributors', GedcomX.AtomPerson);
135
  };
136
  
137
  /**
138
   * Get the contributors
139
   * 
140
   * @return {AtomPerson[]} contributors
141
   */
142
  AtomSource.prototype.getContributors = function(){
143
    return this.contributors || [];
144
  };
145
  
146
  /**
147
   * Set the categories
148
   * 
149
   * @param {AtomCategory[]} categories
150
   * @return {AtomSource} this
151
   */
152
  AtomSource.prototype.setCategories = function(categories){
153
    return this._setArray(categories, 'categories', 'addCategory');
154
  };
155
  
156
  /**
157
   * Add a category
158
   * 
159
   * @param {AtomCategory} category
160
   * @return {AtomSource} this
161
   */
162
  AtomSource.prototype.addCategory = function(category){
163
    return this._arrayPush(category, 'categories', GedcomX.AtomCategory);
164
  };
165
  
166
  /**
167
   * Get the categories
168
   * 
169
   * @return {AtomCategory[]} categories
170
   */
171
  AtomSource.prototype.getCategories = function(){
172
    return this.categories || [];
173
  };
174
  
175
  /**
176
   * Set the generator
177
   * 
178
   * @param {AtomGenerator} generator
179
   * @return {AtomSource}
180
   */
181
  AtomSource.prototype.setGenerator = function(generator){
182
    if(generator){
183
      this.generator = GedcomX.AtomGenerator(generator);
184
    }
185
    return this;
186
  };
187
  
188
  /**
189
   * Get the generator
190
   * 
191
   * @return {AtomGenerator} generator
192
   */
193
  AtomSource.prototype.getGenerator = function(){
194
    return this.generator;
195
  };
196
  
197
  /**
198
   * Set the icon
199
   * 
200
   * @param {String} icon
201
   * @return {AtomSource}
202
   */
203
  AtomSource.prototype.setIcon = function(icon){
204
    this.icon = icon;
205
    return this;
206
  };
207
  
208
  /**
209
   * Get the icon
210
   * 
211
   * @return {String} icon
212
   */
213
  AtomSource.prototype.getIcon = function(){
214
    return this.icon;
215
  };
216
  
217
  /**
218
   * Set the id
219
   * 
220
   * @param {String} id
221
   * @return {AtomSource}
222
   */
223
  AtomSource.prototype.setId = function(id){
224
    this.id = id;
225
    return this;
226
  };
227
  
228
  /**
229
   * Get the id
230
   * 
231
   * @return {String} id
232
   */
233
  AtomSource.prototype.getId = function(){
234
    return this.id;
235
  };
236
  
237
  /**
238
   * Set the links
239
   * 
240
   * @param {Links} links
241
   * @return {AtomSource} this
242
   */
243
  AtomSource.prototype.setLinks = function(links){
244
    if(links){
245
      this.links = GedcomX.Links(links);
246
    }
247
    return this;
248
  };
249
  
250
  /**
251
   * Add a link
252
   * 
253
   * @param {Link} link
254
   * @return {AtomSource} this
255
   */
256
  AtomSource.prototype.addLink = function(link){
257
    if(link){
258
      if(!this.links){
259
        this.links = GedcomX.Links();
260
      }
261
      this.links.addLink(link);
262
    }
263
    return this;
264
  };
265
  
266
  /**
267
   * Get the links
268
   * 
269
   * @return {Link[]}
270
   */
271
  AtomSource.prototype.getLinks = function(){
272
    return this.links ? this.links.getLinks() : [];
273
  };
274
  
275
  /**
276
   * Get a link
277
   * 
278
   * @param {String} rel
279
   * @return {Links}
280
   */
281
  AtomSource.prototype.getLink = function(rel){
282
    if(this.links){
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if this.links is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
283
      return this.links.getLink(rel);
284
    }
285
  };
286
  
287
  /**
288
   * Set the logo
289
   * 
290
   * @param {String} logo
291
   * @return {AtomSource}
292
   */
293
  AtomSource.prototype.setLogo = function(logo){
294
    this.logo = logo;
295
    return this;
296
  };
297
  
298
  /**
299
   * Get the logo
300
   * 
301
   * @return {String} logo
302
   */
303
  AtomSource.prototype.getLogo = function(){
304
    return this.logo;
305
  };
306
  
307
  /**
308
   * Set the rights
309
   * 
310
   * @param {String} rights
311
   * @return {AtomSource}
312
   */
313
  AtomSource.prototype.setRights = function(rights){
314
    this.rights = rights;
315
    return this;
316
  };
317
  
318
  /**
319
   * Get the rights
320
   * 
321
   * @return {String} rights
322
   */
323
  AtomSource.prototype.getRights = function(){
324
    return this.rights;
325
  };
326
  
327
  /**
328
   * Set the subtitle
329
   * 
330
   * @param {String} subtitle
331
   * @return {AtomSource}
332
   */
333
  AtomSource.prototype.setSubtitle = function(subtitle){
334
    this.subtitle = subtitle;
335
    return this;
336
  };
337
  
338
  /**
339
   * Get the subtitle
340
   * 
341
   * @return {String} subtitle
342
   */
343
  AtomSource.prototype.getSubtitle = function(){
344
    return this.subtitle;
345
  };
346
  
347
  /**
348
   * Set the title
349
   * 
350
   * @param {String} title
351
   * @return {AtomSource}
352
   */
353
  AtomSource.prototype.setTitle = function(title){
354
    this.title = title;
355
    return this;
356
  };
357
  
358
  /**
359
   * Get the title
360
   * 
361
   * @return {String} title
362
   */
363
  AtomSource.prototype.getTitle = function(){
364
    return this.title;
365
  };
366
367
  /**
368
   * Get the updated timestamp
369
   * 
370
   * @returns {Date} updated
371
   */
372
  AtomSource.prototype.getUpdated = function(){
373
    return this.updated;
374
  };
375
  
376
  /**
377
   * Set the updated timestamp
378
   * 
379
   * @param {Date|Number} date Integer timestamp (milliseconds since epoch) or JavaScript Date instance
380
   * @returns {AtomSource} This instance
381
   */
382
  AtomSource.prototype.setUpdated = function(date){
383
    if(date){
384
      this.updated = new Date(date);
385
    }
386
    return this;
387
  };
388
  
389
  /**
390
   * Export the object as JSON
391
   * 
392
   * @return {Object} JSON object
393
   */
394
  AtomSource.prototype.toJSON = function(){
395
    return this._toJSON(AtomCommon, AtomSource.jsonProps);
396
  };
397
  
398
  GedcomX.AtomSource = AtomSource;
399
400
};