EntryFactory::createFromArray()   C
last analyzed

Complexity

Conditions 13
Paths 30

Size

Total Lines 52
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 13
eloc 35
c 3
b 0
f 0
nc 30
nop 1
dl 0
loc 52
rs 6.6166

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
namespace Sepia\PoParser\Catalog;
4
5
class EntryFactory
6
{
7
    /**
8
     * @param array $entryArray
9
     * @return Entry
10
     */
11
    public static function createFromArray(array $entryArray)
12
    {
13
        $entry = new Entry(
14
            $entryArray['msgid'],
15
            isset($entryArray['msgstr']) ? $entryArray['msgstr'] : null
16
        );
17
        $plurals = array();
18
19
        foreach ($entryArray as $key => $value) {
20
            switch (true) {
21
                case $key === 'msgctxt':
22
                    $entry->setMsgCtxt($entryArray['msgctxt']);
23
                    break;
24
25
                case $key === 'flags':
26
                    $entry->setFlags($entryArray['flags']);
27
                    break;
28
29
                case $key === 'reference':
30
                    $entry->setReference($entryArray['reference']);
31
                    break;
32
33
                case $key === 'previous':
34
                    $entry->setPreviousEntry(self::createFromArray($entryArray['previous']));
35
                    break;
36
37
                case $key === 'tcomment':
38
                    $entry->setTranslatorComments($value);
39
                    break;
40
41
                case $key === 'ccomment':
42
                    $entry->setDeveloperComments($value);
43
                    break;
44
45
                case $key === 'obsolete':
46
                    $entry->setObsolete(true);
47
                    break;
48
49
                case 0 === \strpos($key, 'msgstr['):
50
                    $plurals[] = $value;
51
                    break;
52
            }
53
        }
54
55
        if (\count($plurals) > 0) {
56
            $entry->setMsgStrPlurals($plurals);
57
            if(!empty($entryArray['msgid_plural'])){
58
                $entry->setMsgIdPlural($entryArray['msgid_plural']);
59
            }
60
        }
61
62
        return $entry;
63
    }
64
}
65