TagChoice::getChildrenElements()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\WsdlHandler\Tag;
6
7
use WsdlToPhp\WsdlHandler\AbstractDocument;
8
9
/**
10
 * @see https://www.w3.org/TR/xmlschema11-1/#element-choice
11
 */
12
class TagChoice extends Tag
13
{
14 12
    public function getChildrenElements(): array
15
    {
16 12
        $children = [];
17 12
        foreach (self::getChildrenElementsTags() as $tagName) {
18 12
            $children = array_merge($children, $this->getFilteredChildrenByName($tagName));
19
        }
20
21 12
        return $children;
22
    }
23
24 14
    public static function getChildrenElementsTags(): array
25
    {
26 14
        return [
27 14
            AbstractDocument::TAG_ELEMENT,
28 14
            AbstractDocument::TAG_GROUP,
29 14
            AbstractDocument::TAG_CHOICE,
30 14
            AbstractDocument::TAG_ANY,
31 14
        ];
32
    }
33
34 14
    public static function getForbiddenParentTags(): array
35
    {
36 14
        return [
37 14
            AbstractDocument::TAG_COMPLEX_TYPE,
38 14
            AbstractDocument::TAG_SEQUENCE,
39 14
        ];
40
    }
41
42 12
    protected function getFilteredChildrenByName(string $tagName): array
43
    {
44 12
        return array_filter($this->getChildrenByName($tagName), [
45 12
            $this,
46 12
            'filterFoundChildren',
47 12
        ]);
48
    }
49
50
    /**
51
     * This must ensure the current element, based on its tagName is not contained by another element than the choice.
52
     * If it is contained by another element, then it is child/property of its parent element and does not belong to the choice elements.
53
     */
54 12
    protected function filterFoundChildren(AbstractTag $child): bool
55
    {
56 12
        $forbiddenParentTags = self::getForbiddenParentTags();
57 12
        $valid = true;
58 12
        while ($child && $child->getParent() && !$this->getNode()->isSameNode($child->getParent()->getNode())) {
59 4
            $valid = $valid && !in_array($child->getParent()->getName(), $forbiddenParentTags);
60 4
            $child = $child->getParent();
61
        }
62
63 12
        return (bool) $valid;
64
    }
65
}
66