Skip to content

Commit b78555a

Browse files
Merge branch '2.8' into 3.1
* 2.8: [Security] Fix test [Validator] phpize default option values test for the Validator component to be present [DependencyInjection] Fix on-invalid attribute type in xsd [FrameworkBundle] Fix PHP form templates on translatable attributes [VarDumper] Fix dumping by-ref variadics [Validator] add Indonesian translation fixed CS [config] Fix issue when key removed and left value only [Console] fixed BC issue with static closures [Security] AbstractVoter method supportsAttribute gives false positive if attribute is zero (0)
2 parents 7cc9549 + d7dae10 commit b78555a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

Command/Command.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,15 @@ public function setCode(callable $code)
276276
if ($code instanceof \Closure) {
277277
$r = new \ReflectionFunction($code);
278278
if (null === $r->getClosureThis()) {
279-
$code = \Closure::bind($code, $this);
279+
if (PHP_VERSION_ID < 70000) {
280+
// Bug in PHP5: https://bugs.php.net/bug.php?id=64761
281+
// This means that we cannot bind static closures and therefore we must
282+
// ignore any errors here. There is no way to test if the closure is
283+
// bindable.
284+
$code = @\Closure::bind($code, $this);
285+
} else {
286+
$code = \Closure::bind($code, $this);
287+
}
280288
}
281289
}
282290

Tests/Command/CommandTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,29 @@ public function testSetCodeBindToClosure($previouslyBound, $expected)
334334
$this->assertEquals('interact called'.PHP_EOL.$expected.PHP_EOL, $tester->getDisplay());
335335
}
336336

337+
public function testSetCodeWithStaticClosure()
338+
{
339+
$command = new \TestCommand();
340+
$command->setCode(self::createClosure());
341+
$tester = new CommandTester($command);
342+
$tester->execute(array());
343+
344+
if (PHP_VERSION_ID < 70000) {
345+
// Cannot bind static closures in PHP 5
346+
$this->assertEquals('interact called'.PHP_EOL.'not bound'.PHP_EOL, $tester->getDisplay());
347+
} else {
348+
// Can bind static closures in PHP 7
349+
$this->assertEquals('interact called'.PHP_EOL.'bound'.PHP_EOL, $tester->getDisplay());
350+
}
351+
}
352+
353+
private static function createClosure()
354+
{
355+
return function (InputInterface $input, OutputInterface $output) {
356+
$output->writeln(isset($this) ? 'bound' : 'not bound');
357+
};
358+
}
359+
337360
public function testSetCodeWithNonClosureCallable()
338361
{
339362
$command = new \TestCommand();

0 commit comments

Comments
 (0)