diff --git a/Command/Command.php b/Command/Command.php index c2b1f4c7e..71ad4a49e 100644 --- a/Command/Command.php +++ b/Command/Command.php @@ -281,7 +281,14 @@ public function setCode(callable $code) if ($code instanceof \Closure) { $r = new \ReflectionFunction($code); if (null === $r->getClosureThis()) { - $code = \Closure::bind($code, $this); + set_error_handler(static function () {}); + try { + if ($c = \Closure::bind($code, $this)) { + $code = $c; + } + } finally { + restore_error_handler(); + } } } diff --git a/Tests/Command/CommandTest.php b/Tests/Command/CommandTest.php index 34156f7b5..9e1ae05eb 100644 --- a/Tests/Command/CommandTest.php +++ b/Tests/Command/CommandTest.php @@ -398,6 +398,18 @@ public function callableMethodCommand(InputInterface $input, OutputInterface $ou { $output->writeln('from the code...'); } + + public function testSetCodeWithStaticAnonymousFunction() + { + $command = new \TestCommand(); + $command->setCode(static function (InputInterface $input, OutputInterface $output) { + $output->writeln(isset($this) ? 'bound' : 'not bound'); + }); + $tester = new CommandTester($command); + $tester->execute([]); + + $this->assertEquals('interact called'.\PHP_EOL.'not bound'.\PHP_EOL, $tester->getDisplay()); + } } // In order to get an unbound closure, we should create it outside a class