Test Failed
Branch master (4a3c5b)
by Greg
12:31
created

GedcomCodeRela   F

Complexity

Total Complexity 75

Size/Duplication

Total Lines 263
Duplicated Lines 9.89 %

Importance

Changes 0
Metric Value
dl 26
loc 263
rs 2.3076
c 0
b 0
f 0
wmc 75

2 Methods

Rating   Name   Duplication   Size   Complexity  
D getValue() 18 223 73
A getValues() 8 8 2

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like GedcomCodeRela 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.

While breaking up the class, it is a good idea to analyze how other classes use GedcomCodeRela, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * webtrees: online genealogy
4
 * Copyright (C) 2017 webtrees development team
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
 * GNU General Public License for more details.
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15
 */
16
namespace Fisharebest\Webtrees\GedcomCode;
17
18
use Fisharebest\Webtrees\GedcomRecord;
19
use Fisharebest\Webtrees\I18N;
20
use Fisharebest\Webtrees\Individual;
21
22
/**
23
 * Class GedcomCodeRela - Functions and logic for GEDCOM "RELA" codes
24
 */
25
class GedcomCodeRela {
26
	/** @var string[] List of possible values for the RELA tag */
27
	private static $TYPES = [
28
		'attendant', 'attending', 'best_man', 'bridesmaid', 'buyer',
29
		'circumciser', 'civil_registrar', 'employee', 'employer', 'foster_child',
30
		'foster_father', 'foster_mother', 'friend', 'godfather', 'godmother',
31
		'godparent', 'godson', 'goddaughter', 'godchild', 'guardian',
32
		'informant', 'lodger', 'nanny', 'nurse', 'owner',
33
		'priest', 'rabbi', 'registry_officer', 'seller', 'servant',
34
		'slave', 'ward', 'witness',
35
	];
36
37
	/**
38
	 * Translate a code, for an (optional) record.
39
	 * We need the record to translate the sex (godfather/godmother) but
40
	 * we won’t have this when adding data for new individuals.
41
	 *
42
	 * @param string            $type
43
	 * @param GedcomRecord|null $record
44
	 *
45
	 * @return string
46
	 */
47
	public static function getValue($type, GedcomRecord $record = null) {
48
		if ($record instanceof Individual) {
49
			$sex = $record->getSex();
50
		} else {
51
			$sex = 'U';
52
		}
53
54
		switch ($type) {
55
		case 'attendant':
56
			switch ($sex) {
57
			case 'M':
58
				return I18N::translateContext('MALE', 'Attendant');
59
			case 'F':
60
				return I18N::translateContext('FEMALE', 'Attendant');
61
			default:
62
				return I18N::translate('Attendant');
63
			}
64
		case 'attending':
65
			switch ($sex) {
66
			case 'M':
67
				return I18N::translateContext('MALE', 'Attending');
68
			case 'F':
69
				return I18N::translateContext('FEMALE', 'Attending');
70
			default:
71
				return I18N::translate('Attending');
72
			}
73
		case 'best_man':
74
			// always male
75
			return I18N::translate('Best man');
76
		case 'bridesmaid':
77
			// always female
78
			return I18N::translate('Bridesmaid');
79
		case 'buyer':
80
			switch ($sex) {
81
			case 'M':
82
				return I18N::translateContext('MALE', 'Buyer');
83
			case 'F':
84
				return I18N::translateContext('FEMALE', 'Buyer');
85
			default:
86
				return I18N::translate('Buyer');
87
			}
88
		case 'circumciser':
89
			// always male
90
			return I18N::translate('Circumciser');
91
		case 'civil_registrar':
92
			switch ($sex) {
93
			case 'M':
94
				return I18N::translateContext('MALE', 'Civil registrar');
95
			case 'F':
96
				return I18N::translateContext('FEMALE', 'Civil registrar');
97
			default:
98
				return I18N::translate('Civil registrar');
99
			}
100
		case 'employee':
101
			switch ($sex) {
102
			case 'M':
103
				return I18N::translateContext('MALE', 'Employee');
104
			case 'F':
105
				return I18N::translateContext('FEMALE', 'Employee');
106
			default:
107
				return I18N::translate('Employee');
108
			}
109
		case 'employer':
110
			switch ($sex) {
111
			case 'M':
112
				return I18N::translateContext('MALE', 'Employer');
113
			case 'F':
114
				return I18N::translateContext('FEMALE', 'Employer');
115
			default:
116
				return I18N::translate('Employer');
117
			}
118
		case 'foster_child':
119
			// no sex implied
120
			return I18N::translate('Foster child');
121
		case 'foster_father':
122
			// always male
123
			return I18N::translate('Foster father');
124
		case 'foster_mother':
125
			// always female
126
			return I18N::translate('Foster mother');
127
		case 'friend':
128
			switch ($sex) {
129
			case 'M':
130
				return I18N::translateContext('MALE', 'Friend');
131
			case 'F':
132
				return I18N::translateContext('FEMALE', 'Friend');
133
			default:
134
				return I18N::translate('Friend');
135
			}
136
		case 'godfather':
137
			// always male
138
			return I18N::translate('Godfather');
139
		case 'godmother':
140
			// always female
141
			return I18N::translate('Godmother');
142
		case 'godparent':
143
			switch ($sex) {
144
			case 'M':
145
				return I18N::translate('Godfather');
146
			case 'F':
147
				return I18N::translate('Godmother');
148
			default:
149
				return I18N::translate('Godparent');
150
			}
151
		case 'godson':
152
			// always male
153
			return I18N::translate('Godson');
154
		case 'goddaughter':
155
			// always female
156
			return I18N::translate('Goddaughter');
157 View Code Duplication
		case 'godchild':
158
			switch ($sex) {
159
			case 'M':
160
				return I18N::translate('Godson');
161
			case 'F':
162
				return I18N::translate('Goddaughter');
163
			default:
164
				return I18N::translate('Godchild');
165
			}
166
		case 'guardian':
167
			switch ($sex) {
168
			case 'M':
169
				return I18N::translateContext('MALE', 'Guardian');
170
			case 'F':
171
				return I18N::translateContext('FEMALE', 'Guardian');
172
			default:
173
				return I18N::translate('Guardian');
174
			}
175
		case 'informant':
176
			switch ($sex) {
177
			case 'M':
178
				return I18N::translateContext('MALE', 'Informant');
179
			case 'F':
180
				return I18N::translateContext('FEMALE', 'Informant');
181
			default:
182
				return I18N::translate('Informant');
183
			}
184
		case 'lodger':
185
			switch ($sex) {
186
			case 'M':
187
				return I18N::translateContext('MALE', 'Lodger');
188
			case 'F':
189
				return I18N::translateContext('FEMALE', 'Lodger');
190
			default:
191
				return I18N::translate('Lodger');
192
			}
193
		case 'nanny':
194
			// no sex implied
195
			return I18N::translate('Nanny');
196
		case 'nurse':
197
			switch ($sex) {
198
			case 'M':
199
				return I18N::translateContext('MALE', 'Nurse');
200
			case 'F':
201
				return I18N::translateContext('FEMALE', 'Nurse');
202
			default:
203
				return I18N::translate('Nurse');
204
			}
205
		case 'owner':
206
			switch ($sex) {
207
			case 'M':
208
				return I18N::translateContext('MALE', 'Owner');
209
			case 'F':
210
				return I18N::translateContext('FEMALE', 'Owner');
211
			default:
212
				return I18N::translate('Owner');
213
			}
214
		case 'priest':
215
			// no sex implied
216
			return I18N::translate('Priest');
217
		case 'rabbi':
218
			// always male
219
			return I18N::translate('Rabbi');
220
		case 'registry_officer':
221
			switch ($sex) {
222
			case 'M':
223
				return I18N::translateContext('MALE', 'Registry officer');
224
			case 'F':
225
				return I18N::translateContext('FEMALE', 'Registry officer');
226
			default:
227
				return I18N::translate('Registry officer');
228
			}
229
		case 'seller':
230
			switch ($sex) {
231
			case 'M':
232
				return I18N::translateContext('MALE', 'Seller');
233
			case 'F':
234
				return I18N::translateContext('FEMALE', 'Seller');
235
			default:
236
				return I18N::translate('Seller');
237
			}
238
		case 'servant':
239
			switch ($sex) {
240
			case 'M':
241
				return I18N::translateContext('MALE', 'Servant');
242
			case 'F':
243
				return I18N::translateContext('FEMALE', 'Servant');
244
			default:
245
				return I18N::translate('Servant');
246
			}
247
		case 'slave':
248
			switch ($sex) {
249
			case 'M':
250
				return I18N::translateContext('MALE', 'Slave');
251
			case 'F':
252
				return I18N::translateContext('FEMALE', 'Slave');
253
			default:
254
				return I18N::translate('Slave');
255
			}
256 View Code Duplication
		case 'ward':
257
			switch ($sex) {
258
			case 'M':
259
				return I18N::translateContext('MALE', 'Ward');
260
			case 'F':
261
				return I18N::translateContext('FEMALE', 'Ward');
262
			default:
263
				return I18N::translate('Ward');
264
			}
265
		case 'witness':
266
			// Do we need separate male/female translations for this?
267
			return I18N::translate('Witness');
268
		default:
269
			return I18N::translate($type);
270
		}
271
	}
272
273
	/**
274
	 * A list of all possible values for RELA
275
	 *
276
	 * @param GedcomRecord|null $record
277
	 *
278
	 * @return string[]
279
	 */
280 View Code Duplication
	public static function getValues(GedcomRecord $record = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
281
		$values = [];
282
		foreach (self::$TYPES as $type) {
283
			$values[$type] = self::getValue($type, $record);
284
		}
285
		uasort($values, '\Fisharebest\Webtrees\I18N::strcasecmp');
286
287
		return $values;
288
	}
289
}
290