ArrayType::getValue()   C
last analyzed

Complexity

Conditions 12
Paths 4

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 12

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 12
eloc 20
c 1
b 1
f 0
nc 4
nop 0
dl 0
loc 37
ccs 20
cts 20
cp 1
crap 12
rs 6.9666

How to fix   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
declare(strict_types=1);
4
5
/**
6
 * This file is part of Wszetko Sitemap.
7
 *
8
 * (c) Paweł Kłopotek-Główczewski <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Wszetko\Sitemap\Items\DataTypes;
15
16
use InvalidArgumentException;
17
use Wszetko\Sitemap\Interfaces\DataType;
18
19
/**
20
 * Class ArrayType.
21
 *
22
 * @package Wszetko\Sitemap\Items\DataTypes
23
 */
24
class ArrayType extends AbstractDataType
25
{
26
    /**
27
     * Number of maximum elements to keep.
28
     *
29
     * @var null|int
30
     */
31
    protected $maxElements;
32
33
    /**
34
     * Base object that are used to create elements.
35
     *
36
     * @var \Wszetko\Sitemap\Items\DataTypes\AbstractDataType
37
     */
38
    private $baseDataType;
39
40
    /**
41
     * @inheritDoc
42
     *
43
     * @var string $dataType
44
     *
45
     * @throws \InvalidArgumentException
46
     */
47 390
    public function __construct(string $name, string $dataType)
48
    {
49 390
        parent::__construct($name);
50
51 390
        $baseDataType = new $dataType($this->getName());
52
53 390
        if (($baseDataType instanceof AbstractDataType) === false) {
54
            // @codeCoverageIgnoreStart
55
            throw new InvalidArgumentException('Provided DataType is invalid.');
56
            // @codeCoverageIgnoreEnd
57
        }
58
59 390
        $this->baseDataType = $baseDataType;
60 390
        $this->value = [];
61 390
    }
62
63
    /**
64
     * Return object to create new element.
65
     *
66
     * @return \Wszetko\Sitemap\Items\DataTypes\AbstractDataType
67
     */
68 390
    public function getBaseDataType(): AbstractDataType
69
    {
70 390
        return $this->baseDataType;
71
    }
72
73
    /**
74
     * Return maximum number of elements to handle.
75
     *
76
     * @return null|int
77
     */
78 72
    public function getMaxElements(): ?int
79
    {
80 72
        return $this->maxElements;
81
    }
82
83
    /**
84
     * Set maximum number of elements to handle.
85
     *
86
     * @param int $maxElements
87
     *
88
     * @return self
89
     */
90 362
    public function setMaxElements(int $maxElements): self
91
    {
92 362
        $this->maxElements = $maxElements;
93
94 362
        return $this;
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100 46
    public function setValue($value, $parameters = []): DataType
101
    {
102 46
        if (is_array($value)) {
103 6
            foreach ($value as $val) {
104 6
                $this->addValue($val, $parameters);
105
            }
106
        } else {
107 44
            $this->addValue($value, $parameters);
108
        }
109
110 44
        return $this;
111
    }
112
113
    /**
114
     * Add single element to collection.
115
     *
116
     * @param mixed $value
117
     * @param array $parameters
118
     *
119
     * @return \Wszetko\Sitemap\Interfaces\DataType
120
     */
121 92
    public function addValue($value, $parameters = []): DataType
122
    {
123 92
        if (is_array($value)) {
124 8
            foreach ($value as $val) {
125 8
                $this->addValue($val, $parameters);
126
            }
127
        } else {
128 92
            $var = clone $this->getBaseDataType();
129 92
            $var->setValue($value, $parameters);
130
131 88
            if (null !== $var->getValue()) {
132 84
                $this->value[] = $var;
133
            }
134
        }
135
136 88
        return $this;
137
    }
138
139
    /**
140
     * @inheritDoc
141
     */
142 76
    public function getValue()
143
    {
144 76
        $values = parent::getValue();
145
146 76
        if (null === $values || [] === $values) {
147 8
            return null;
148
        }
149
150 72
        $result = [];
151
152 72
        if (is_array($values)) {
153
            /** @var DataType $element */
154 72
            foreach ($values as $element) {
155 72
                $this->propagateDomain($element);
156 72
                $value = $element->getValue();
157
158
                if (
159 72
                    is_array($value) &&
160 72
                    isset(array_values($value)[0]) &&
161 72
                    '' !== array_values($value)[0]
162
                ) {
163 38
                    $key = array_key_first($value);
164
165 38
                    if (null !== $key) {
166 38
                        $result[] = [$key => array_values($value)[0]];
167
                    }
168 38
                } elseif (null !== $value && '' !== $value) {
169 38
                    $result[] = $value;
170
                }
171
            }
172
173 72
            if (null !== $this->getMaxElements()) {
174 26
                $result = array_slice($result, 0, $this->getMaxElements());
175
            }
176
        }
177
178 72
        return $result;
179
    }
180
}
181