Passed
Branch master (6a7148)
by Curtis
01:48
created

Indi   F

Complexity

Total Complexity 67

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 148
dl 0
loc 171
rs 3.04
c 0
b 0
f 0
wmc 67

1 Method

Rating   Name   Duplication   Size   Complexity  
F parse() 0 166 67

How to fix   Complexity   

Complex Class

Complex classes like Indi 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 Indi, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * php-gedcom
4
 *
5
 * php-gedcom is a library for parsing, manipulating, importing and exporting
6
 * GEDCOM 5.5 files in PHP 5.3+.
7
 *
8
 * @author          Kristopher Wilson <[email protected]>
9
 * @copyright       Copyright (c) 2010-2013, Kristopher Wilson
10
 * @package         php-gedcom
11
 * @license         MIT
12
 * @link            http://github.com/mrkrstphr/php-gedcom
13
 */
14
15
namespace PhpGedcom\Parser;
16
17
/**
18
 *
19
 *
20
 */
21
class Indi extends \PhpGedcom\Parser\Component {
22
	/**
23
	 *
24
	 *
25
	 */
26
	public static function parse(\PhpGedcom\Parser $parser) {
27
		$record = $parser->getCurrentLineRecord();
28
		$depth = (int) $record[0];
29
		if (isset($record[1])) {
30
			$identifier = $parser->normalizeIdentifier($record[1]);
31
		} else {
32
			$parser->skipToNextLevel($depth);
33
			return null;
34
		}
35
36
		$indi = new \PhpGedcom\Record\Indi();
37
		$indi->setId($identifier);
38
39
		$parser->getGedcom()->addIndi($indi);
40
41
		$parser->forward();
42
43
		while (!$parser->eof()) {
44
			$record = $parser->getCurrentLineRecord();
45
			$recordType = strtoupper(trim($record[1]));
46
			$currentDepth = (int) $record[0];
47
48
			if ($currentDepth <= $depth) {
49
				$parser->back();
50
				break;
51
			}
52
53
			switch ($recordType) {
54
			case '_UID':
55
				$indi->setUid(trim($record[2]));
56
				break;
57
			case 'RESN':
58
				$indi->setResn(trim($record[2]));
59
				break;
60
			case 'NAME':
61
				$name = \PhpGedcom\Parser\Indi\Name::parse($parser);
62
				$indi->addName($name);
63
				break;
64
			case 'SEX':
65
				$indi->setSex(isset($record[2]) ? trim($record[2]) : '');
66
				break;				
67
			case 'ADOP':
68
			case 'BIRT':
69
			case 'BAPM':
70
			case 'BARM':
71
			case 'BASM':
72
			case 'BLES':
73
			case 'BURI':
74
			case 'CENS':
75
			case 'CHR':
76
			case 'CHRA':
77
			case 'CONF':
78
			case 'CREM':
79
			case 'DEAT':
80
			case 'EMIG':
81
			case 'FCOM':
82
			case 'GRAD':
83
			case 'IMMI':
84
			case 'NATU':
85
			case 'ORDN':
86
			case 'RETI':
87
			case 'PROB':
88
			case 'WILL':
89
			case 'EVEN':
90
				$className = ucfirst(strtolower($recordType));
91
				$class = '\\PhpGedcom\\Parser\\Indi\\' . $className;
92
93
				$event = $class::parse($parser);
94
				$indi->addEven($event);
95
				break;
96
			case 'CAST':
97
			case 'DSCR':
98
			case 'EDUC':
99
			case 'IDNO':
100
			case 'NATI':
101
			case 'NCHI':
102
			case 'NMR':
103
			case 'OCCU':
104
			case 'PROP':
105
			case 'RELI':
106
			case 'RESI':
107
			case 'SSN':
108
			case 'TITL':
109
				$className = ucfirst(strtolower($recordType));
110
				$class = '\\PhpGedcom\\Parser\\Indi\\' . $className;
111
112
				$att = $class::parse($parser);
113
				$indi->addAttr($att);
114
				break;
115
			case 'BAPL':
116
			case 'CONL':
117
			case 'ENDL':
118
			case 'SLGC':
119
				$className = ucfirst(strtolower($recordType));
120
				$class = '\\PhpGedcom\\Parser\\Indi\\' . $className;
121
122
				$lds = $class::parse($parser);
123
				$indi->{'add' . $recordType}[] = $lds;
124
				break;
125
			case 'FAMC':
126
				$famc = \PhpGedcom\Parser\Indi\Famc::parse($parser);
127
				if ($famc) {
128
					$indi->addFamc($famc);
129
				}
130
				break;
131
			case 'FAMS':
132
				$fams = \PhpGedcom\Parser\Indi\Fams::parse($parser);
133
				if ($fams) {
134
					$indi->addFams($fams);
135
				}
136
				break;
137
			case 'SUBM':
138
				$indi->addSubm($parser->normalizeIdentifier($record[2]));
139
				break;
140
			case 'ASSO':
141
				$asso = \PhpGedcom\Parser\Indi\Asso::parse($parser);
142
				$indi->addAsso($asso);
143
				break;
144
			case 'ALIA':
145
				$indi->addAlia($parser->normalizeIdentifier($record[2]));
146
				break;
147
			case 'ANCI':
148
				$indi->addAnci($parser->normalizeIdentifier($record[2]));
149
				break;
150
			case 'DESI':
151
				$indi->addDesi($parser->normalizeIdentifier($record[2]));
152
				break;
153
			case 'RFN':
154
				$indi->setRfn(trim($record[2]));
155
				break;
156
			case 'AFN':
157
				$indi->setAfn(trim($record[2]));
158
				break;
159
			case 'REFN':
160
				$ref = \PhpGedcom\Parser\Refn::parse($parser);
161
				$indi->addRefn($ref);
162
				break;
163
			case 'RIN':
164
				$indi->setRin(trim($record[2]));
165
				break;
166
			case 'CHAN':
167
				$chan = \PhpGedcom\Parser\Chan::parse($parser);
168
				$indi->setChan($chan);
0 ignored issues
show
Bug introduced by
$chan of type PhpGedcom\Record\Chan is incompatible with the type string expected by parameter $chan of PhpGedcom\Record\Indi::setChan(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

168
				$indi->setChan(/** @scrutinizer ignore-type */ $chan);
Loading history...
169
				break;
170
			case 'NOTE':
171
				$note = \PhpGedcom\Parser\NoteRef::parse($parser);
172
				if ($note) {
173
					$indi->addNote($note);
174
				}
175
				break;
176
			case 'SOUR':
177
				$sour = \PhpGedcom\Parser\SourRef::parse($parser);
178
				$indi->addSour($sour);
179
				break;
180
			case 'OBJE':
181
				$obje = \PhpGedcom\Parser\ObjeRef::parse($parser);
182
				$indi->addObje($obje);
183
				break;
184
			default:
185
				$parser->logUnhandledRecord(get_class() . ' @ ' . __LINE__);
186
			}
187
188
			$parser->forward();
189
		}
190
191
		return $indi;
192
	}
193
}
194