Passed
Push — static-analysis ( 85afd6...0e0544 )
by SignpostMarv
03:23
created

Group   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 123
Duplicated Lines 15.45 %

Test Coverage

Coverage 80%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 19
loc 123
ccs 36
cts 45
cp 0.8
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSchema() 0 3 1
A setDoc() 0 4 1
A getDoc() 0 3 1
B loadGroupBeforeCheckingChildNodes() 0 26 4
A __construct() 0 4 1
B loadGroup() 19 37 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        return function () use ($reader, $group, $node, $methods) {
112 135
            SchemaReaderLoadAbstraction::againstDOMNodeList(
113 135
                $node,
114 135 View Code Duplication
                function (
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...
115
                    DOMElement $node,
116
                    DOMElement $childNode
117
                ) use (
118 135
                    $methods,
119 135
                    $reader,
120 135
                    $group
121
                ) {
122
                    /**
123
                    * @var string[] $methods
124
                    */
125 135
                    $methods = $methods;
126
127 135
                    $reader->maybeCallMethod(
128 135
                        $methods,
129 135
                        $childNode->localName,
130 135
                        $childNode,
131 135
                        $group,
132 90
                        $childNode
133 45
                    );
134 135
                }
135 45
            );
136 135
        };
137
    }
138
}
139