Skip to content

Commit e7c12d3

Browse files
minor #21888 [DI] Replace PHP7-conditional return-type checks by regular type-hints that work on PHP5 (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] Replace PHP7-conditional return-type checks by regular type-hints that work on PHP5 | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 372ff7c [DI] Replace PHP7-conditional return-type checks by regular type-hints that work on PHP5
2 parents fa451b2 + 372ff7c commit e7c12d3

File tree

5 files changed

+10
-235
lines changed

5 files changed

+10
-235
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,23 +1687,19 @@ private function dumpValue($value, $interpolate = true)
16871687

16881688
private function dumpServiceClosure(Reference $reference, $interpolate, $oneLine)
16891689
{
1690-
$type = '';
1691-
if (PHP_VERSION_ID >= 70000 && $reference instanceof TypedReference) {
1692-
$type = $reference->getType();
1693-
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE === $reference->getInvalidBehavior()) {
1694-
$type = ': \\'.$type;
1695-
} elseif (PHP_VERSION_ID >= 70100) {
1696-
$type = ': ?\\'.$type;
1697-
} else {
1698-
$type = '';
1699-
}
1690+
$code = $this->dumpValue($reference, $interpolate);
1691+
1692+
if ($reference instanceof TypedReference) {
1693+
$code = sprintf('$f = function (\\%s $v%s) { return $v; }; return $f(%s);', $reference->getType(), ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior() ? ' = null' : '', $code);
1694+
} else {
1695+
$code = sprintf('return %s;', $code);
17001696
}
17011697

17021698
if ($oneLine) {
1703-
return sprintf('function ()%s { return %s; }', $type, $this->dumpValue($reference, $interpolate));
1699+
return sprintf('function () { %s }', $code);
17041700
}
17051701

1706-
return sprintf("function ()%s {\n return %s;\n }", $type, $this->dumpValue($reference, $interpolate));
1702+
return sprintf("function () {\n %s\n }", $code);
17071703
}
17081704

17091705
/**

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,6 @@ public function testServiceLocator()
648648

649649
$dumper = new PhpDumper($container);
650650

651-
$suffix = PHP_VERSION_ID >= 70100 ? '71' : (PHP_VERSION_ID >= 70000 ? '70' : '55');
652-
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_locator_php'.$suffix.'.php', $dumper->dump());
651+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_locator.php', $dumper->dump());
653652
}
654653
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator_php55.php renamed to src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected function getFooServiceService()
8787
return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\ServiceLocator(array('bar' => function () {
8888
return ${($_ = isset($this->services['bar_service']) ? $this->services['bar_service'] : $this->get('bar_service')) && false ?: '_'};
8989
}, 'baz' => function () {
90-
return ${($_ = isset($this->services['baz_service']) ? $this->services['baz_service'] : $this->getBazServiceService()) && false ?: '_'};
90+
$f = function (\stdClass $v) { return $v; }; return $f(${($_ = isset($this->services['baz_service']) ? $this->services['baz_service'] : $this->getBazServiceService()) && false ?: '_'});
9191
}));
9292
}
9393

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator_php70.php

Lines changed: 0 additions & 110 deletions
This file was deleted.

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator_php71.php

Lines changed: 0 additions & 110 deletions
This file was deleted.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy