Passed
Push — master ( 7aa466...8ea880 )
by Smoren
03:23 queued 01:36
created

NormalizedSlice::getStart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
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
 * @implements \IteratorAggregate<int, int>
13
 */
14
class NormalizedSlice extends Slice implements \Countable, \IteratorAggregate
15
{
16
    /**
17
     * Creates a new NormalizedSlice instance with optional start, end, and step values.
18
     *
19
     * @param int|null $start The start index of the slice range.
20
     * @param int|null $end The end index of the slice range.
21
     * @param int|null $step The step size for selecting elements in the slice range.
22
     */
23
    public function __construct(int $start = null, int $end = null, int $step = null)
24
    {
25
        parent::__construct($start, $end, $step);
26
    }
27
28
    /**
29
     * Getter for the start index of the normalized slice.
30
     *
31
     * @return int
32
     */
33
    public function getStart(): int
34
    {
35
        /** @var int */
36
        return $this->start;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->start could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
37
    }
38
39
    /**
40
     * Getter for the stop index of the normalized slice.
41
     *
42
     * @return int
43
     */
44
    public function getEnd(): int
45
    {
46
        /** @var int */
47
        return $this->end;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->end could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
48
    }
49
50
    /**
51
     * Getter for the step of the normalized slice.
52
     *
53
     * @return int
54
     */
55
    public function getStep(): int
56
    {
57
        /** @var int */
58
        return $this->step;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->step could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
59
    }
60
61
    /**
62
     * Return size of the slice range.
63
     *
64
     * @return int Size of the slice range.
65
     */
66
    public function count(): int
67
    {
68
        return intval(ceil(abs((($this->end - $this->start) / $this->step))));
69
    }
70
71
    /**
72
     * Converts the provided index to the actual index based on the normalized slice parameters.
73
     *
74
     * @param int $i The index to convert.
75
     *
76
     * @return int The converted index value.
77
     */
78
    public function convertIndex(int $i): int
79
    {
80
        return $this->start + Util::normalizeIndex($i, \count($this), false) * $this->step;
81
    }
82
83
    /**
84
     * Return iterator to iterate slice range.
85
     *
86
     * @return \Generator<int, int>
87
     */
88
    public function getIterator(): \Generator
89
    {
90
        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...
91
            yield $this->convertIndex($i);
92
        }
93
    }
94
}
95