Skip to content

Commit

Permalink
Add ArrayUtils::hasDuplicateValue + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis Fortunier committed Jun 20, 2024
1 parent 695771c commit a814a99
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
CHANGELOG for 1.x
===================
## v1.7.1 - (2024-06-20)
### Added
- `ArrayUtils::hasDuplicateValue` + tests

## v1.7.0 - (2024-06-14)
### Added
- `MonitoringApiControllerTrait` used to centralize ApiCall manipulation during api monitoring
Expand Down
8 changes: 8 additions & 0 deletions src/Utils/ArrayUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,12 @@ public static function flatToMap(?array $array, ?\Closure $fnKey, ?\Closure $fnV

return array_combine($keys, $values);
}

/**
* Inspiration : https://stackoverflow.com/questions/3145607/php-check-if-an-array-has-duplicates
*/
public static function hasDuplicateValue(array $array): bool
{
return count($array) !== count(array_flip($array));
}
}
30 changes: 30 additions & 0 deletions tests/Utils/ArrayUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,4 +576,34 @@ function (int $value) {
],
];
}

/**
* @dataProvider hasDuplicateProvider
*/
public function testHasDuplicateValue(bool $expected, array $array): void
{
$this->assertSame($expected, ArrayUtils::hasDuplicateValue($array));
}

public function hasDuplicateProvider(): array
{
return [
'array_with_string_duplicated' => [
true,
['a', 'b', 'c', 'a']
],
'array_with_string_not_duplicated' => [
false,
['a', 'b', 'c']
],
'array_with_int_duplicated' => [
true,
[1, 15, 15, 50]
],
'array_with_int_not_duplicated' => [
false,
[18, 10, 88]
],
];
}
}

0 comments on commit a814a99

Please sign in to comment.