ChildElementCollectionImpl::setImmutable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Xml\Impl\Type\Child;
4
5
use Xml\Exception\UnsupportedModelOperationException;
6
use Xml\ModelInterface;
7
use Xml\Impl\Instance\ModelElementInstanceImpl;
8
use Xml\Impl\Type\ModelElementTypeImpl;
9
use Xml\Impl\Util\ModelUtil;
10
use Xml\Instance\ModelElementInstanceInterface;
11
use Xml\Type\Child\{
12
    ChildElementCollectionInterface
13
};
14
use Xml\Type\ModelElementTypeInterface;
15
16
class ChildElementCollectionImpl implements ChildElementCollectionInterface
17
{
18
    protected $childElementTypeClass;
19
    private $parentElementType;
20
    private $minOccurs = 0;
21
    protected $maxOccurs = -1;
22
    private $isMutable = true;
23
24
    public function __construct(string $childElementTypeClass, ModelElementTypeImpl $parentElementType)
25
    {
26
        $this->childElementTypeClass = $childElementTypeClass;
27
        $this->parentElementType = $parentElementType;
28
    }
29
30
    public function setImmutable(): void
31
    {
32
        $this->setMutable(false);
33
    }
34
35
    public function setMutable(bool $isMutable): void
36
    {
37
        $this->isMutable = $isMutable;
38
    }
39
40
    public function isImmutable(): bool
41
    {
42
        return !$this->isMutable;
43
    }
44
45
    private function getView(ModelElementInstanceImpl $modelElement): array
46
    {
47
        return $modelElement->getDomElement()->getChildElementsByType(
48
            $modelElement->getModelInstance(),
49
            $this->childElementTypeClass
50
        );
51
    }
52
53
    public function getMinOccurs(): int
54
    {
55
        return $this->minOccurs;
56
    }
57
58
    public function setMinOccurs(int $minOccurs): void
59
    {
60
        $this->minOccurs = $minOccurs;
61
    }
62
63
    public function getMaxOccurs(): int
64
    {
65
        return $this->maxOccurs;
66
    }
67
68
    public function getChildElementType(ModelInterface $model): ModelElementTypeInterface
69
    {
70
        return $model->getType($this->childElementTypeClass);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $model->getType($...>childElementTypeClass) could return the type null which is incompatible with the type-hinted return Xml\Type\ModelElementTypeInterface. Consider adding an additional type-check to rule them out.
Loading history...
71
    }
72
73
    public function getChildElementTypeClass(): string
74
    {
75
        return $this->childElementTypeClass;
76
    }
77
78
    public function getParentElementType(): ModelElementTypeInterface
79
    {
80
        return $this->parentElementType;
81
    }
82
83
    public function setMaxOccurs(int $maxOccurs): void
84
    {
85
        $this->maxOccurs = $maxOccurs;
86
    }
87
88
    public function performAddOperation(ModelElementInstanceImpl $modelElement, ModelElementInstanceInterface $e): void
89
    {
90
        $modelElement->addChildElement($e);
91
    }
92
93
    private function performRemoveOperation(
94
        ModelElementInstanceImpl $modelElement,
95
        ModelElementInstanceInterface $e
96
    ): bool {
97
        return $modelElement->removeChildElement($e);
98
    }
99
100
    private function performClearOperation(ModelElementInstanceImpl $modelElement, array $elementsToRemove): void
101
    {
102
        $modelElements = ModelUtil::getModelElementCollection($elementsToRemove, $modelElement->getModelInstance());
103
        foreach ($modelElements as $element) {
104
            $modelElement->removeChildElement($element);
105
        }
106
    }
107
108
    public function contains(ModelElementInstanceImpl $modelElement, ?ModelElementInstanceInterface $e): bool
109
    {
110
        if ($e === null) {
111
            return false;
112
        } else {
113
            foreach ($this->getView($modelElement) as $el) {
114
                if ($e->getDomElement()->equals($el)) {
115
                    return true;
116
                }
117
            }
118
            return false;
119
        }
120
    }
121
122
    public function containsAll(ModelElementInstanceImpl $modelElement, array $c): bool
123
    {
124
        foreach ($c as $elementToCheck) {
125
            if (!$this->contains($modelElement, $elementToCheck)) {
126
                return false;
127
            }
128
        }
129
        return true;
130
    }
131
132
    public function isEmpty(ModelElementInstanceImpl $modelElement): bool
133
    {
134
        return count($this->getView($modelElement)) == 0;
135
    }
136
137
    public function size(ModelElementInstanceImpl $modelElement): int
138
    {
139
        return count($this->getView($modelElement));
140
    }
141
142
    public function add(ModelElementInstanceImpl $modelElement, ModelElementInstanceInterface $e): bool
143
    {
144
        if (!$this->isMutable) {
145
            throw new UnsupportedModelOperationException("collection is immutable");
146
        }
147
        $this->performAddOperation($modelElement, $e);
148
        return true;
149
    }
150
151
    public function addAll(ModelElementInstanceImpl $modelElement, array $c): bool
152
    {
153
        if (!$this->isMutable) {
154
            throw new UnsupportedModelOperationException("collection is immutable");
155
        }
156
        $result = false;
157
        foreach ($c as $elementToAdd) {
158
            $result |= $this->add($modelElement, $elementToAdd);
159
        }
160
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result could return the type integer which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
161
    }
162
163
    public function clear(ModelElementInstanceImpl $modelElement): void
164
    {
165
        if (!$this->isMutable) {
166
            throw new UnsupportedModelOperationException("collection is immutable");
167
        }
168
        $view = $this->getView($modelElement);
169
        $this->performClearOperation($modelElement, $view);
170
    }
171
172
    public function remove(ModelElementInstanceImpl $modelElement, ModelElementInstanceInterface $e): bool
173
    {
174
        if (!$this->isMutable) {
175
            throw new UnsupportedModelOperationException("collection is immutable");
176
        }
177
        return $this->performRemoveOperation($modelElement, $e);
178
    }
179
180
    public function removeAll(ModelElementInstanceImpl $modelElement, array $c): bool
181
    {
182
        if (!$this->isMutable) {
183
            throw new UnsupportedModelOperationException("collection is immutable");
184
        }
185
        $result = false;
186
        foreach ($c as $elementToRemove) {
187
            $result |= $this->remove($modelElement, $elementToRemove);
188
        }
189
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result could return the type integer which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
190
    }
191
192
    public function retainAll(ModelElementInstanceImpl $modelElement, array $c): bool
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed. ( Ignorable by Annotation )

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

192
    public function retainAll(ModelElementInstanceImpl $modelElement, /** @scrutinizer ignore-unused */ array $c): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $modelElement is not used and could be removed. ( Ignorable by Annotation )

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

192
    public function retainAll(/** @scrutinizer ignore-unused */ ModelElementInstanceImpl $modelElement, array $c): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
193
    {
194
        throw new UnsupportedModelOperationException("retainAll not implemented");
195
    }
196
197
    public function get(ModelElementInstanceInterface $modelElement): array
198
    {
199
        return ModelUtil::getModelElementCollection($this->getView($modelElement), $modelElement->getModelInstance());
200
    }
201
}
202