Skip to content

Commit 7db4781

Browse files
committed
Merge branch '4.4' into 5.1
* 4.4: "export-ignore" contracts and phpunit-bridge [Console][Command] Fix Closure code binding when it is a static anonymous function
2 parents 8a7755b + 492097a commit 7db4781

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Command/Command.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,14 @@ public function setCode(callable $code)
285285
if ($code instanceof \Closure) {
286286
$r = new \ReflectionFunction($code);
287287
if (null === $r->getClosureThis()) {
288-
$code = \Closure::bind($code, $this);
288+
set_error_handler(static function () {});
289+
try {
290+
if ($c = \Closure::bind($code, $this)) {
291+
$code = $c;
292+
}
293+
} finally {
294+
restore_error_handler();
295+
}
289296
}
290297
}
291298

Tests/Command/CommandTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,18 @@ public function callableMethodCommand(InputInterface $input, OutputInterface $ou
391391
{
392392
$output->writeln('from the code...');
393393
}
394+
395+
public function testSetCodeWithStaticAnonymousFunction()
396+
{
397+
$command = new \TestCommand();
398+
$command->setCode(static function (InputInterface $input, OutputInterface $output) {
399+
$output->writeln(isset($this) ? 'bound' : 'not bound');
400+
});
401+
$tester = new CommandTester($command);
402+
$tester->execute([]);
403+
404+
$this->assertEquals('interact called'.\PHP_EOL.'not bound'.\PHP_EOL, $tester->getDisplay());
405+
}
394406
}
395407

396408
// In order to get an unbound closure, we should create it outside a class

0 commit comments

Comments
 (0)