Head::parse()   C
last analyzed

Complexity

Conditions 16
Paths 3

Size

Total Lines 78
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 63
dl 0
loc 78
c 1
b 0
f 0
rs 5.5666
cc 16
nc 3
nop 1

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 Gedcom\Parser;
16
17
class Head extends \Gedcom\Parser\Component
18
{
19
    /**
20
     * @return \Gedcom\Record\Head
21
     */
22
    public static function parse(\Gedcom\Parser $parser)
23
    {
24
        $record = $parser->getCurrentLineRecord();
25
        $depth = (int) $record[0];
26
        if (isset($record[1])) {
27
            $parser->normalizeIdentifier($record[1]);
28
        } else {
29
            $parser->skipToNextLevel($depth);
30
31
            return null;
32
        }
33
34
        $head = new \Gedcom\Record\Head();
35
36
        $parser->getGedcom()->setHead($head);
37
38
        $parser->forward();
39
40
        while (!$parser->eof()) {
41
            $record = $parser->getCurrentLineRecord();
42
            $currentDepth = (int) $record[0];
43
            $recordType = strtoupper(trim($record[1]));
44
45
            if ($currentDepth <= $depth) {
46
                $parser->back();
47
                break;
48
            }
49
50
            switch ($recordType) {
51
                case 'SOUR':
52
                    $sour = \Gedcom\Parser\Head\Sour::parse($parser);
53
                    $head->setSour($sour);
54
                    break;
55
                case 'SUBM':
56
                    $head->setSubm($parser->normalizeIdentifier($record[2]));
57
                    break;
58
                case 'SUBN':
59
                    $head->setSubn($parser->normalizeIdentifier($record[2]));
60
                    break;
61
                case 'DEST':
62
                    $head->setDest(trim($record[2]));
63
                    break;
64
                case 'FILE':
65
                    $head->setFile(trim($record[2]));
66
                    break;
67
                case 'COPR':
68
                    $head->setCopr(trim($record[2]));
69
                    break;
70
                case 'LANG':
71
                    $head->setLang(trim($record[2]));
72
                    break;
73
                case 'DATE':
74
                    $date = \Gedcom\Parser\Head\Date::parse($parser);
75
                    $head->setDate($date);
76
                    break;
77
                case 'GEDC':
78
                    $gedc = \Gedcom\Parser\Head\Gedc::parse($parser);
79
                    $head->setGedc($gedc);
80
                    break;
81
                case 'CHAR':
82
                    $char = \Gedcom\Parser\Head\Char::parse($parser);
83
                    $head->setChar($char);
84
                    break;
85
                case 'PLAC':
86
                    $plac = \Gedcom\Parser\Head\Plac::parse($parser);
87
                    $head->setPlac($plac);
88
                    break;
89
                case 'NOTE':
90
                    $head->setNote($parser->parseMultiLineRecord());
91
                    break;
92
                default:
93
                    $parser->logUnhandledRecord(self::class.' @ '.__LINE__);
94
            }
95
96
            $parser->forward();
97
        }
98
99
        return $head;
100
    }
101
}
102