Skip to content

Commit

Permalink
fix duplicates check:
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim MacDonald authored and timacdonald committed Nov 30, 2021
1 parent 172e843 commit 5d835b4
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 25 deletions.
26 changes: 2 additions & 24 deletions src/HasParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ private static function validateOriginalAndAliasHaveNotBeenPassed(Collection $ar
}
}

private static function validateAliasesDontPointToSameParamters(Collection $aliases): void
private static function validateAliasesDontPointToSameParameters(Collection $aliases): void
{
if (static::duplicates($aliases)->isNotEmpty()) {
if ($aliases->unique()->count() !== $aliases->count()) {
throw new TypeError('Two provided aliases cannot point to the same parameter.');
}
}
Expand All @@ -250,26 +250,4 @@ private static function validateAliasesReferenceParameters(Collection $parameter
throw new TypeError('Aliases must reference existing parameters.');
}
}

private static function duplicates(Collection $items): Collection
{
// Copied from Collection::duplicates() v8.36.2 for backwards
// compatibility. This could probably be a macro or an extended class,
// but feels like overkill for this single file package right now.

$uniqueItems = $items->unique(null);

$duplicates = new Collection();

/** @var string $value */
foreach ($items as $key => $value) {
if ($uniqueItems->isNotEmpty() && $value === $uniqueItems->first()) {
$uniqueItems->shift();
} else {
$duplicates[$key] = $value;
}
}

return $duplicates;
}
}
2 changes: 1 addition & 1 deletion tests/HasParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ protected static function parameterAliasMap(): array
return [
'firstAlias' => 'original',
'secondAlias' => 'original',
'thirdAlias' => 'anotherOne',
'fourthAlias' => 'anotherOne',
];
}
};
Expand Down

0 comments on commit 5d835b4

Please sign in to comment.