Collection   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 190
ccs 67
cts 67
cp 1
rs 10
c 0
b 0
f 0
wmc 29

18 Methods

Rating   Name   Duplication   Size   Complexity  
A addIfNotFalse() 0 7 2
A getFirstItem() 0 3 2
A numeric() 0 3 1
A addIfNotEmpty() 0 7 2
A ifTrue() 0 9 5
A assoc() 0 3 1
A generate() 0 7 1
A push() 0 9 2
A setInline() 0 5 1
A count() 0 3 1
A addItem() 0 9 2
A setWithKeys() 0 5 1
A addIfNotNull() 0 7 2
A __construct() 0 6 1
A setMultiline() 0 5 1
A addConverter() 0 5 1
A getConverters() 0 3 1
A map() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Murtukov\PHPCodeGenerator;
6
7
use function count;
8
use function is_bool;
9
use function is_callable;
10
11
class Collection extends DependencyAwareGenerator
12
{
13
    protected array $items = [];
14
    protected bool  $multiline = false;
15
    protected bool  $withKeys = true;
16
    protected array $converters = [];
17
18
    protected Utils $utils;
19
20 6
    public function __construct(array $items = [], bool $multiline = false, bool $withKeys = true)
21
    {
22 6
        $this->items = $items;
23 6
        $this->multiline = $multiline;
24 6
        $this->withKeys = $withKeys;
25 6
        $this->utils = new Utils();
26 6
    }
27
28 2
    public static function numeric(array $items = [], bool $multiline = false): self
29
    {
30 2
        return new static($items, $multiline, false);
31
    }
32
33
    /**
34
     * Shorthand for `new AssocArray($items, true)`.
35
     *
36
     * @return Collection
37
     */
38 6
    public static function assoc(array $items = [], bool $multiline = true): self
39
    {
40 6
        return new static($items, $multiline);
41
    }
42
43
    /**
44
     * Creates a multiline array and adds all provided items, after applying a callback to them.
45
     */
46 1
    public static function map(array $items, callable $map): self
47
    {
48 1
        $array = new static([], true);
49
50 1
        foreach ($items as $key => $value) {
51 1
            $array->addItem($key, $map($value, $key));
52
        }
53
54 1
        return $array;
55
    }
56
57
    /**
58
     * Adds item to the array.
59
     *
60
     * @param mixed $value
61
     */
62 3
    public function addItem(string $key, $value): self
63
    {
64 3
        $this->items[$key] = $value;
65
66 3
        if ($value instanceof DependencyAwareGenerator) {
67 2
            $this->dependencyAwareChildren[] = $value;
68
        }
69
70 3
        return $this;
71
    }
72
73
    /**
74
     * Adds item to the array if it's not equal null.
75
     *
76
     * @param mixed $value
77
     */
78 1
    public function addIfNotNull(string $key, $value): self
79
    {
80 1
        if (null === $value) {
81 1
            return $this;
82
        }
83
84 1
        return $this->addItem($key, $value);
85
    }
86
87
    /**
88
     * Adds item to the array if it's not empty.
89
     *
90
     * @param mixed $value
91
     */
92 1
    public function addIfNotEmpty(string $key, $value): self
93
    {
94 1
        if (empty($value)) {
95 1
            return $this;
96
        }
97
98 1
        return $this->addItem($key, $value);
99
    }
100
101
    /**
102
     * Adds item to the array if it's not equal false.
103
     *
104
     * @param mixed $value
105
     */
106 1
    public function addIfNotFalse(string $key, $value): self
107
    {
108 1
        if (false === $value) {
109 1
            return $this;
110
        }
111
112 1
        return $this->addItem($key, $value);
113
    }
114
115
    /**
116
     * Returns self if value is true or callback returns true, otherwise returns a mock object.
117
     *
118
     * @param bool|\Closure $value
119
     *
120
     * @return self|Mock
121
     */
122 1
    public function ifTrue($value)
123
    {
124 1
        if (is_bool($value)) {
125 1
            return $value ? $this : Mock::getInstance($this);
126 1
        } elseif (is_callable($value)) {
127 1
            return $value() ? $this : Mock::getInstance($this);
128
        }
129
130 1
        return Mock::getInstance($this);
131
    }
132
133 6
    public function getConverters()
134
    {
135 6
        return $this->converters;
136
    }
137
138 1
    public function addConverter(ConverterInterface $converter): self
139
    {
140 1
        $this->converters[] = $converter;
141
142 1
        return $this;
143
    }
144
145 2
    public function setMultiline(): self
146
    {
147 2
        $this->multiline = true;
148
149 2
        return $this;
150
    }
151
152 1
    public function setInline(): self
153
    {
154 1
        $this->multiline = false;
155
156 1
        return $this;
157
    }
158
159 1
    public function setWithKeys(bool $val = true): self
160
    {
161 1
        $this->withKeys = $val;
162
163 1
        return $this;
164
    }
165
166 1
    public function count()
167
    {
168 1
        return count($this->items);
169
    }
170
171
    /**
172
     * @return GeneratorInterface|string|null
173
     */
174 1
    public function getFirstItem()
175
    {
176 1
        return reset($this->items) ?: null;
177
    }
178
179 6
    public function generate(): string
180
    {
181 6
        return $this->utils->stringify(
182 6
            $this->items,
183 6
            $this->multiline,
184 6
            $this->withKeys,
185 6
            $this->getConverters()
186
        );
187
    }
188
189
    /**
190
     * @param string|GeneratorInterface $item
191
     */
192 2
    public function push($item): self
193
    {
194 2
        $this->items[] = $item;
195
196 2
        if ($item instanceof DependencyAwareGenerator) {
197 2
            $this->dependencyAwareChildren[] = $item;
198
        }
199
200 2
        return $this;
201
    }
202
}
203