Passed
Push — static-analysis ( ae4331...8e814c )
by SignpostMarv
03:31
created

Group::getSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace GoetasWebservices\XML\XSDReader\Schema\Element;
3
4
use DOMElement;
5
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeItemTrait;
6
use GoetasWebservices\XML\XSDReader\Schema\Schema;
7
use GoetasWebservices\XML\XSDReader\SchemaReader;
8
use GoetasWebservices\XML\XSDReader\SchemaReaderLoadAbstraction;
9
10
class Group implements ElementItem, ElementContainer
11
{
12
    use AttributeItemTrait;
13
    use ElementContainerTrait;
14
15
    /**
16
     *
17
     * @var Schema
18
     */
19
    protected $schema;
20
21
    /**
22
    * @var string|null
23
    */
24
    protected $doc;
25
26
    /**
27
    * @param string $name
28
    */
29 135
    public function __construct(Schema $schema, $name)
30
    {
31 135
        $this->schema = $schema;
32 135
        $this->name = $name;
33 135
    }
34
35
    /**
36
    * @return string|null
37
    */
38
    public function getDoc()
39
    {
40
        return $this->doc;
41
    }
42
43
    /**
44
    * @param string $doc
45
    *
46
    * @return $this
47
    */
48 135
    public function setDoc($doc)
49
    {
50 135
        $this->doc = $doc;
51 135
        return $this;
52
    }
53
54
    /**
55
    * @return Schema
56
    */
57 135
    public function getSchema()
58
    {
59 135
        return $this->schema;
60
    }
61
62
    /**
63
    * @return Group|GroupRef
64
    */
65 135
    protected static function loadGroupBeforeCheckingChildNodes(
66
        Schema $schema,
67
        DOMElement $node
68
    ) {
69 135
        $group = new Group($schema, $node->getAttribute("name"));
70 135
        $group->setDoc(SchemaReader::getDocumentation($node));
71
72 135
        if ($node->hasAttribute("maxOccurs")) {
73
            /**
74
            * @var GroupRef $group
75
            */
76
            $group = SchemaReader::maybeSetMax(new GroupRef($group), $node);
77
        }
78 135
        if ($node->hasAttribute("minOccurs")) {
79
            /**
80
            * @var GroupRef $group
81
            */
82
            $group = SchemaReader::maybeSetMin(
83
                $group instanceof GroupRef ? $group : new GroupRef($group),
84
                $node
85
            );
86
        }
87
88 135
        $schema->addGroup($group);
89
90 135
        return $group;
91
    }
92
93
    /**
94
    * @return \Closure
95
    */
96 135
    public static function loadGroup(
97
        SchemaReaderLoadAbstraction $reader,
98
        Schema $schema,
99
        DOMElement $node
100
    ) {
101 135
        $group = static::loadGroupBeforeCheckingChildNodes(
102 135
            $schema,
103 90
            $node
104 45
        );
105 90
        static $methods = [
106
            'sequence' => 'loadSequence',
107
            'choice' => 'loadSequence',
108
            'all' => 'loadSequence',
109 45
        ];
110
111 135
        return function () use ($reader, $group, $node, $methods) {
112 135
            $limit = $node->childNodes->length;
113 135 View Code Duplication
            for ($i = 0; $i < $limit; $i += 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114 135
                $childNode = $node->childNodes->item($i);
115
116
                /**
117
                * @var string[] $methods
118
                */
119 135
                $methods = $methods;
120
121 135
                $reader->maybeCallMethod(
122 135
                    $methods,
123 135
                    (string) $childNode->localName,
124 135
                    $childNode,
125 135
                    $group,
126 90
                    $childNode
127 45
                );
128 45
            }
129 135
        };
130
    }
131
}
132