Skip to content

Commit

Permalink
Slice class updated: properties are protected now.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 12, 2024
1 parent 7aa4668 commit 8ea8800
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 26 deletions.
48 changes: 32 additions & 16 deletions src/Structs/NormalizedSlice.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,53 @@
/**
* Represents a normalized slice definition with start, end, and step values.
*
* @property-read int $start The start index of the normalized slice.
* @property-read int $end The end index of the normalized slice.
* @property-read int $step The step size for selecting elements in the normalized slice.
*
* @implements \IteratorAggregate<int, int>
*/
class NormalizedSlice extends Slice implements \Countable, \IteratorAggregate
{
/**
* @var int|null The start index of the normalized slice.
* Creates a new NormalizedSlice instance with optional start, end, and step values.
*
* @param int|null $start The start index of the slice range.
* @param int|null $end The end index of the slice range.
* @param int|null $step The step size for selecting elements in the slice range.
*/
public ?int $start; // TODO int, not int|null, but phpstan do not like it.
public function __construct(int $start = null, int $end = null, int $step = null)
{
parent::__construct($start, $end, $step);
}

/**
* @var int|null The end index of the normalized slice.
* Getter for the start index of the normalized slice.
*
* @return int
*/
public ?int $end;
public function getStart(): int
{
/** @var int */
return $this->start;
}

/**
* @var int|null The step size for selecting elements in the normalized slice.
* Getter for the stop index of the normalized slice.
*
* @return int
*/
public ?int $step;
public function getEnd(): int
{
/** @var int */
return $this->end;
}

/**
* Creates a new NormalizedSlice instance with optional start, end, and step values.
* Getter for the step of the normalized slice.
*
* @param int|null $start The start index of the slice range.
* @param int|null $end The end index of the slice range.
* @param int|null $step The step size for selecting elements in the slice range.
* @return int
*/
public function __construct(int $start = null, int $end = null, int $step = null)
public function getStep(): int
{
parent::__construct($start, $end, $step);
/** @var int */
return $this->step;
}

/**
Expand Down
40 changes: 33 additions & 7 deletions src/Structs/Slice.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,21 @@

/**
* Represents a slice definition for selecting a range of elements.
*
* @property-read int|null $start The start index of the slice range.
* @property-read int|null $end The end index of the slice range.
* @property-read int|null $step The step size for selecting elements in the slice range.
*/
class Slice
{
/**
* @var int|null The start index of the slice range.
*/
public ?int $start;
protected ?int $start;
/**
* @var int|null The end index of the slice range.
*/
public ?int $end;
protected ?int $end;
/**
* @var int|null The step size for selecting elements in the slice range.
*/
public ?int $step;
protected ?int $step;

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

/**
* Getter for the start index of the normalized slice.
*
* @return int|null
*/
public function getStart(): ?int
{
return $this->start;
}

/**
* Getter for the stop index of the normalized slice.
*
* @return int|null
*/
public function getEnd(): ?int
{
return $this->end;
}

/**
* Getter for the step of the normalized slice.
*
* @return int|null
*/
public function getStep(): ?int
{
return $this->step;
}

/**
* Normalizes the slice parameters based on the container length.
*
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/Structs/SliceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function testToSlice($input, array $expected)
$actual = Slice::toSlice($input);
$expectedSlice = new Slice(...$expected);

$this->assertSame($expectedSlice->start, $actual->start);
$this->assertSame($expectedSlice->end, $actual->end);
$this->assertSame($expectedSlice->step, $actual->step);
$this->assertSame($expectedSlice->getStart(), $actual->getStart());
$this->assertSame($expectedSlice->getEnd(), $actual->getEnd());
$this->assertSame($expectedSlice->getStep(), $actual->getStep());
}

/**
Expand Down

0 comments on commit 8ea8800

Please sign in to comment.