Completed
Push — master ( e3ee1d...5cbb1b )
by Justin
01:36
created

src/core/Event.js   A

Complexity

Total Complexity 18
Complexity/F 1.38

Size

Lines of Code 167
Function Count 13

Duplication

Duplicated Lines 167
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 2
dl 167
loc 167
rs 10
wmc 18
mnd 1
bc 18
fnc 13
bpm 1.3846
cpm 1.3846
noi 3

13 Functions

Rating   Name   Duplication   Size   Complexity  
A Event.setRoles 3 3 1
A Event.getDate 3 3 1
A Event.setPlace 6 6 2
A Event.setType 4 4 1
A Event.getPlace 3 3 1
A Event.js ➔ Event 14 14 3
A Event.isInstance 3 3 1
A Event.toJSON 3 3 1
A Event.getType 3 3 1
A Event.setDate 6 6 2
A Event.getRoles 3 3 1
A Event.init 12 12 2
A Event.addRole 3 3 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 GedcomX = require('../'),
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
    utils = require('../utils');
3
4
/**
5
 * An event.
6
 * 
7
 * @constructor
8
 * @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...
9
 */
10
var Event = function(json){
11
  
12
  // Protect against forgetting the new keyword when calling the constructor
13
  if(!(this instanceof Event)){
14
    return new Event(json);
15
  }
16
  
17
  // If the given object is already an instance then just return it. DON'T copy it.
18
  if(Event.isInstance(json)){
19
    return json;
20
  }
21
  
22
  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...
23
};
24
25
Event.prototype = Object.create(GedcomX.Subject.prototype);
26
27
Event._gedxClass = Event.prototype._gedxClass = 'GedcomX.Event';
28
29
Event.jsonProps = [
30
  'type',
31
  'date',
32
  'place',
33
  'roles'
34
];
35
36
/**
37
 * Check whether the given object is an instance of this class.
38
 * 
39
 * @param {Object} obj
40
 * @returns {Boolean}
41
 */
42
Event.isInstance = function(obj){
43
  return utils.isInstance(obj, this._gedxClass);
44
};
45
46
/**
47
 * Initialize from JSON
48
 * 
49
 * @param {Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
50
 * @return {Event} this
51
 */
52
Event.prototype.init = function(json){
53
  
54
  GedcomX.Subject.prototype.init.call(this, json);
55
  
56
  if(json){
57
    this.setType(json.type);
58
    this.setDate(json.date);
59
    this.setPlace(json.place);
60
    this.setRoles(json.roles);
61
  }
62
  return this;
63
};
64
65
/**
66
 * Get the type
67
 * 
68
 * @returns {String}
69
 */
70
Event.prototype.getType = function(){
71
  return this.type;
72
};
73
74
/**
75
 * Set the type
76
 * 
77
 * @param {String} type
78
 * @returns {Event}
79
 */
80
Event.prototype.setType = function(type){
81
  this.type = type;
82
  return this;
83
};
84
85
/**
86
 * Get the date
87
 * 
88
 * @returns {Date}
89
 */
90
Event.prototype.getDate = function(){
91
  return this.date;
92
};
93
94
/**
95
 * Set the date
96
 * 
97
 * @param {Date} date
98
 * @returns {Event}
99
 */
100
Event.prototype.setDate = function(date){
101
  if(date){
102
    this.date = GedcomX.Date(date);
103
  }
104
  return this;
105
};
106
107
/**
108
 * Get the place
109
 * 
110
 * @returns {PlaceReference}
111
 */
112
Event.prototype.getPlace = function(){
113
  return this.place;
114
};
115
116
/**
117
 * Set the place
118
 * 
119
 * @param {PlaceReference} place
120
 * @returns {Event}
121
 */
122
Event.prototype.setPlace = function(place){
123
  if(place){
124
    this.place = GedcomX.PlaceReference(place);
125
  }
126
  return this;
127
};
128
129
/**
130
 * Get the roles
131
 * 
132
 * @returns {EventRole[]}
133
 */
134
Event.prototype.getRoles = function(){
135
  return this.roles || [];
136
};
137
138
/**
139
 * Set the roles
140
 * 
141
 * @param {EventRole[]|Object[]} roles
142
 * @returns {Event}
143
 */
144
Event.prototype.setRoles = function(roles){
145
  return this._setArray(roles, 'roles', 'addRole');
146
};
147
148
/**
149
 * Add a role
150
 * 
151
 * @param {EventRole|Object} role
152
 * @returns {Event}
153
 */
154
Event.prototype.addRole = function(role){
155
  return this._arrayPush(role, 'roles', GedcomX.EventRole);
156
};
157
158
/**
159
 * Export the object as JSON
160
 * 
161
 * @return {Object} JSON object
162
 */
163
Event.prototype.toJSON = function(){
164
  return this._toJSON(GedcomX.Subject, Event.jsonProps);
165
};
166
167
module.exports = Event;