Skip to content

Commit

Permalink
Merge branch '4.4' into 5.1
Browse files Browse the repository at this point in the history
* 4.4:
  "export-ignore" contracts and phpunit-bridge
  [Console][Command] Fix Closure code binding when it is a static anonymous function
  • Loading branch information
xabbuh committed Jan 23, 2021
2 parents 8a7755b + 492097a commit 7db4781
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,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();
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions Tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,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
Expand Down

0 comments on commit 7db4781

Please sign in to comment.