Files
pest/src/Plugins/Concerns/HandleArguments.php
T
flap152 8467c64c22 fix: popArgument drops duplicate arguments breaking --parallel --exclude-gropup= (#1674)
* fix: popArgument drops duplicate arguments breaking --parallel multi-exclude-group

Fixes #1437

* fix: ensure popArgument handles duplicate arguments

* fix: update expected test results and snapshots after rebase

---------

Signed-off-by: nuno maduro <enunomaduro@gmail.com>
Co-authored-by: nuno maduro <enunomaduro@gmail.com>
2026-06-12 17:58:37 +01:00

90 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Pest\Plugins\Concerns;
/**
* @internal
*/
trait HandleArguments
{
/**
* Checks if the given argument exists on the arguments.
*
* @param array<int, string> $arguments
*/
public function hasArgument(string $argument, array $arguments): bool
{
foreach ($arguments as $arg) {
if ($arg === $argument) {
return true;
}
if (str_starts_with((string) $arg, "$argument=")) { // @phpstan-ignore-line
return true;
}
}
return false;
}
/**
* Adds the given argument and value to the list of arguments.
*
* @param array<int, string> $arguments
* @return array<int, string>
*/
public function pushArgument(string $argument, array $arguments): array
{
$arguments[] = $argument;
return $arguments;
}
/**
* Pops the given argument from the arguments.
*
* @param array<int, string> $arguments
* @return array<int, string>
*/
public function popArgument(string $argument, array $arguments): array
{
$key = array_search($argument, $arguments, true);
while ($key !== false) {
unset($arguments[$key]);
$key = array_search($argument, $arguments, true);
}
return array_values($arguments);
}
/**
* Pops the given argument and its value from the arguments, returning the value.
*
* @param array<int, string> $arguments
*/
public function popArgumentValue(string $argument, array &$arguments): ?string
{
foreach ($arguments as $key => $value) {
if (str_contains($value, "$argument=")) {
unset($arguments[$key]);
$arguments = array_values($arguments);
return substr($value, strlen($argument) + 1);
}
if ($value === $argument && isset($arguments[$key + 1])) {
$result = $arguments[$key + 1];
unset($arguments[$key], $arguments[$key + 1]);
$arguments = array_values($arguments);
return $result;
}
}
return null;
}
}