Completed
Push — master ( e193d1...415e1b )
by Justin
01:32
created

src/atom/AtomSource.js   B

Complexity

Total Complexity 44
Complexity/F 1.29

Size

Lines of Code 396
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 396
rs 8.3396
wmc 44
mnd 2
bc 43
fnc 34
bpm 1.2647
cpm 1.2941
noi 4

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
   * @constructor
11
   * @param {Object} [json]
0 ignored issues
show
Documentation introduced by
The parameter [json] does not exist. Did you maybe forget to remove this comment?
Loading history...
12
   */
13
  var AtomSource = function(json){
14
    
15
    // Protect against forgetting the new keyword when calling the constructor
16
    if(!(this instanceof AtomSource)){
17
      return new AtomSource(json);
18
    }
19
    
20
    // If the given object is already an instance then just return it. DON'T copy it.
21
    if(AtomSource.isInstance(json)){
22
      return json;
23
    }
24
    
25
    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...
26
  };
27
  
28
  AtomSource.prototype = Object.create(AtomCommon.prototype);
29
  
30
  AtomSource._gedxClass = AtomSource.prototype._gedxClass = 'GedcomX.AtomSource';
31
  
32
  AtomSource.jsonProps = [
33
    'authors',
34
    'contributors',
35
    'categories',
36
    'generator',
37
    'icon',
38
    'id',
39
    'links',
40
    'logo',
41
    'rights',
42
    'subtitle',
43
    'title',
44
    'updated'
45
  ];
46
  
47
  /**
48
   * Check whether the given object is an instance of this class.
49
   * 
50
   * @param {Object} obj
51
   * @returns {Boolean}
52
   */
53
  AtomSource.isInstance = function(obj){
54
    return utils.isInstance(obj, this._gedxClass);
55
  };
56
57
  /**
58
   * Initialize from JSON
59
   * 
60
   * @param {Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
61
   * @return {AtomSource} this
62
   */
63
  AtomSource.prototype.init = function(json){
64
    
65
    AtomCommon.prototype.init.call(this, json);
66
    
67
    if(json){
68
      this.setAuthors(json.authors);
69
      this.setContributors(json.contributors);
70
      this.setCategories(json.categories);
71
      this.setGenerator(json.generator);
72
      this.setIcon(json.icon);
73
      this.setId(json.id);
74
      this.setLinks(json.links);
75
      this.setLogo(json.logo);
76
      this.setRights(json.rights);
77
      this.setSubtitle(json.subtitle);
78
      this.setTitle(json.title);
79
      this.setUpdated(json.updated);
80
    }
81
    return this;
82
  };
83
  
84
  /**
85
   * Set the authors
86
   * 
87
   * @param {AtomPerson[]} authors
88
   * @return {AtomSource} this
89
   */
90
  AtomSource.prototype.setAuthors = function(authors){
91
    return this._setArray(authors, 'authors', 'addAuthor');
92
  };
93
  
94
  /**
95
   * Add an author
96
   * 
97
   * @param {AtomPerson} author
98
   * @return {AtomSource} this
99
   */
100
  AtomSource.prototype.addAuthor = function(author){
101
    return this._arrayPush(author, 'authors', GedcomX.AtomPerson);
102
  };
103
  
104
  /**
105
   * Get the authors
106
   * 
107
   * @return {AtomPerson[]} authors
108
   */
109
  AtomSource.prototype.getAuthors = function(){
110
    return this.authors || [];
111
  };
112
  
113
  /**
114
   * Set the contributors
115
   * 
116
   * @param {AtomPerson[]} contributors
117
   * @return {AtomSource} this
118
   */
119
  AtomSource.prototype.setContributors = function(contributors){
120
    return this._setArray(contributors, 'contributors', 'addContributor');
121
  };
122
  
123
  /**
124
   * Add a contributor
125
   * 
126
   * @param {AtomPerson} contributor
127
   * @return {AtomSource} this
128
   */
129
  AtomSource.prototype.addContributor = function(contributor){
130
    return this._arrayPush(contributor, 'contributors', GedcomX.AtomPerson);
131
  };
132
  
133
  /**
134
   * Get the contributors
135
   * 
136
   * @return {AtomPerson[]} contributors
137
   */
138
  AtomSource.prototype.getContributors = function(){
139
    return this.contributors || [];
140
  };
141
  
142
  /**
143
   * Set the categories
144
   * 
145
   * @param {AtomCategory[]} categories
146
   * @return {AtomSource} this
147
   */
148
  AtomSource.prototype.setCategories = function(categories){
149
    return this._setArray(categories, 'categories', 'addCategory');
150
  };
151
  
152
  /**
153
   * Add a category
154
   * 
155
   * @param {AtomCategory} category
156
   * @return {AtomSource} this
157
   */
158
  AtomSource.prototype.addCategory = function(category){
159
    return this._arrayPush(category, 'categories', GedcomX.AtomCategory);
160
  };
161
  
162
  /**
163
   * Get the categories
164
   * 
165
   * @return {AtomCategory[]} categories
166
   */
167
  AtomSource.prototype.getCategories = function(){
168
    return this.categories || [];
169
  };
170
  
171
  /**
172
   * Set the generator
173
   * 
174
   * @param {AtomGenerator} generator
175
   * @return {AtomSource}
176
   */
177
  AtomSource.prototype.setGenerator = function(generator){
178
    if(generator){
179
      this.generator = GedcomX.AtomGenerator(generator);
180
    }
181
    return this;
182
  };
183
  
184
  /**
185
   * Get the generator
186
   * 
187
   * @return {AtomGenerator} generator
188
   */
189
  AtomSource.prototype.getGenerator = function(){
190
    return this.generator;
191
  };
192
  
193
  /**
194
   * Set the icon
195
   * 
196
   * @param {String} icon
197
   * @return {AtomSource}
198
   */
199
  AtomSource.prototype.setIcon = function(icon){
200
    this.icon = icon;
201
    return this;
202
  };
203
  
204
  /**
205
   * Get the icon
206
   * 
207
   * @return {String} icon
208
   */
209
  AtomSource.prototype.getIcon = function(){
210
    return this.icon;
211
  };
212
  
213
  /**
214
   * Set the id
215
   * 
216
   * @param {String} id
217
   * @return {AtomSource}
218
   */
219
  AtomSource.prototype.setId = function(id){
220
    this.id = id;
221
    return this;
222
  };
223
  
224
  /**
225
   * Get the id
226
   * 
227
   * @return {String} id
228
   */
229
  AtomSource.prototype.getId = function(){
230
    return this.id;
231
  };
232
  
233
  /**
234
   * Set the links
235
   * 
236
   * @param {Links} links
237
   * @return {AtomSource} this
238
   */
239
  AtomSource.prototype.setLinks = function(links){
240
    if(links){
241
      this.links = GedcomX.Links(links);
242
    }
243
    return this;
244
  };
245
  
246
  /**
247
   * Add a link
248
   * 
249
   * @param {Link} link
250
   * @return {AtomSource} this
251
   */
252
  AtomSource.prototype.addLink = function(link){
253
    if(link){
254
      if(!this.links){
255
        this.links = GedcomX.Links();
256
      }
257
      this.links.addLink(link);
258
    }
259
    return this;
260
  };
261
  
262
  /**
263
   * Get the links
264
   * 
265
   * @return {Link[]}
266
   */
267
  AtomSource.prototype.getLinks = function(){
268
    return this.links ? this.links.getLinks() : [];
269
  };
270
  
271
  /**
272
   * Get a link
273
   * 
274
   * @param {String} rel
275
   * @return {Links}
276
   */
277
  AtomSource.prototype.getLink = function(rel){
278
    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...
279
      return this.links.getLink(rel);
280
    }
281
  };
282
  
283
  /**
284
   * Set the logo
285
   * 
286
   * @param {String} logo
287
   * @return {AtomSource}
288
   */
289
  AtomSource.prototype.setLogo = function(logo){
290
    this.logo = logo;
291
    return this;
292
  };
293
  
294
  /**
295
   * Get the logo
296
   * 
297
   * @return {String} logo
298
   */
299
  AtomSource.prototype.getLogo = function(){
300
    return this.logo;
301
  };
302
  
303
  /**
304
   * Set the rights
305
   * 
306
   * @param {String} rights
307
   * @return {AtomSource}
308
   */
309
  AtomSource.prototype.setRights = function(rights){
310
    this.rights = rights;
311
    return this;
312
  };
313
  
314
  /**
315
   * Get the rights
316
   * 
317
   * @return {String} rights
318
   */
319
  AtomSource.prototype.getRights = function(){
320
    return this.rights;
321
  };
322
  
323
  /**
324
   * Set the subtitle
325
   * 
326
   * @param {String} subtitle
327
   * @return {AtomSource}
328
   */
329
  AtomSource.prototype.setSubtitle = function(subtitle){
330
    this.subtitle = subtitle;
331
    return this;
332
  };
333
  
334
  /**
335
   * Get the subtitle
336
   * 
337
   * @return {String} subtitle
338
   */
339
  AtomSource.prototype.getSubtitle = function(){
340
    return this.subtitle;
341
  };
342
  
343
  /**
344
   * Set the title
345
   * 
346
   * @param {String} title
347
   * @return {AtomSource}
348
   */
349
  AtomSource.prototype.setTitle = function(title){
350
    this.title = title;
351
    return this;
352
  };
353
  
354
  /**
355
   * Get the title
356
   * 
357
   * @return {String} title
358
   */
359
  AtomSource.prototype.getTitle = function(){
360
    return this.title;
361
  };
362
363
  /**
364
   * Get the updated timestamp
365
   * 
366
   * @returns {Date} updated
367
   */
368
  AtomSource.prototype.getUpdated = function(){
369
    return this.updated;
370
  };
371
  
372
  /**
373
   * Set the updated timestamp
374
   * 
375
   * @param {Date|Number} date Integer timestamp (milliseconds since epoch) or JavaScript Date instance
376
   * @returns {AtomSource} This instance
377
   */
378
  AtomSource.prototype.setUpdated = function(date){
379
    if(date){
380
      this.updated = new Date(date);
381
    }
382
    return this;
383
  };
384
  
385
  /**
386
   * Export the object as JSON
387
   * 
388
   * @return {Object} JSON object
389
   */
390
  AtomSource.prototype.toJSON = function(){
391
    return this._toJSON(AtomCommon, AtomSource.jsonProps);
392
  };
393
  
394
  GedcomX.AtomSource = AtomSource;
395
396
};