Skip to content

Commit ab4fad5

Browse files
committed
close #27
1 parent ff1e615 commit ab4fad5

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

src/Arguments.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,24 +279,22 @@ private function handleDefaultNested(
279279

280280
private function assertArgumentCount(): void
281281
{
282-
if ($this->parameters()->requiredKeys()->count() <= $this->count) {
282+
$requiredKeys = $this->parameters()->requiredKeys()->toArray();
283+
if (count($requiredKeys) <= $this->count) {
283284
return;
284285
}
285286
$argumentKeys = array_keys($this->arguments);
286287
if ($this->isPositional) {
287288
$missing = array_diff(
288-
array_keys($this->parameters()->keys()),
289+
array_keys($requiredKeys),
289290
$argumentKeys
290291
);
291292
$missing = array_map(
292-
fn (int $key) => $this->parameters()->keys()[$key],
293+
fn (int $key) => $requiredKeys[$key],
293294
$missing
294295
);
295296
} else {
296-
$missing = array_diff(
297-
$this->parameters()->keys(),
298-
$argumentKeys
299-
);
297+
$missing = array_diff($requiredKeys, $argumentKeys);
300298
}
301299

302300
throw new ArgumentCountError(

tests/ParametersTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Chevere\Tests;
1515

16+
use ArgumentCountError;
1617
use BadMethodCallException;
1718
use Chevere\Parameter\ArrayParameter;
1819
use Chevere\Parameter\BoolParameter;
@@ -110,6 +111,19 @@ public function testOptionalMissing(): void
110111
$parameters->optional('foo');
111112
}
112113

114+
public function testRequiredMissingNoOptional(): void
115+
{
116+
$parameters = (new Parameters(foo: string()))
117+
->withOptional('bar', string());
118+
$this->expectException(ArgumentCountError::class);
119+
$this->expectExceptionMessage(
120+
<<<PLAIN
121+
Missing required argument(s): `foo`
122+
PLAIN
123+
);
124+
$parameters();
125+
}
126+
113127
public function testRequiredCasting(): void
114128
{
115129
$parameter = string();

0 commit comments

Comments
 (0)