Completed
Push — develop ( 9087a8...c9b4ef )
by Greg
16:31 queued 05:44
created

GedcomCodeRela   F

Complexity

Total Complexity 75

Size/Duplication

Total Lines 263
Duplicated Lines 0 %

Importance

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

2 Methods

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

How to fix   Complexity   

Complex Class

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) 2018 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':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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
			case 'godchild':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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
			case 'ward':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
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
	public static function getValues(GedcomRecord $record = null) {
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