Test Setup Failed
Pull Request — master (#3)
by Timur
05:47 queued 03:49
created

Collection::setWithKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Murtukov\PHPCodeGenerator;
6
7
use Closure;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Murtukov\PHPCodeGenerator\Closure. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

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