Passed
Push — master ( f33296...7aa466 )
by Smoren
01:56 queued 10s
created

NormalizedSlice::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\ArrayView\Structs;
6
7
use Smoren\ArrayView\Util;
8
9
/**
10
 * Represents a normalized slice definition with start, end, and step values.
11
 *
12
 * @property-read int $start The start index of the normalized slice.
13
 * @property-read int $end The end index of the normalized slice.
14
 * @property-read int $step The step size for selecting elements in the normalized slice.
15
 *
16
 * @implements \IteratorAggregate<int, int>
17
 */
18
class NormalizedSlice extends Slice implements \Countable, \IteratorAggregate
19
{
20
    /**
21
     * @var int|null The start index of the normalized slice.
22
     */
23
    public ?int $start; // TODO int, not int|null, but phpstan do not like it.
24
    /**
25
     * @var int|null The end index of the normalized slice.
26
     */
27
    public ?int $end;
28
    /**
29
     * @var int|null The step size for selecting elements in the normalized slice.
30
     */
31
    public ?int $step;
32
33
    /**
34
     * Creates a new NormalizedSlice instance with optional start, end, and step values.
35
     *
36
     * @param int|null $start The start index of the slice range.
37
     * @param int|null $end The end index of the slice range.
38
     * @param int|null $step The step size for selecting elements in the slice range.
39
     */
40
    public function __construct(int $start = null, int $end = null, int $step = null)
41
    {
42
        parent::__construct($start, $end, $step);
43
    }
44
45
    /**
46
     * Return size of the slice range.
47
     *
48
     * @return int Size of the slice range.
49
     */
50
    public function count(): int
51
    {
52
        return intval(ceil(abs((($this->end - $this->start) / $this->step))));
53
    }
54
55
    /**
56
     * Converts the provided index to the actual index based on the normalized slice parameters.
57
     *
58
     * @param int $i The index to convert.
59
     *
60
     * @return int The converted index value.
61
     */
62
    public function convertIndex(int $i): int
63
    {
64
        return $this->start + Util::normalizeIndex($i, \count($this), false) * $this->step;
65
    }
66
67
    /**
68
     * Return iterator to iterate slice range.
69
     *
70
     * @return \Generator<int, int>
71
     */
72
    public function getIterator(): \Generator
73
    {
74
        for ($i = 0; $i < \count($this); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
75
            yield $this->convertIndex($i);
76
        }
77
    }
78
}
79