Skip to content

Commit 8ea8800

Browse files
committed
Slice class updated: properties are protected now.
1 parent 7aa4668 commit 8ea8800

File tree

3 files changed

+68
-26
lines changed

3 files changed

+68
-26
lines changed

src/Structs/NormalizedSlice.php

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,53 @@
99
/**
1010
* Represents a normalized slice definition with start, end, and step values.
1111
*
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-
*
1612
* @implements \IteratorAggregate<int, int>
1713
*/
1814
class NormalizedSlice extends Slice implements \Countable, \IteratorAggregate
1915
{
2016
/**
21-
* @var int|null The start index of the normalized slice.
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.
2222
*/
23-
public ?int $start; // TODO int, not int|null, but phpstan do not like it.
23+
public function __construct(int $start = null, int $end = null, int $step = null)
24+
{
25+
parent::__construct($start, $end, $step);
26+
}
27+
2428
/**
25-
* @var int|null The end index of the normalized slice.
29+
* Getter for the start index of the normalized slice.
30+
*
31+
* @return int
2632
*/
27-
public ?int $end;
33+
public function getStart(): int
34+
{
35+
/** @var int */
36+
return $this->start;
37+
}
38+
2839
/**
29-
* @var int|null The step size for selecting elements in the normalized slice.
40+
* Getter for the stop index of the normalized slice.
41+
*
42+
* @return int
3043
*/
31-
public ?int $step;
44+
public function getEnd(): int
45+
{
46+
/** @var int */
47+
return $this->end;
48+
}
3249

3350
/**
34-
* Creates a new NormalizedSlice instance with optional start, end, and step values.
51+
* Getter for the step of the normalized slice.
3552
*
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.
53+
* @return int
3954
*/
40-
public function __construct(int $start = null, int $end = null, int $step = null)
55+
public function getStep(): int
4156
{
42-
parent::__construct($start, $end, $step);
57+
/** @var int */
58+
return $this->step;
4359
}
4460

4561
/**

src/Structs/Slice.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,21 @@
1010

1111
/**
1212
* Represents a slice definition for selecting a range of elements.
13-
*
14-
* @property-read int|null $start The start index of the slice range.
15-
* @property-read int|null $end The end index of the slice range.
16-
* @property-read int|null $step The step size for selecting elements in the slice range.
1713
*/
1814
class Slice
1915
{
2016
/**
2117
* @var int|null The start index of the slice range.
2218
*/
23-
public ?int $start;
19+
protected ?int $start;
2420
/**
2521
* @var int|null The end index of the slice range.
2622
*/
27-
public ?int $end;
23+
protected ?int $end;
2824
/**
2925
* @var int|null The step size for selecting elements in the slice range.
3026
*/
31-
public ?int $step;
27+
protected ?int $step;
3228

3329
/**
3430
* Converts a slice string or Slice object into a Slice instance.
@@ -142,6 +138,36 @@ public function __construct(?int $start = null, ?int $end = null, ?int $step = n
142138
$this->step = $step;
143139
}
144140

141+
/**
142+
* Getter for the start index of the normalized slice.
143+
*
144+
* @return int|null
145+
*/
146+
public function getStart(): ?int
147+
{
148+
return $this->start;
149+
}
150+
151+
/**
152+
* Getter for the stop index of the normalized slice.
153+
*
154+
* @return int|null
155+
*/
156+
public function getEnd(): ?int
157+
{
158+
return $this->end;
159+
}
160+
161+
/**
162+
* Getter for the step of the normalized slice.
163+
*
164+
* @return int|null
165+
*/
166+
public function getStep(): ?int
167+
{
168+
return $this->step;
169+
}
170+
145171
/**
146172
* Normalizes the slice parameters based on the container length.
147173
*

tests/unit/Structs/SliceTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public function testToSlice($input, array $expected)
4848
$actual = Slice::toSlice($input);
4949
$expectedSlice = new Slice(...$expected);
5050

51-
$this->assertSame($expectedSlice->start, $actual->start);
52-
$this->assertSame($expectedSlice->end, $actual->end);
53-
$this->assertSame($expectedSlice->step, $actual->step);
51+
$this->assertSame($expectedSlice->getStart(), $actual->getStart());
52+
$this->assertSame($expectedSlice->getEnd(), $actual->getEnd());
53+
$this->assertSame($expectedSlice->getStep(), $actual->getStep());
5454
}
5555

5656
/**

0 commit comments

Comments
 (0)