Skip to content

Improve psalm types in ArrayHelper::group() #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Enh #168: Add benchmark, improve `ArrayHelper::htmlEncode()` performance (@samdark)
- Enh #169: Bump `yiisoft/strings` dependency to `^2.6` (@vjik)
- Enh #171: Improve psalm types in `ArrayHelper::group()` (@vjik)

## 3.2.0 February 01, 2025

Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8.2",
"phpunit/phpunit": "^10.5.45",
"rector/rector": "^2.0.9",
"phpunit/phpunit": "^10.5.48",
"rector/rector": "^2.1.2",
"roave/infection-static-analysis-plugin": "^1.35",
"spatie/phpunit-watcher": "^1.24",
"vimeo/psalm": "^5.26.1 || ^6.5.1",
"phpbench/phpbench": "^1.4"
"phpbench/phpbench": "^1.4.1"
},
"autoload": {
"psr-4": {
Expand All @@ -52,7 +52,6 @@
},
"config": {
"sort-packages": true,
"bump-after-update": "dev",
"allow-plugins": {
"bamarni/composer-bin-plugin": true,
"infection/extension-installer": true,
Expand Down
7 changes: 5 additions & 2 deletions src/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@
foreach ($array as $element) {
if (!is_array($element) && !is_object($element)) {
throw new InvalidArgumentException(
'index() can not get value from ' . gettype($element) .

Check warning on line 757 in src/ArrayHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": @@ @@ /** @var mixed $element */ foreach ($array as $element) { if (!is_array($element) && !is_object($element)) { - throw new InvalidArgumentException('index() can not get value from ' . gettype($element) . '. The $array should be either multidimensional array or an array of objects.'); + throw new InvalidArgumentException('index() can not get value from ' . gettype($element)); } $lastArray =& $result; foreach ($groups as $group) {

Check warning on line 757 in src/ArrayHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "Concat": @@ @@ /** @var mixed $element */ foreach ($array as $element) { if (!is_array($element) && !is_object($element)) { - throw new InvalidArgumentException('index() can not get value from ' . gettype($element) . '. The $array should be either multidimensional array or an array of objects.'); + throw new InvalidArgumentException('index() can not get value from ' . '. The $array should be either multidimensional array or an array of objects.' . gettype($element)); } $lastArray =& $result; foreach ($groups as $group) {

Check warning on line 757 in src/ArrayHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": @@ @@ /** @var mixed $element */ foreach ($array as $element) { if (!is_array($element) && !is_object($element)) { - throw new InvalidArgumentException('index() can not get value from ' . gettype($element) . '. The $array should be either multidimensional array or an array of objects.'); + throw new InvalidArgumentException('index() can not get value from ' . '. The $array should be either multidimensional array or an array of objects.'); } $lastArray =& $result; foreach ($groups as $group) {

Check warning on line 757 in src/ArrayHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": @@ @@ /** @var mixed $element */ foreach ($array as $element) { if (!is_array($element) && !is_object($element)) { - throw new InvalidArgumentException('index() can not get value from ' . gettype($element) . '. The $array should be either multidimensional array or an array of objects.'); + throw new InvalidArgumentException(gettype($element) . '. The $array should be either multidimensional array or an array of objects.'); } $lastArray =& $result; foreach ($groups as $group) {

Check warning on line 757 in src/ArrayHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "Concat": @@ @@ /** @var mixed $element */ foreach ($array as $element) { if (!is_array($element) && !is_object($element)) { - throw new InvalidArgumentException('index() can not get value from ' . gettype($element) . '. The $array should be either multidimensional array or an array of objects.'); + throw new InvalidArgumentException(gettype($element) . 'index() can not get value from ' . '. The $array should be either multidimensional array or an array of objects.'); } $lastArray =& $result; foreach ($groups as $group) {
'. The $array should be either multidimensional array or an array of objects.'
);
}
Expand Down Expand Up @@ -797,12 +797,15 @@
* @param Closure[]|string|string[] $groups The array of keys that will be used to group the input array
* by one or more keys.
*
* @psalm-param iterable<mixed, array|object> $array
*
* @return array The grouped array.
*
* @template T as array|object
* @psalm-param iterable<mixed, T> $array
* @psalm-return array<non-empty-list<T>>
*/
public static function group(iterable $array, array|string $groups): array
{
/** @psalm-var array<non-empty-list<T>> */
return self::index($array, null, $groups);
}

Expand Down Expand Up @@ -943,7 +946,7 @@
*
* @return bool Whether the array contains the specified key.
*/
public static function keyExists(array $array, array|float|int|string $key, bool $caseSensitive = true): bool

Check warning on line 949 in src/ArrayHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": @@ @@ * * @return bool Whether the array contains the specified key. */ - public static function keyExists(array $array, array|float|int|string $key, bool $caseSensitive = true): bool + public static function keyExists(array $array, array|float|int|string $key, bool $caseSensitive = false): bool { if (is_array($key)) { if (empty($key)) {
{
if (is_array($key)) {
if (empty($key)) {
Expand Down Expand Up @@ -1022,7 +1025,7 @@
public static function pathExists(
array $array,
array|float|int|string $path,
bool $caseSensitive = true,

Check warning on line 1028 in src/ArrayHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": @@ @@ * * @psalm-param ArrayPath $path */ - public static function pathExists(array $array, array|float|int|string $path, bool $caseSensitive = true, string $delimiter = '.'): bool + public static function pathExists(array $array, array|float|int|string $path, bool $caseSensitive = false, string $delimiter = '.'): bool { return self::keyExists($array, self::parseMixedPath($path, $delimiter), $caseSensitive); }
string $delimiter = '.'
): bool {
return self::keyExists($array, self::parseMixedPath($path, $delimiter), $caseSensitive);
Expand Down Expand Up @@ -1051,7 +1054,7 @@
$data = iterator_to_array($data);
}

$encoding ??= ini_get('default_charset') ?: 'UTF-8';

Check warning on line 1057 in src/ArrayHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "AssignCoalesce": @@ @@ if (!is_array($data)) { $data = iterator_to_array($data); } - $encoding ??= ini_get('default_charset') ?: 'UTF-8'; + $encoding = ini_get('default_charset') ?: 'UTF-8'; $flags = ENT_QUOTES | ENT_SUBSTITUTE; return $valuesOnly ? self::htmlEncodeValues($data, $flags, $encoding) : self::htmlEncodeKeysAndValues($data, $flags, $encoding); }
$flags = ENT_QUOTES | ENT_SUBSTITUTE;

return $valuesOnly
Expand Down Expand Up @@ -1355,7 +1358,7 @@
$numNestedKeys = count($keys) - 1;
foreach ($keys as $i => $key) {
if (!is_array($excludeNode) || !array_key_exists($key, $excludeNode)) {
continue 2; // Jump to the next filter.

Check warning on line 1361 in src/ArrayHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "Continue_": @@ @@ $numNestedKeys = count($keys) - 1; foreach ($keys as $i => $key) { if (!is_array($excludeNode) || !array_key_exists($key, $excludeNode)) { - continue 2; + break; // Jump to the next filter. } if ($i < $numNestedKeys) {

Check warning on line 1361 in src/ArrayHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": @@ @@ $numNestedKeys = count($keys) - 1; foreach ($keys as $i => $key) { if (!is_array($excludeNode) || !array_key_exists($key, $excludeNode)) { - continue 2; + continue 1; // Jump to the next filter. } if ($i < $numNestedKeys) {
}

if ($i < $numNestedKeys) {
Expand Down