diff --git a/composer.json b/composer.json index 64e7b5d..2a778b5 100644 --- a/composer.json +++ b/composer.json @@ -20,8 +20,8 @@ "require-dev": { "laravel/pint": "^1.2", "phpunit/phpunit": "^11.0", - "phpstan/phpstan": "^1.6", - "rector/rector": "^1" + "phpstan/phpstan": "^2.0", + "rector/rector": "^2.0" }, "autoload": { "psr-4": { diff --git a/src/Arr.php b/src/Arr.php index 01fc73c..edf70fe 100755 --- a/src/Arr.php +++ b/src/Arr.php @@ -22,13 +22,9 @@ final class Arr implements Iterator, ArrayAccess, Countable { use Calculable; - /** @var array */ - private array $arr; - /** @param array $arr */ - public function __construct(array $arr = []) + public function __construct(private array $arr = []) { - $this->arr = $arr; } public static function fromFunction(callable $callable, int $count): Arr @@ -111,7 +107,7 @@ public function arr(): array * * @param non-empty-string $charNestedKey */ - public function get(mixed $key, mixed $defaultValue = null, string $charNestedKey = "."): mixed + public function get(int|string $key, mixed $defaultValue = null, string $charNestedKey = "."): mixed { if (is_string($key)) { $keyString = strval($key); @@ -137,7 +133,7 @@ public function get(mixed $key, mixed $defaultValue = null, string $charNestedKe * In the case the $key doesn't exist, an empty Arr can be returned * @param non-empty-string $charNestedKey */ - public function getArr(mixed $key, mixed $defaultValue = null, string $charNestedKey = "."): Arr + public function getArr(int|string $key, mixed $defaultValue = null, string $charNestedKey = "."): Arr { $value = $this->getArrNullable($key, $defaultValue, $charNestedKey); if (is_null($value)) { @@ -152,7 +148,7 @@ public function getArr(mixed $key, mixed $defaultValue = null, string $charNeste * In the case the $key doesn't exist, null can be returned * @param non-empty-string $charNestedKey */ - public function getArrNullable(mixed $key, mixed $defaultValue = null, string $charNestedKey = "."): Arr|null + public function getArrNullable(int|string $key, mixed $defaultValue = null, string $charNestedKey = "."): Arr|null { $value = $this->get($key, $defaultValue, $charNestedKey); if (is_null($value)) { @@ -195,7 +191,10 @@ public function set(int|string $key, mixed $value, string $charNestedKey = "."): $array = &$array[$key]; } - $array[array_shift($keys)] = $value; + $lastKey = array_shift($keys); + if ($lastKey !== null) { + $array[$lastKey] = $value; + } return; } @@ -205,7 +204,7 @@ public function set(int|string $key, mixed $value, string $charNestedKey = "."): /** * Unset an array element by their key if it exists */ - public function unset(mixed $key): bool + public function unset(int|string $key): bool { if ($this->get($key)) { unset($this->arr[$key]); @@ -315,9 +314,9 @@ public function forEach(callable $callback): self * It returns Arr or [] depending on $returnArrClass value * * @param bool $returnArrClass true if you need Arr object - * @return int|string|array|Arr + * @return array|Arr */ - public function keys(bool $returnArrClass = false): int|string|array|Arr + public function keys(bool $returnArrClass = false): array|Arr { if ($returnArrClass) { return self::make(array_keys($this->arr)); @@ -504,8 +503,6 @@ public function some(callable $callback): bool /** * Determines whether the array includes a certain value $element among its entries, * returning true or false as appropriate - * - * @param int|null $fromIndex */ public function includes(mixed $element, ?int $fromIndex = null): bool { @@ -767,7 +764,6 @@ public function find(callable $callback): mixed * The copyWithin() method shallow copies part of an array to another * location in the same array and returns it without modifying its length. * - * @param int|null $end * @return array */ public function copyWithin(int $target, int $start = 0, ?int $end = null): array @@ -775,7 +771,7 @@ public function copyWithin(int $target, int $start = 0, ?int $end = null): array $arrayLength = $this->length(); $chuck = $this->slice($start, $end); if ($target < 0) { - $target = $arrayLength - (int) abs($target); + $target = $arrayLength - abs($target); } foreach ($chuck as $value) { diff --git a/src/Table.php b/src/Table.php index c07eb1d..8eea019 100644 --- a/src/Table.php +++ b/src/Table.php @@ -177,13 +177,17 @@ public function groupBy(string|int $field): Table foreach ($this->rows as $value) { $property = $value->get($field); $property = $this->castVariableForStrval($property); - if (!$property) { + if ($property === null) { continue; } - if (array_key_exists(strval($property), $result)) { + if ($property === false) { continue; } - $result[$property] = $value; + $key = is_bool($property) || is_float($property) ? strval($property) : $property; + if (array_key_exists(strval($key), $result)) { + continue; + } + $result[$key] = $value; } return self::make(array_values($result)); diff --git a/tests/ArrSetTest.php b/tests/ArrSetTest.php index 7054c98..f24cd05 100644 --- a/tests/ArrSetTest.php +++ b/tests/ArrSetTest.php @@ -32,20 +32,6 @@ public function test_basic_set(): void public function test_nested_set_array(): void { $articleText = "Some words as a sample sentence"; - $textFieldArray = [ - "type" => "doc", - "content" => [ - [ - "content" => [ - [ - "text" => $articleText, - "type" => "text" - ] - ], - "type" => "paragraph" - ] - ] - ]; $textField = Arr::make(); $textField->set("type", "doc"); $textField->set("content.0.content.0.text", $articleText);