Even::parse()   D
last analyzed

Complexity

Conditions 23
Paths 17

Size

Total Lines 103
Code Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
eloc 78
nc 17
nop 1
dl 0
loc 103
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
 * @license         MIT
11
 *
12
 * @link            http://github.com/mrkrstphr/php-gedcom
13
 */
14
15
namespace PhpGedcom\Parser\Indi;
16
17
use PhpGedcom\Parser\Chan;
18
19
class Even extends \PhpGedcom\Parser\Component
20
{
21
    public static function parse(\PhpGedcom\Parser $parser)
22
    {
23
        $record = $parser->getCurrentLineRecord();
24
        $depth = (int) $record[0];
25
        if (empty($record[1])) {
26
            $parser->skipToNextLevel($depth);
27
28
            return null;
29
        }
30
31
        $even = null;
32
33
        if (strtoupper(trim($record[1])) != 'EVEN') {
34
            $className = '\\PhpGedcom\\Record\\Indi\\'.ucfirst(strtolower(trim($record[1])));
35
            $even = new $className();
36
        } else {
37
            $even = new \PhpGedcom\Record\Indi\Even();
38
        }
39
40
        if (isset($record[1]) && strtoupper(trim($record[1])) != 'EVEN') {
41
            $even->setType(trim($record[1]));
42
        }
43
44
        // ensures we capture any data following the EVEN type
45
        if (isset($record[2]) && !empty($record[2])) {
46
            $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

46
            $even->/** @scrutinizer ignore-call */ 
47
                   setAttr(trim($record[2]));
Loading history...
47
        }
48
49
        $parser->forward();
50
51
        while (!$parser->eof()) {
52
            $record = $parser->getCurrentLineRecord();
53
            $recordType = strtoupper(trim($record[1]));
54
            $currentDepth = (int) $record[0];
55
56
            if ($currentDepth <= $depth) {
57
                $parser->back();
58
                break;
59
            }
60
61
            switch ($recordType) {
62
            case 'TYPE':
63
                $even->setType(trim($record[2]));
64
                break;
65
            case 'DATE':
66
                $dat = \PhpGedcom\Parser\Date::parse($parser);
67
                $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

67
                $even->setDate(/** @scrutinizer ignore-type */ $dat);
Loading history...
68
                //$even->setDate(trim($record[2]))
69
                break;
70
            case 'PLAC':
71
                $plac = \PhpGedcom\Parser\Plac::parse($parser);
72
                $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

72
                $even->setPlac(/** @scrutinizer ignore-type */ $plac);
Loading history...
73
                break;
74
            case 'ADDR':
75
                $even->setAddr(\PhpGedcom\Parser\Addr::parse($parser));
76
                break;
77
            case 'PHON':
78
                $phone = \PhpGedcom\Parser\Phon::parse($parser);
79
                $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

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