Completed
Push — master ( 3be2b4...9bcc1d )
by Paweł
02:33
created

ArrayType::getValue()   C

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
     * @var null|int
28
     */
29
    protected $maxElements;
30
31
    /**
32
     * @var \Wszetko\Sitemap\Items\DataTypes\AbstractDataType
33
     */
34
    private $baseDataType;
35
36
    /**
37
     * ArrayType constructor.
38
     *
39
     * @param mixed $name
40
     * @param mixed $dataType
41
     *
42
     * @throws \InvalidArgumentException
43
     */
44 380
    public function __construct($name, $dataType)
45
    {
46 380
        parent::__construct($name);
47
48 380
        $baseDataType = new $dataType($this->getName());
49
50 380
        if (($baseDataType instanceof AbstractDataType) === false) {
51
            throw new InvalidArgumentException('Provided DataType is invalid.');
52
        }
53
54 380
        $this->baseDataType = $baseDataType;
55 380
        $this->value = [];
56 380
    }
57
58
    /**
59
     * @return \Wszetko\Sitemap\Items\DataTypes\AbstractDataType
60
     */
61 380
    public function getBaseDataType(): AbstractDataType
62
    {
63 380
        return $this->baseDataType;
64
    }
65
66
    /**
67
     * @return null|int
68
     */
69 42
    public function getMaxElements(): ?int
70
    {
71 42
        return $this->maxElements;
72
    }
73
74
    /**
75
     * @param int $maxElements
76
     *
77
     * @return self
78
     */
79 352
    public function setMaxElements(int $maxElements): self
80
    {
81 352
        $this->maxElements = $maxElements;
82
83 352
        return $this;
84
    }
85
86
    /**
87
     * @param mixed $value
88
     * @param array $parameters
89
     *
90
     * @return static
91
     */
92 10
    public function setValue($value, $parameters = []): DataType
93
    {
94 10
        if (is_array($value)) {
95 6
            foreach ($value as $val) {
96 6
                $this->addValue($val, $parameters);
97
            }
98
        } else {
99 4
            $this->addValue($value, $parameters);
100
        }
101
102 10
        return $this;
103
    }
104
105
    /**
106
     * @param mixed $value
107
     * @param array $parameters
108
     *
109
     * @return \Wszetko\Sitemap\Interfaces\DataType
110
     */
111 56
    public function addValue($value, $parameters = []): DataType
112
    {
113 56
        if (is_array($value)) {
114 8
            foreach ($value as $val) {
115 8
                $this->addValue($val, $parameters);
116
            }
117
        } else {
118 56
            $var = clone $this->getBaseDataType();
119 56
            $var->setValue($value, $parameters);
120
121 54
            if (null !== $var->getValue()) {
122 54
                $this->value[] = $var;
123
            }
124
        }
125
126 54
        return $this;
127
    }
128
129
    /**
130
     * @return mixed
131
     */
132 42
    public function getValue()
133
    {
134 42
        $values = parent::getValue();
135
136 42
        if (null === $values || [] === $values) {
137 2
            return null;
138
        }
139
140 42
        $result = [];
141
142 42
        if (is_array($values)) {
143
            /** @var DataType $element */
144 42
            foreach ($values as $element) {
145 42
                $this->propagateDomain($element);
146 42
                $value = $element->getValue();
147
148
                if (
149 42
                    is_array($value) &&
150 42
                    isset(array_values($value)[0]) &&
151 42
                    '' !== array_values($value)[0]
152
                ) {
153 4
                    $key = array_key_first($value);
154
155 4
                    if (null !== $key) {
156 4
                        $result[$key] = array_values($value)[0];
157
                    }
158 38
                } elseif (null !== $value && '' !== $value) {
159 40
                    $result[] = $value;
160
                }
161
            }
162
163 42
            if (null !== $this->getMaxElements()) {
164 26
                $result = array_slice($result, 0, $this->getMaxElements());
165
            }
166
        }
167
168 42
        return $result;
169
    }
170
}
171