SectionsInfos::fetch()   C
last analyzed

Complexity

Conditions 12
Paths 4

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 6.4678
c 0
b 0
f 0
cc 12
nc 4
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
 * Copyright: Deux Huit Huit 2017
4
 * LICENCE: MIT https://deuxhuithuit.mit-license.org
5
 */
6
class SectionsInfos
7
{
8
    private static $deepness = 0;
9
    private static $seenFields = array();
10
    public static function fetch($sections)
11
    {
12
        self::$deepness++;
13
        $options = array();
14
        $sections = SectionManager::fetch($sections);
15
        if (!empty($sections)) {
16
            foreach ($sections as $section) {
17
                $section_fields = $section->fetchFields();
18
                if(!is_array($section_fields)) {
19
                    continue;
20
                }
21
22
                $fields = array();
23
                foreach($section_fields as $f) {
24
                    $fd = General::intval($f->get('deepness'));
25
                    if ($fd > 0 && self::$deepness > $fd) {
26
                        continue;
27
                    }
28
                    if (in_array($f->get('id'), self::$seenFields)) {
29
                        continue;
30
                    }
31
                    self::$seenFields[] = $f->get('id');
32
                    $modes = $f->fetchIncludableElements();
33
                    
34
                    if (is_array($modes)) {
35
                        // include default
36
                        $fields[] = array(
37
                            'id' => $f->get('id'),
38
                            'name' => $f->get('label'),
39
                            'handle' => $f->get('element_name'),
40
                            'type' => $f->get('type'),
41
                            'default' => true,
42
                        );
43
                        if (count($modes) > 1) {
44
                            foreach ($modes as $mode) {
45
                                $fields[] = array(
46
                                    'id' => $f->get('id'),
47
                                    'name' => $f->get('label'),
48
                                    'handle' => $mode,
49
                                    'type' => $f->get('type')
50
                                );
51
                            }
52
                        }
53
                    }
54
                }
55
56
                $options[] = array(
57
                    'name' => $section->get('name'),
58
                    'handle' => $section->get('handle'),
59
                    'fields' => $fields
60
                );
61
            }
62
        }
63
        self::$deepness--;
64
        if (self::$deepness === 0) {
65
            self::$seenFields = array();
66
        }
67
        return $options;
68
    }
69
}
70