Skip to content

Commit 6034cfb

Browse files
committed
Merge branch 'fix-cli-option-binding'
2 parents 9d30780 + 424c185 commit 6034cfb

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/Cli/CliOption.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public function __construct(
384384
$this->Visibility |= CliOptionVisibility::SCHEMA;
385385
}
386386

387-
if (func_num_args() >= 20) {
387+
if (func_num_args() >= 22) {
388388
$this->BindTo = &$bindTo;
389389
$this->IsBound = true;
390390
} else {

tests/unit/Cli/CliOptionBuilderTest.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Lkrms\Cli\Catalog\CliOptionVisibility;
99
use Lkrms\Cli\CliOption;
1010
use Lkrms\Cli\CliOptionBuilder;
11+
use Lkrms\Concept\Builder;
1112
use Lkrms\Container\Container;
1213
use Lkrms\Contract\IContainer;
1314
use Lkrms\Tests\TestCase;
@@ -216,22 +217,46 @@ public function testValue(): void
216217
'default' => false,
217218
], $option->getJsonSchema());
218219

220+
$constructor = (new ReflectionClass(CliOption::class))->getConstructor();
221+
222+
$option = $this->getValue();
223+
foreach ($constructor->getParameters() as $param) {
224+
$name = $param->getName();
225+
if (
226+
$param->isPassedByReference() ||
227+
$option->issetB($name) ||
228+
method_exists(Builder::class, $name)
229+
) {
230+
continue;
231+
}
232+
$value = null;
233+
if ($param->isDefaultValueAvailable()) {
234+
$value = $param->getDefaultValue();
235+
}
236+
/** @var CliOptionBuilder */
237+
$option = $option->$name($value);
238+
}
239+
$option = $option->load();
240+
$this->assertSame(false, $option->IsBound, sprintf(
241+
'Option bound to variable without %s::bindTo(). Check comparison with func_num_args() in %s::__construct() is >= %d',
242+
CliOptionBuilder::class,
243+
CliOption::class,
244+
$constructor->getNumberOfParameters(),
245+
));
246+
219247
$bound = null;
220248
$option = $this
221249
->getValue()
222250
->required(false)
223251
->multipleAllowed()
224252
->bindTo($bound)
225253
->load();
226-
$message = sprintf(
254+
$this->assertSame(true, $option->IsBound, sprintf(
227255
'%s::bindTo() failed. Check comparison with func_num_args() in %s::__construct() is >= %d',
228256
CliOptionBuilder::class,
229257
CliOption::class,
230-
(new ReflectionClass(CliOption::class))
231-
->getConstructor()
232-
->getNumberOfParameters(),
233-
);
234-
$this->assertSame(true, $option->IsBound, $message);
258+
$constructor->getNumberOfParameters(),
259+
));
235260
$this->assertNull($bound);
236261
$option->applyValue([]);
237262
$this->assertSame([], $bound);

0 commit comments

Comments
 (0)