Passed
Pull Request — master (#7)
by Nicolas
02:00 queued 43s
created

Chunk::chunk()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 15
ccs 0
cts 8
cp 0
crap 30
rs 9.6111
1
<?php
2
3
namespace Cocur\Chain\Link;
4
5
/**
6
 * Chunk.
7
 *
8
 * @author    Nicolas Reynis
9
 */
10
trait Chunk
11
{
12
    /**
13
     * @param int   $size
14
     * @param array $options options, including:
15
     *                       bool `preserveKeys` to prevent reindexing, default to false
16
     *                       bool `decorate` to generate chains instead of arrays, default tu true
17
     *
18
     * @return self
19
     */
20
    public function chunk(int $size, array $options = []): self
21
    {
22
        if (!empty($options['preserveKeys'])) {
23
            $this->array = array_chunk($this->array, $size, $options['preserveKeys']);
0 ignored issues
show
Bug Best Practice introduced by
The property array does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
24
        } else {
25
            $this->array = array_chunk($this->array, $size);
26
        }
27
28
        if (empty($options['decorate']) || $options['decorate'] === true) {
29
            foreach ($this->array as $index => $chunk) {
30
                $this->array[$index] = new static($chunk);
0 ignored issues
show
Unused Code introduced by
The call to Cocur\Chain\Link\Chunk::__construct() has too many arguments starting with $chunk. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
                $this->array[$index] = /** @scrutinizer ignore-call */ new static($chunk);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
31
            }
32
        }
33
34
        return $this;
35
    }
36
}
37