src/atom/AtomCommon.js   A
last analyzed

Complexity

Total Complexity 11
Complexity/F 1.38

Size

Lines of Code 113
Function Count 8

Duplication

Duplicated Lines 110
Ratio 97.35 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 110
loc 113
rs 10
wmc 11
mnd 1
bc 11
fnc 8
bpm 1.375
cpm 1.375
noi 2

8 Functions

Rating   Name   Duplication   Size   Complexity  
A AtomCommon.getLang 3 3 1
A AtomCommon.init 10 10 2
A AtomCommon.isInstance 3 3 1
A AtomCommon.js ➔ AtomCommon 14 14 3
A AtomCommon.toJSON 2 3 1
A AtomCommon.setBase 4 4 1
A AtomCommon.getBase 3 3 1
A AtomCommon.setLang 4 4 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 View Code Duplication
var utils = require('../utils'),
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
    Base = require('../Base');
3
4
/**
5
 * Common attributes for all Atom entities
6
 * 
7
 * @see {@link https://tools.ietf.org/html/rfc4287#page-7|RFC 4287}
8
 * 
9
 * @class AtomCommon
10
 * @extends Base
11
 * @param {Object} [json]
12
 */
13
var AtomCommon = function(json){
14
  
15
  // Protect against forgetting the new keyword when calling the constructor
16
  if(!(this instanceof AtomCommon)){
17
    return new AtomCommon(json);
18
  }
19
  
20
  // If the given object is already an instance then just return it. DON'T copy it.
21
  if(AtomCommon.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
AtomCommon.prototype = Object.create(Base.prototype);
29
30
AtomCommon._gedxClass = AtomCommon.prototype._gedxClass = 'GedcomX.AtomCommon';
31
32
AtomCommon.jsonProps = [
33
  'base',
34
  'lang'
35
];
36
37
/**
38
 * Check whether the given object is an instance of this class.
39
 * 
40
 * @param {Object} obj
41
 * @returns {Boolean}
42
 */
43
AtomCommon.isInstance = function(obj){
44
  return utils.isInstance(obj, this._gedxClass);
45
};
46
47
/**
48
 * Initialize from JSON
49
 * 
50
 * @param {Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
51
 * @return {AtomCommon} this
52
 */
53
AtomCommon.prototype.init = function(json){
54
  
55
  Base.prototype.init.call(this, json);
56
  
57
  if(json){
58
    this.setBase(json.base);
59
    this.setLang(json.lang);
60
  }
61
  return this;
62
};
63
64
/**
65
 * Set the base attribute
66
 * 
67
 * @param {String} base
68
 * @return {AtomCommon} this
69
 */
70
AtomCommon.prototype.setBase = function(base){
71
  this.base = base;
72
  return this;
73
};
74
75
/**
76
 * Get the base
77
 * 
78
 * @return {String} base
79
 */
80
AtomCommon.prototype.getBase = function(base){
81
  return this.base;
82
};
83
84
/**
85
 * Set the lang
86
 * 
87
 * @param {String} lang
88
 * @return {AtomCommon} this
89
 */
90
AtomCommon.prototype.setLang = function(lang){
91
  this.lang = lang;
92
  return this;
93
};
94
95
/**
96
 * Get the lang
97
 * 
98
 * @return {String} lang
99
 */
100
AtomCommon.prototype.getLang = function(lang){
101
  return this.lang;
102
};
103
104
/**
105
 * Export the object as JSON
106
 * 
107
 * @return {Object} JSON object
108
 */
109
AtomCommon.prototype.toJSON = function(){
110
  return this._toJSON(Base, AtomCommon.jsonProps);
111
};
112
113
module.exports = AtomCommon;
114