Passed
Pull Request — master (#3)
by Nicolas
01:02
created

Chain::fill()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cocur\Chain;
4
5
use Cocur\Chain\Link\ChangeKeyCase;
6
use Cocur\Chain\Link\Combine;
7
use Cocur\Chain\Link\Count;
8
use Cocur\Chain\Link\CountValues;
9
use Cocur\Chain\Link\Diff;
10
use Cocur\Chain\Link\Fill;
0 ignored issues
show
Bug introduced by
The type Cocur\Chain\Link\Fill was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Cocur\Chain\Link\Filter;
12
use Cocur\Chain\Link\First;
13
use Cocur\Chain\Link\Find;
14
use Cocur\Chain\Link\FlatMap;
15
use Cocur\Chain\Link\Flip;
16
use Cocur\Chain\Link\Intersect;
17
use Cocur\Chain\Link\IntersectAssoc;
18
use Cocur\Chain\Link\IntersectKey;
19
use Cocur\Chain\Link\Join;
20
use Cocur\Chain\Link\KeyExists;
21
use Cocur\Chain\Link\Keys;
22
use Cocur\Chain\Link\Last;
23
use Cocur\Chain\Link\Map;
24
use Cocur\Chain\Link\Merge;
25
use Cocur\Chain\Link\Pad;
26
use Cocur\Chain\Link\Pop;
27
use Cocur\Chain\Link\Product;
28
use Cocur\Chain\Link\Push;
29
use Cocur\Chain\Link\Rand;
30
use Cocur\Chain\Link\Reduce;
31
use Cocur\Chain\Link\Replace;
32
use Cocur\Chain\Link\Reverse;
33
use Cocur\Chain\Link\Search;
34
use Cocur\Chain\Link\Shift;
35
use Cocur\Chain\Link\Shuffle;
36
use Cocur\Chain\Link\Slice;
37
use Cocur\Chain\Link\Sort;
38
use Cocur\Chain\Link\SortKeys;
39
use Cocur\Chain\Link\Splice;
40
use Cocur\Chain\Link\Sum;
41
use Cocur\Chain\Link\Unique;
42
use Cocur\Chain\Link\Unshift;
43
use Cocur\Chain\Link\Values;
44
use Countable;
45
46
/**
47
 * Chain.
48
 *
49
 * @author    Florian Eckerstorfer
50
 * @copyright 2015-2017 Florian Eckerstorfer
51
 */
52
class Chain extends AbstractChain implements Countable
53
{
54
    use ChangeKeyCase;
55
    use Combine;
56
    use Count;
57
    use CountValues;
58
    use Diff;
59
    use Filter;
60
    use Find;
61
    use First;
62
    use FlatMap;
63
    use Flip;
64
    use Intersect;
65
    use IntersectAssoc;
66
    use IntersectKey;
67
    use Join;
68
    use KeyExists;
69
    use Keys;
70
    use Last;
71
    use Map;
72
    use Merge;
73
    use Pad;
74
    use Pop;
75
    use Product;
76
    use Push;
77
    use Rand;
78
    use Reduce;
79
    use Replace;
80
    use Reverse;
81
    use Search;
82
    use Shift;
83
    use Shuffle;
84
    use Slice;
85
    use Sort;
86
    use SortKeys;
87
    use Splice;
88
    use Sum;
89
    use Unique;
90
    use Unshift;
91
    use Values;
92
93
    /**
94
     * @param array $array
95
     */
96 2
    public function __construct(array $array = [])
97
    {
98 2
        $this->array = $array;
99 2
    }
100
101
    /**
102
     * @param array $array
103
     *
104
     * @return self
105
     */
106 1
    public static function create(array $array = []): self
107
    {
108 1
        return new static($array);
109
    }
110
111
    /**
112
     * @param string $delimiter If the option `regexp` is `true` this must be a regular expression
113
     * @param string $string
114
     * @param array  $options   if the option `regexp` is `true` the string is split by using `preg_split()`, otherwise
115
     *                          `explode()` is used
116
     *
117
     * @return self
118
     *
119
     * @throws \InvalidArgumentException if delimiter is an invalid regular exception
120
     */
121 3
    public static function createFromString(string $delimiter, string $string, array $options = []): self
122
    {
123 3
        $options = array_merge(['regexp' => false], $options);
124 3
        $chain   = new static();
125
126 3
        if ($options['regexp']) {
127 2
            $split = @preg_split($delimiter, $string);
128 2
            if (false === $split) {
129 1
                throw new \InvalidArgumentException('Invalid pattern "'.$delimiter.'"');
130
            }
131 1
            $chain->array = $split;
132
        } else {
133 1
            $chain->array = explode($delimiter, $string);
134
        }
135
136 2
        return $chain;
137
    }
138
139
    /**
140
     * Create a new Chain and fill with values.
141
     *
142
     * Creates a new Chain and fills the array with `num` entries of the value of `value` parameters, keys starting
143
     * at the `startIndex` parameter.
144
     *
145
     * @param int   $startIndex The first index of the array. If `startIndex` is negative, the first index of the
146
     *                          returned array will be `startIndex` and the following indices will start from zero.
147
     * @param int   $num        Number of elements to insert. Must be greater than or equal to zero.
148
     * @param mixed $value      value to use for filling
149
     *
150
     * @return self
151
     */
152 1
    public static function fill(int $startIndex, int $num, $value = null): self
153
    {
154 1
        return new self(array_fill($startIndex, $num, $value));
155
    }
156
}
157