Skip to content

Commit

Permalink
stan fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 10, 2024
1 parent 599cf5a commit bf4cd77
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test_master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ jobs:
- name: PHP Code Sniffer
run: composer codesniffer

# - name: PHPStan analysis
# run: composer stan
- name: PHPStan analysis
run: composer stan

code-coverage:
name: Code coverage
Expand Down
2 changes: 2 additions & 0 deletions src/Interfaces/ArraySelectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ interface ArraySelectorInterface
{
/**
* @template T
*
* @param ArrayViewInterface<T> $source
* @param bool|null $readonly
*
* @return ArrayViewInterface<T>
*/
public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayViewInterface;
Expand Down
19 changes: 12 additions & 7 deletions src/Interfaces/ArrayViewInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@

/**
* @template T
* @extends \ArrayAccess<int, T>
* @extends \ArrayAccess<int, T|array<T>>
* @extends \IteratorAggregate<int, T>
*/
interface ArrayViewInterface extends \ArrayAccess, \Countable, \IteratorAggregate
interface ArrayViewInterface extends \ArrayAccess, \IteratorAggregate, \Countable
{
/**
* @param array<T>|ArrayView<T> $source
* @param array<T>|ArrayViewInterface<T> $source
* @param bool|null $readonly
* @return ArrayView<T>
* @return ArrayViewInterface<T>
*/
public static function toView(&$source, ?bool $readonly = null): ArrayView;
public static function toView(&$source, ?bool $readonly = null): ArrayViewInterface;

/**
* @return array<T>
Expand All @@ -26,7 +27,7 @@ public function toArray(): array;

/**
* @param callable(T): bool $predicate
* @return ArrayViewInterface
* @return ArrayViewInterface<T>
*/
public function filter(callable $predicate): ArrayViewInterface;

Expand All @@ -45,20 +46,24 @@ public function subview($selector, bool $readonly = null): ArrayViewInterface;

/**
* @param callable(T, int): T $mapper
*
* @return ArrayViewInterface<T>
*/
public function apply(callable $mapper): self;

/**
* @template U
*
* @param array<U>|ArrayViewInterface<U> $data
* @param callable(T, U, int): T $mapper
*
* @return ArrayViewInterface<T>
*/
public function applyWith($data, callable $mapper): self;

/**
* @param array<T>|ArrayView<T> $newValues
* @param array<T>|ArrayViewInterface<T>|T $newValues
*
* @return ArrayViewInterface<T>
*/
public function set($newValues): self;
Expand Down
11 changes: 7 additions & 4 deletions src/Selectors/IndexListSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
use Smoren\ArrayView\Interfaces\ArrayViewInterface;
use Smoren\ArrayView\Views\ArrayIndexListView;

/**
* @template T
* @implements ArraySelectorInterface<T>
*/
final class IndexListSelector implements ArraySelectorInterface
{
/**
Expand All @@ -26,6 +22,13 @@ public function __construct(array $value)
}

/**
* @template T
*
* @param ArrayViewInterface<T> $source
* @param bool|null $readonly
*
* @return ArrayIndexListView<T>
*
* {@inheritDoc}
*/
public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayIndexListView
Expand Down
7 changes: 7 additions & 0 deletions src/Selectors/MaskSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public function __construct(array $value)
}

/**
* @template T
*
* @param ArrayViewInterface<T> $source
* @param bool|null $readonly
*
* @return ArrayMaskView<T>
*
* {@inheritDoc}
*/
public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayMaskView
Expand Down
10 changes: 10 additions & 0 deletions src/Selectors/SliceSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public function __construct($slice)
parent::__construct($s->start, $s->end, $s->step);
}

/**
* @template T
*
* @param ArrayViewInterface<T> $source
* @param bool|null $readonly
*
* @return ArraySliceView<T>
*
* {@inheritDoc}
*/
public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayViewInterface
{
return new ArraySliceView($source, $this, $readonly ?? $source->isReadonly());
Expand Down
7 changes: 6 additions & 1 deletion src/Structs/NormalizedSlice.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* @property-read int $start
* @property-read int $end
* @property-read int $step
*
* @implements \IteratorAggregate<int, int>
*/
class NormalizedSlice extends Slice implements \Countable, \IteratorAggregate
{
Expand All @@ -17,14 +19,17 @@ class NormalizedSlice extends Slice implements \Countable, \IteratorAggregate

public function count(): int
{
return ceil(abs((($this->end - $this->start) / $this->step)));
return intval(ceil(abs((($this->end - $this->start) / $this->step))));
}

public function convertIndex(int $i): int
{
return $this->start + Util::normalizeIndex($i, \count($this), false) * $this->step;
}

/**
* @return \Generator<int, int>
*/
public function getIterator(): \Generator
{
for ($i = 0; $i < \count($this); ++$i) {
Expand Down
24 changes: 16 additions & 8 deletions src/Structs/Slice.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,27 @@ class Slice

/**
* @param string|Slice $s
* @return void
*
* @return Slice
*/
public static function toSlice($s): Slice
{
if ($s instanceof Slice) {
return $s;
}

if (!static::isSliceString($s)) {
if (!self::isSliceString($s)) {
throw new ValueError("Invalid slice: \"{$s}\".");
}

$slice = static::parseSliceString($s);
$slice = self::parseSliceString($s);

return new Slice(...$slice);
}

/**
* @param mixed $s
*
* @return bool
*/
public static function isSlice($s): bool
Expand All @@ -56,6 +58,7 @@ public static function isSlice($s): bool

/**
* @param mixed $s
*
* @return bool
*/
public static function isSliceString($s): bool
Expand All @@ -72,7 +75,7 @@ public static function isSliceString($s): bool
return false;
}

$slice = static::parseSliceString($s);
$slice = self::parseSliceString($s);

return !(\count($slice) < 1 || \count($slice) > 3);
}
Expand All @@ -89,6 +92,11 @@ public function __construct(?int $start = null, ?int $end = null, ?int $step = n
$this->step = $step;
}

/**
* @param int $containerLength
*
* @return NormalizedSlice
*/
public function normalize(int $containerLength): NormalizedSlice
{
// TODO: Need refactor
Expand All @@ -103,9 +111,9 @@ public function normalize(int $containerLength): NormalizedSlice
$start = $this->start ?? ($step > 0 ? 0 : $containerLength - 1);
$end = $this->end ?? ($step > 0 ? $containerLength : -1);

$start = round($start);
$end = round($end);
$step = round($step);
$start = intval(round($start));
$end = intval(round($end));
$step = intval(round($step));

$start = Util::normalizeIndex($start, $containerLength, false);
$end = Util::normalizeIndex($end, $containerLength, false);
Expand Down Expand Up @@ -138,7 +146,7 @@ public function toString(): string

/**
* @param string $s
* @return array<int>
* @return array<int|null>
*/
private static function parseSliceString(string $s): array
{
Expand Down
12 changes: 12 additions & 0 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

class Util
{
/**
* @param int $index
* @param int $containerLength
* @param bool $throwError
*
* @return int
*/
public static function normalizeIndex(int $index, int $containerLength, bool $throwError = true): int
{
$dist = $index >= 0 ? $index : abs($index) - 1;
Expand All @@ -15,6 +22,11 @@ public static function normalizeIndex(int $index, int $containerLength, bool $th
return $index < 0 ? $containerLength + $index : $index;
}

/**
* @param array<mixed> $source
*
* @return bool
*/
public static function isArraySequential(array $source): bool
{
if (!function_exists('array_is_list')) {
Expand Down
11 changes: 11 additions & 0 deletions src/Views/ArrayIndexListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ArrayIndexListView extends ArrayView
* @param array<T>|ArrayViewInterface<T> $source
* @param array<int> $indexes
* @param bool|null $readonly
*
* @throws ReadonlyError
*/
public function __construct(&$source, array $indexes, ?bool $readonly = null)
Expand All @@ -29,16 +30,26 @@ public function __construct(&$source, array $indexes, ?bool $readonly = null)
$this->indexes = $indexes;
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
/** @var Array<T> */
return array_map(fn(int $index) => $this[$index], array_keys($this->indexes));
}

/**
* {@inheritDoc}
*/
public function count(): int
{
return \count($this->indexes);
}

/**
* {@inheritDoc}
*/
protected function convertIndex(int $i): int
{
return Util::normalizeIndex(
Expand Down
7 changes: 5 additions & 2 deletions src/Views/ArraySliceView.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Smoren\ArrayView\Views;

use Smoren\ArrayView\Interfaces\ArrayViewInterface;
use Smoren\ArrayView\Structs\NormalizedSlice;
use Smoren\ArrayView\Structs\Slice;

Expand All @@ -12,12 +13,14 @@
class ArraySliceView extends ArrayView
{
/**
* @var NormalizedSlice|Slice
* @var NormalizedSlice
*/
protected NormalizedSlice $slice;

/**
* @param NormalizedSlice $slice
* @param Array<T>|ArrayViewInterface<T> $source
* @param Slice $slice
* @param bool|null $readonly
*/
public function __construct(&$source, Slice $slice, ?bool $readonly = null)
{
Expand Down
Loading

0 comments on commit bf4cd77

Please sign in to comment.