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

Even::parse()   D

Complexity

Conditions 23
Paths 17

Size

Total Lines 101
Code Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
eloc 78
nc 17
nop 1
dl 0
loc 101
rs 4.1666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Indi;
16
17
use PhpGedcom\Parser\Chan;
18
19
/**
20
 *
21
 *
22
 */
23
class Even extends \PhpGedcom\Parser\Component {
24
25
	/**
26
	 *
27
	 *
28
	 */
29
	public static function parse(\PhpGedcom\Parser $parser) {
30
		$record = $parser->getCurrentLineRecord();
31
		$depth = (int) $record[0];
32
		if (empty($record[1])) {
33
			$parser->skipToNextLevel($depth);
34
			return null;
35
		}
36
37
		$even = null;
38
39
		if (strtoupper(trim($record[1])) != 'EVEN') {
40
			$className = '\\PhpGedcom\\Record\\Indi\\' . ucfirst(strtolower(trim($record[1])));
41
			$even = new $className();
42
		} else {
43
			$even = new \PhpGedcom\Record\Indi\Even();
44
		}
45
46
		if (isset($record[1]) && strtoupper(trim($record[1])) != 'EVEN') {
47
			$even->setType(trim($record[1]));
48
		}
49
50
        // ensures we capture any data following the EVEN type
51
        if (isset($record[2]) && !empty($record[2])) {
52
            $even->setAttr(trim($record[2]));
0 ignored issues
show
Bug introduced by
The method setAttr() does not exist on PhpGedcom\Record\Indi\Even. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

52
            $even->/** @scrutinizer ignore-call */ 
53
                   setAttr(trim($record[2]));
Loading history...
53
        }
54
55
        $parser->forward();
56
57
		while (!$parser->eof()) {
58
			$record = $parser->getCurrentLineRecord();
59
			$recordType = strtoupper(trim($record[1]));
60
			$currentDepth = (int) $record[0];
61
62
			if ($currentDepth <= $depth) {
63
				$parser->back();
64
				break;
65
			}
66
67
			switch ($recordType) {
68
			case 'TYPE':
69
				$even->setType(trim($record[2]));
70
				break;
71
			case 'DATE':
72
				$dat = \PhpGedcom\Parser\Date::parse($parser);
73
				$even->setDate($dat);
0 ignored issues
show
Bug introduced by
It seems like $dat can also be of type PhpGedcom\Record\Date; however, parameter $date of PhpGedcom\Record\Indi\Even::setDate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

73
				$even->setDate(/** @scrutinizer ignore-type */ $dat);
Loading history...
74
				//$even->setDate(trim($record[2]))
75
				break;
76
			case 'PLAC':
77
				$plac = \PhpGedcom\Parser\Plac::parse($parser);
78
				$even->setPlac($plac);
0 ignored issues
show
Bug introduced by
It seems like $plac can also be of type PhpGedcom\Record\Plac; however, parameter $plac of PhpGedcom\Record\Indi\Even::setPlac() does only seem to accept PhpGedcom\Record\Indi\Even\Plac, maybe add an additional type check? ( Ignorable by Annotation )

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

78
				$even->setPlac(/** @scrutinizer ignore-type */ $plac);
Loading history...
79
				break;
80
			case 'ADDR':
81
				$even->setAddr(\PhpGedcom\Parser\Addr::parse($parser));
82
				break;
83
			case 'PHON':
84
				$phone = \PhpGedcom\Parser\Phon::parse($parser);
85
				$even->addPhone($phone);
0 ignored issues
show
Bug introduced by
The method addPhone() does not exist on PhpGedcom\Record\Indi\Even. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

85
				$even->/** @scrutinizer ignore-call */ 
86
           addPhone($phone);
Loading history...
86
				break;
87
			case 'CAUS':
88
				$even->setCaus(trim($record[2]));
89
				break;
90
			case 'AGE':
91
				$even->setAge(trim($record[2]));
92
				break;
93
			case 'AGNC':
94
				$even->setAgnc(trim($record[2]));
95
				break;
96
			case 'SOUR':
97
				$sour = \PhpGedcom\Parser\SourRef::parse($parser);
98
				$even->addSour($sour);
99
				break;
100
			case 'OBJE':
101
				$obje = \PhpGedcom\Parser\ObjeRef::parse($parser);
102
				$even->addObje($obje);
103
				break;
104
			case 'NOTE':
105
				$note = \PhpGedcom\Parser\NoteRef::parse($parser);
106
				if ($note) {
107
					$even->addNote($note);
108
				}
109
				break;
110
			case 'CHAN':
111
				$change = Chan::parse($parser);
112
				$even->setChan($change);
113
				break;
114
			default:
115
				$self = get_called_class();
116
				$method = 'parse' . $recordType;
117
118
				if (method_exists($self, $method)) {
119
					$self::$method($parser, $even);
120
				} else {
121
					$parser->logUnhandledRecord($self . ' @ ' . __LINE__);
122
					$parser->skipToNextLevel($currentDepth);
123
				}
124
			}
125
126
			$parser->forward();
127
		}
128
129
		return $even;
130
	}
131
}
132