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

Writer::convert()   F

Complexity

Conditions 31
Paths 512

Size

Total Lines 87
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 31
eloc 44
nc 512
nop 2
dl 0
loc 87
rs 0.6777
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;
16
17
use \PhpGedcom\Gedcom;
18
use \PhpGedcom\Writer\Head;
19
use \PhpGedcom\Writer\Subn;
20
use \PhpGedcom\Writer\Subm;
21
use \PhpGedcom\Writer\Sour;
22
use \PhpGedcom\Writer\Indi;
23
use \PhpGedcom\Writer\Fam;
24
use \PhpGedcom\Writer\Note;
25
use \PhpGedcom\Writer\Repo;
26
use \PhpGedcom\Writer\Obje;
27
28
/**
29
 *
30
 */
31
class Writer
32
{
33
    const GEDCOM55 = 'gedcom5.5';
34
    
35
    protected $_output = null;
36
    
37
    /**
38
     *
39
     * @param \PhpGedcom\Gedcom $gedcom The GEDCOM object
40
     * @param string $format The format to convert the GEDCOM object to
41
     * @return string The contents of the document in the converted format
42
     */
43
    public static function convert(Gedcom $gedcom, $format = self::GEDCOM55)
44
    {
45
        $head = $gedcom->getHead();
46
        $subn = $gedcom->getSubn();
47
        $subms = $gedcom->getSubm();    // array()
48
        $sours = $gedcom->getSour();    // array()
49
        $indis = $gedcom->getIndi();    // array()
50
        $fams = $gedcom->getFam();      // array()
51
        $notes = $gedcom->getNote();    // array()
52
        $repos = $gedcom->getRepo();    // array()
53
        $objes = $gedcom->getObje();    // array()
54
        
55
        $output = "0 FORMAT ".$format."\n";
56
57
        // head
58
        if($head){
0 ignored issues
show
introduced by
$head is of type PhpGedcom\Record\Head, thus it always evaluated to true.
Loading history...
59
            $output = Head::convert($head, $format);
60
        }
61
        
62
        // subn
63
        if($subn){
0 ignored issues
show
introduced by
$subn is of type PhpGedcom\Record\Subn, thus it always evaluated to true.
Loading history...
64
            $output .= Subn::convert($subn);
65
        }
66
67
        // subms
68
        if(!empty($subms) && count($subms) > 0){
69
            foreach($subms as $item){
70
                if($item){
71
                    $output .= Subm::convert($item);
72
                }
73
            }
74
        }
75
        
76
        // sours
77
        if(!empty($sours) && count($sours) > 0){
78
            foreach($sours as $item){
79
                if($item){
80
                    $output .= Sour::convert($item);
0 ignored issues
show
Bug introduced by
The call to PhpGedcom\Writer\Sour::convert() has too few arguments starting with level. ( Ignorable by Annotation )

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

80
                    $output .= Sour::/** @scrutinizer ignore-call */ convert($item);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
81
                }
82
            }
83
        }
84
85
        // indis
86
        if(!empty($indis) && count($indis) > 0){
87
            foreach($indis as $item){
88
                if($item){
89
                    $output .= Indi::convert($item);
90
                }
91
            }
92
        }
93
94
        // fams
95
        if(!empty($fams) && count($fams) > 0){
96
            foreach($fams as $item){
97
                if($item){
98
                    $output .= Fam::convert($item);
99
                }
100
            }
101
        }
102
        // notes
103
        if(!empty($notes) && count($notes) > 0){
104
            foreach($notes as $item){
105
                if($item){
106
                    $output .= Note::convert($item);
107
                }
108
            }
109
        }
110
111
        // repos
112
        if(!empty($repos) && count($repos) > 0){
113
            foreach($repos as $item){
114
                if($item){
115
                    $output .= Repo::convert($item);
116
                }
117
            }
118
        }
119
        // Objes
120
        if(!empty($objes) && count($objes) > 0){
121
            foreach($objes as $item){
122
                if($item){
123
                    $output .= Obje::convert($item);
124
                }
125
            }
126
        }
127
        // EOF
128
        $output .= "0 TRLR\n";
129
        return $output;
130
    }
131
}
132