Skip to content

Commit 66f650c

Browse files
Merge pull request #99 from Hi-Folks/feat/phpstan2
PHPstan version 2 and Rector version 2
2 parents d54f49a + 9a624c3 commit 66f650c

4 files changed

Lines changed: 21 additions & 35 deletions

File tree

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
"require-dev": {
2121
"laravel/pint": "^1.2",
2222
"phpunit/phpunit": "^11.0",
23-
"phpstan/phpstan": "^1.6",
24-
"rector/rector": "^1"
23+
"phpstan/phpstan": "^2.0",
24+
"rector/rector": "^2.0"
2525
},
2626
"autoload": {
2727
"psr-4": {

src/Arr.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,9 @@ final class Arr implements Iterator, ArrayAccess, Countable
2222
{
2323
use Calculable;
2424

25-
/** @var array<int|string, mixed> */
26-
private array $arr;
27-
2825
/** @param array<int|string, mixed> $arr */
29-
public function __construct(array $arr = [])
26+
public function __construct(private array $arr = [])
3027
{
31-
$this->arr = $arr;
3228
}
3329

3430
public static function fromFunction(callable $callable, int $count): Arr
@@ -111,7 +107,7 @@ public function arr(): array
111107
*
112108
* @param non-empty-string $charNestedKey
113109
*/
114-
public function get(mixed $key, mixed $defaultValue = null, string $charNestedKey = "."): mixed
110+
public function get(int|string $key, mixed $defaultValue = null, string $charNestedKey = "."): mixed
115111
{
116112
if (is_string($key)) {
117113
$keyString = strval($key);
@@ -137,7 +133,7 @@ public function get(mixed $key, mixed $defaultValue = null, string $charNestedKe
137133
* In the case the $key doesn't exist, an empty Arr can be returned
138134
* @param non-empty-string $charNestedKey
139135
*/
140-
public function getArr(mixed $key, mixed $defaultValue = null, string $charNestedKey = "."): Arr
136+
public function getArr(int|string $key, mixed $defaultValue = null, string $charNestedKey = "."): Arr
141137
{
142138
$value = $this->getArrNullable($key, $defaultValue, $charNestedKey);
143139
if (is_null($value)) {
@@ -152,7 +148,7 @@ public function getArr(mixed $key, mixed $defaultValue = null, string $charNeste
152148
* In the case the $key doesn't exist, null can be returned
153149
* @param non-empty-string $charNestedKey
154150
*/
155-
public function getArrNullable(mixed $key, mixed $defaultValue = null, string $charNestedKey = "."): Arr|null
151+
public function getArrNullable(int|string $key, mixed $defaultValue = null, string $charNestedKey = "."): Arr|null
156152
{
157153
$value = $this->get($key, $defaultValue, $charNestedKey);
158154
if (is_null($value)) {
@@ -195,7 +191,10 @@ public function set(int|string $key, mixed $value, string $charNestedKey = "."):
195191
$array = &$array[$key];
196192
}
197193

198-
$array[array_shift($keys)] = $value;
194+
$lastKey = array_shift($keys);
195+
if ($lastKey !== null) {
196+
$array[$lastKey] = $value;
197+
}
199198
return;
200199

201200
}
@@ -205,7 +204,7 @@ public function set(int|string $key, mixed $value, string $charNestedKey = "."):
205204
/**
206205
* Unset an array element by their key if it exists
207206
*/
208-
public function unset(mixed $key): bool
207+
public function unset(int|string $key): bool
209208
{
210209
if ($this->get($key)) {
211210
unset($this->arr[$key]);
@@ -315,9 +314,9 @@ public function forEach(callable $callback): self
315314
* It returns Arr or [] depending on $returnArrClass value
316315
*
317316
* @param bool $returnArrClass true if you need Arr object
318-
* @return int|string|array<int|string, mixed>|Arr
317+
* @return array<int|string, mixed>|Arr
319318
*/
320-
public function keys(bool $returnArrClass = false): int|string|array|Arr
319+
public function keys(bool $returnArrClass = false): array|Arr
321320
{
322321
if ($returnArrClass) {
323322
return self::make(array_keys($this->arr));
@@ -504,8 +503,6 @@ public function some(callable $callback): bool
504503
/**
505504
* Determines whether the array includes a certain value $element among its entries,
506505
* returning true or false as appropriate
507-
*
508-
* @param int|null $fromIndex
509506
*/
510507
public function includes(mixed $element, ?int $fromIndex = null): bool
511508
{
@@ -767,15 +764,14 @@ public function find(callable $callback): mixed
767764
* The copyWithin() method shallow copies part of an array to another
768765
* location in the same array and returns it without modifying its length.
769766
*
770-
* @param int|null $end
771767
* @return array<int|string, mixed>
772768
*/
773769
public function copyWithin(int $target, int $start = 0, ?int $end = null): array
774770
{
775771
$arrayLength = $this->length();
776772
$chuck = $this->slice($start, $end);
777773
if ($target < 0) {
778-
$target = $arrayLength - (int) abs($target);
774+
$target = $arrayLength - abs($target);
779775
}
780776

781777
foreach ($chuck as $value) {

src/Table.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,17 @@ public function groupBy(string|int $field): Table
177177
foreach ($this->rows as $value) {
178178
$property = $value->get($field);
179179
$property = $this->castVariableForStrval($property);
180-
if (!$property) {
180+
if ($property === null) {
181181
continue;
182182
}
183-
if (array_key_exists(strval($property), $result)) {
183+
if ($property === false) {
184184
continue;
185185
}
186-
$result[$property] = $value;
186+
$key = is_bool($property) || is_float($property) ? strval($property) : $property;
187+
if (array_key_exists(strval($key), $result)) {
188+
continue;
189+
}
190+
$result[$key] = $value;
187191
}
188192

189193
return self::make(array_values($result));

tests/ArrSetTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,6 @@ public function test_basic_set(): void
3232
public function test_nested_set_array(): void
3333
{
3434
$articleText = "Some words as a sample sentence";
35-
$textFieldArray = [
36-
"type" => "doc",
37-
"content" => [
38-
[
39-
"content" => [
40-
[
41-
"text" => $articleText,
42-
"type" => "text"
43-
]
44-
],
45-
"type" => "paragraph"
46-
]
47-
]
48-
];
4935
$textField = Arr::make();
5036
$textField->set("type", "doc");
5137
$textField->set("content.0.content.0.text", $articleText);

0 commit comments

Comments
 (0)