Skip to content

Commit bf4cd77

Browse files
committed
stan fixes
1 parent 599cf5a commit bf4cd77

File tree

12 files changed

+134
-36
lines changed

12 files changed

+134
-36
lines changed

.github/workflows/test_master.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ jobs:
4646
- name: PHP Code Sniffer
4747
run: composer codesniffer
4848

49-
# - name: PHPStan analysis
50-
# run: composer stan
49+
- name: PHPStan analysis
50+
run: composer stan
5151

5252
code-coverage:
5353
name: Code coverage

src/Interfaces/ArraySelectorInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ interface ArraySelectorInterface
88
{
99
/**
1010
* @template T
11+
*
1112
* @param ArrayViewInterface<T> $source
1213
* @param bool|null $readonly
14+
*
1315
* @return ArrayViewInterface<T>
1416
*/
1517
public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayViewInterface;

src/Interfaces/ArrayViewInterface.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88

99
/**
1010
* @template T
11-
* @extends \ArrayAccess<int, T>
11+
* @extends \ArrayAccess<int, T|array<T>>
12+
* @extends \IteratorAggregate<int, T>
1213
*/
13-
interface ArrayViewInterface extends \ArrayAccess, \Countable, \IteratorAggregate
14+
interface ArrayViewInterface extends \ArrayAccess, \IteratorAggregate, \Countable
1415
{
1516
/**
16-
* @param array<T>|ArrayView<T> $source
17+
* @param array<T>|ArrayViewInterface<T> $source
1718
* @param bool|null $readonly
18-
* @return ArrayView<T>
19+
* @return ArrayViewInterface<T>
1920
*/
20-
public static function toView(&$source, ?bool $readonly = null): ArrayView;
21+
public static function toView(&$source, ?bool $readonly = null): ArrayViewInterface;
2122

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

2728
/**
2829
* @param callable(T): bool $predicate
29-
* @return ArrayViewInterface
30+
* @return ArrayViewInterface<T>
3031
*/
3132
public function filter(callable $predicate): ArrayViewInterface;
3233

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

4647
/**
4748
* @param callable(T, int): T $mapper
49+
*
4850
* @return ArrayViewInterface<T>
4951
*/
5052
public function apply(callable $mapper): self;
5153

5254
/**
5355
* @template U
56+
*
5457
* @param array<U>|ArrayViewInterface<U> $data
5558
* @param callable(T, U, int): T $mapper
59+
*
5660
* @return ArrayViewInterface<T>
5761
*/
5862
public function applyWith($data, callable $mapper): self;
5963

6064
/**
61-
* @param array<T>|ArrayView<T> $newValues
65+
* @param array<T>|ArrayViewInterface<T>|T $newValues
66+
*
6267
* @return ArrayViewInterface<T>
6368
*/
6469
public function set($newValues): self;

src/Selectors/IndexListSelector.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
use Smoren\ArrayView\Interfaces\ArrayViewInterface;
77
use Smoren\ArrayView\Views\ArrayIndexListView;
88

9-
/**
10-
* @template T
11-
* @implements ArraySelectorInterface<T>
12-
*/
139
final class IndexListSelector implements ArraySelectorInterface
1410
{
1511
/**
@@ -26,6 +22,13 @@ public function __construct(array $value)
2622
}
2723

2824
/**
25+
* @template T
26+
*
27+
* @param ArrayViewInterface<T> $source
28+
* @param bool|null $readonly
29+
*
30+
* @return ArrayIndexListView<T>
31+
*
2932
* {@inheritDoc}
3033
*/
3134
public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayIndexListView

src/Selectors/MaskSelector.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ public function __construct(array $value)
2222
}
2323

2424
/**
25+
* @template T
26+
*
27+
* @param ArrayViewInterface<T> $source
28+
* @param bool|null $readonly
29+
*
30+
* @return ArrayMaskView<T>
31+
*
2532
* {@inheritDoc}
2633
*/
2734
public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayMaskView

src/Selectors/SliceSelector.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ public function __construct($slice)
1818
parent::__construct($s->start, $s->end, $s->step);
1919
}
2020

21+
/**
22+
* @template T
23+
*
24+
* @param ArrayViewInterface<T> $source
25+
* @param bool|null $readonly
26+
*
27+
* @return ArraySliceView<T>
28+
*
29+
* {@inheritDoc}
30+
*/
2131
public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayViewInterface
2232
{
2333
return new ArraySliceView($source, $this, $readonly ?? $source->isReadonly());

src/Structs/NormalizedSlice.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @property-read int $start
99
* @property-read int $end
1010
* @property-read int $step
11+
*
12+
* @implements \IteratorAggregate<int, int>
1113
*/
1214
class NormalizedSlice extends Slice implements \Countable, \IteratorAggregate
1315
{
@@ -17,14 +19,17 @@ class NormalizedSlice extends Slice implements \Countable, \IteratorAggregate
1719

1820
public function count(): int
1921
{
20-
return ceil(abs((($this->end - $this->start) / $this->step)));
22+
return intval(ceil(abs((($this->end - $this->start) / $this->step))));
2123
}
2224

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

30+
/**
31+
* @return \Generator<int, int>
32+
*/
2833
public function getIterator(): \Generator
2934
{
3035
for ($i = 0; $i < \count($this); ++$i) {

src/Structs/Slice.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,27 @@ class Slice
2828

2929
/**
3030
* @param string|Slice $s
31-
* @return void
31+
*
32+
* @return Slice
3233
*/
3334
public static function toSlice($s): Slice
3435
{
3536
if ($s instanceof Slice) {
3637
return $s;
3738
}
3839

39-
if (!static::isSliceString($s)) {
40+
if (!self::isSliceString($s)) {
4041
throw new ValueError("Invalid slice: \"{$s}\".");
4142
}
4243

43-
$slice = static::parseSliceString($s);
44+
$slice = self::parseSliceString($s);
4445

4546
return new Slice(...$slice);
4647
}
4748

4849
/**
4950
* @param mixed $s
51+
*
5052
* @return bool
5153
*/
5254
public static function isSlice($s): bool
@@ -56,6 +58,7 @@ public static function isSlice($s): bool
5658

5759
/**
5860
* @param mixed $s
61+
*
5962
* @return bool
6063
*/
6164
public static function isSliceString($s): bool
@@ -72,7 +75,7 @@ public static function isSliceString($s): bool
7275
return false;
7376
}
7477

75-
$slice = static::parseSliceString($s);
78+
$slice = self::parseSliceString($s);
7679

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

95+
/**
96+
* @param int $containerLength
97+
*
98+
* @return NormalizedSlice
99+
*/
92100
public function normalize(int $containerLength): NormalizedSlice
93101
{
94102
// TODO: Need refactor
@@ -103,9 +111,9 @@ public function normalize(int $containerLength): NormalizedSlice
103111
$start = $this->start ?? ($step > 0 ? 0 : $containerLength - 1);
104112
$end = $this->end ?? ($step > 0 ? $containerLength : -1);
105113

106-
$start = round($start);
107-
$end = round($end);
108-
$step = round($step);
114+
$start = intval(round($start));
115+
$end = intval(round($end));
116+
$step = intval(round($step));
109117

110118
$start = Util::normalizeIndex($start, $containerLength, false);
111119
$end = Util::normalizeIndex($end, $containerLength, false);
@@ -138,7 +146,7 @@ public function toString(): string
138146

139147
/**
140148
* @param string $s
141-
* @return array<int>
149+
* @return array<int|null>
142150
*/
143151
private static function parseSliceString(string $s): array
144152
{

src/Util.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66

77
class Util
88
{
9+
/**
10+
* @param int $index
11+
* @param int $containerLength
12+
* @param bool $throwError
13+
*
14+
* @return int
15+
*/
916
public static function normalizeIndex(int $index, int $containerLength, bool $throwError = true): int
1017
{
1118
$dist = $index >= 0 ? $index : abs($index) - 1;
@@ -15,6 +22,11 @@ public static function normalizeIndex(int $index, int $containerLength, bool $th
1522
return $index < 0 ? $containerLength + $index : $index;
1623
}
1724

25+
/**
26+
* @param array<mixed> $source
27+
*
28+
* @return bool
29+
*/
1830
public static function isArraySequential(array $source): bool
1931
{
2032
if (!function_exists('array_is_list')) {

src/Views/ArrayIndexListView.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ArrayIndexListView extends ArrayView
2121
* @param array<T>|ArrayViewInterface<T> $source
2222
* @param array<int> $indexes
2323
* @param bool|null $readonly
24+
*
2425
* @throws ReadonlyError
2526
*/
2627
public function __construct(&$source, array $indexes, ?bool $readonly = null)
@@ -29,16 +30,26 @@ public function __construct(&$source, array $indexes, ?bool $readonly = null)
2930
$this->indexes = $indexes;
3031
}
3132

33+
/**
34+
* {@inheritDoc}
35+
*/
3236
public function toArray(): array
3337
{
38+
/** @var Array<T> */
3439
return array_map(fn(int $index) => $this[$index], array_keys($this->indexes));
3540
}
3641

42+
/**
43+
* {@inheritDoc}
44+
*/
3745
public function count(): int
3846
{
3947
return \count($this->indexes);
4048
}
4149

50+
/**
51+
* {@inheritDoc}
52+
*/
4253
protected function convertIndex(int $i): int
4354
{
4455
return Util::normalizeIndex(

0 commit comments

Comments
 (0)