Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7a19350

Browse filesBrowse files
bug #28497 [VarDumper] Fix global dump function return value for PHP7 (patrickcarlohickman)
This PR was squashed before being merged into the 3.4 branch (closes #28497). Discussion ---------- [VarDumper] Fix global dump function return value for PHP7 Retarget of PR #28491. Reposting description below, with relevant updates. | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | In 3.4, the global `dump()` helper function in the VarDumper component was updated to return the arguments passed in to it. However, due to reusing the argument variable in the function, this introduces a bug in PHP7 in the return value of the function. The variable used in the `foreach` loop overwrites the value passed in by the first argument. In PHP5, this is okay. In PHP7, even though the argument is passed by value, the value returned by `func_get_args()` is affected by changes to the arguments inside the function. This is a change from PHP5. From the documentation for [`func_get_args()`](http://php.net/manual/en/function.func-get-args.php): > If the arguments are passed by reference, any changes to the arguments will be reflected in the values returned by this function. As of PHP 7 the current values will also be returned if the arguments are passed by value. This PR simply changes the name of the variable used in the `foreach` loop. It also adds a test file to test the return value of the global `dump()` function. This is my first contribution to Symfony, so please let me know if the issue should be resolved in a different manner, or if the test should be modified in any way. Thanks, Patrick Commits ------- 0def211 [VarDumper] Fix global dump function return value for PHP7
2 parents 4957fa0 + 0def211 commit 7a19350
Copy full SHA for 7a19350

File tree

Expand file treeCollapse file tree

2 files changed

+59
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+59
-2
lines changed

‎src/Symfony/Component/VarDumper/Resources/functions/dump.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Resources/functions/dump.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
*/
1818
function dump($var)
1919
{
20-
foreach (func_get_args() as $var) {
21-
VarDumper::dump($var);
20+
foreach (func_get_args() as $v) {
21+
VarDumper::dump($v);
2222
}
2323

2424
if (1 < func_num_args()) {
+57Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Tests\Dumper;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\VarDumper\Cloner\VarCloner;
16+
use Symfony\Component\VarDumper\Dumper\CliDumper;
17+
use Symfony\Component\VarDumper\VarDumper;
18+
19+
class FunctionsTest extends TestCase
20+
{
21+
public function testDumpReturnsFirstArg()
22+
{
23+
$this->setupVarDumper();
24+
25+
$var1 = 'a';
26+
27+
ob_start();
28+
$return = dump($var1);
29+
$out = ob_get_clean();
30+
31+
$this->assertEquals($var1, $return);
32+
}
33+
34+
public function testDumpReturnsAllArgsInArray()
35+
{
36+
$this->setupVarDumper();
37+
38+
$var1 = 'a';
39+
$var2 = 'b';
40+
$var3 = 'c';
41+
42+
ob_start();
43+
$return = dump($var1, $var2, $var3);
44+
$out = ob_get_clean();
45+
46+
$this->assertEquals(array($var1, $var2, $var3), $return);
47+
}
48+
49+
protected function setupVarDumper()
50+
{
51+
$cloner = new VarCloner();
52+
$dumper = new CliDumper('php://output');
53+
VarDumper::setHandler(function ($var) use ($cloner, $dumper) {
54+
$dumper->dump($cloner->cloneVar($var));
55+
});
56+
}
57+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.