Skip to content

Commit

Permalink
Fix #8232 - always reference classes in var_export() via their FQCN
Browse files Browse the repository at this point in the history
This fix corrects a behavior of `var_export()` that was mostly "hidden" until PHP 8.1 introduced:

* properties with object initializers
* constants containing object references
* default values of class properties containing `enum`s

Since `var_export(..., true)` is mostly used in conjunction with code generation,
and we cannot make assumptions about the generated code being placed in the root
namespace, we must always provide the FQCN of a class in exported code.

For example:

```php
<?php

namespace MyNamespace { class Foo {} }

namespace { echo "<?php\n\nnamespace Example;\n\n" . var_export(new \MyNamespace\Foo(), true) . ';'; }
```

produces:

```php
<?php

namespace Example;

MyNamespace\Foo::__set_state(array(
));
```

This code snippet is invalid, because `Example\MyNamespace\Foo::__set_state()` (which
does not exist) is called.

With this patch applied, the code looks like following (valid):

```php
<?php

namespace Example;

\MyNamespace\Foo::__set_state(array(
));
```

Ref: #8232
Ref: Ocramius/ProxyManager#754
  • Loading branch information
Ocramius authored and iluuu1994 committed Apr 22, 2022
1 parent a690a56 commit 46d9157
Show file tree
Hide file tree
Showing 21 changed files with 312 additions and 283 deletions.
2 changes: 1 addition & 1 deletion Zend/tests/bug73350.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $e = new Exception();
var_export($e);
?>
--EXPECTF--
Exception::__set_state(array(
\Exception::__set_state(array(
'message' => '',
'string' => 'Exception in %sbug73350.php:%d
Stack trace:
Expand Down
28 changes: 28 additions & 0 deletions Zend/tests/enum/enum-in-var-export.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Enum in var_export()
--FILE--
<?php

namespace {
enum Foo { case BAR; }
}

namespace A {
enum Foo { case BAR; }
}

namespace A\B {
enum Foo { case BAR; }
}

namespace Test {
echo var_export(\Foo::BAR, true) . "\n";
echo var_export(\A\Foo::BAR, true) . "\n";
echo var_export(\A\B\Foo::BAR, true) . "\n";
}

?>
--EXPECT--
\Foo::BAR
\A\Foo::BAR
\A\B\Foo::BAR
4 changes: 2 additions & 2 deletions Zend/tests/enum/var_export.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ echo str_replace(" \n", "\n", var_export([Foo::Bar], true));

?>
--EXPECT--
Foo::Bar
\Foo::Bar
array (
0 =>
Foo::Bar,
\Foo::Bar,
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object(SensitiveParameterValue)#%d (%d) refcount(%d){
SensitiveParameterValue Object

# var_export()
SensitiveParameterValue::__set_state(array(
\SensitiveParameterValue::__set_state(array(
))

# (array) / json_encode()
Expand Down
4 changes: 2 additions & 2 deletions ext/date/tests/bug52113.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $diff_un = unserialize($diff_s);
$p = new DatePeriod($start, $diff_un, 2);
var_dump($diff_un, $p);

$unser = DateInterval::__set_state(array(
$unser = \DateInterval::__set_state(array(
'y' => 7,
'm' => 6,
'd' => 5,
Expand Down Expand Up @@ -68,7 +68,7 @@ object(DateInterval)#%d (16) {
int(0)
}
string(332) "O:12:"DateInterval":16:{s:1:"y";i:0;s:1:"m";i:0;s:1:"d";i:0;s:1:"h";i:4;s:1:"i";i:0;s:1:"s";i:0;s:1:"f";d:0;s:7:"weekday";i:0;s:16:"weekday_behavior";i:0;s:17:"first_last_day_of";i:0;s:6:"invert";i:0;s:4:"days";i:0;s:12:"special_type";i:0;s:14:"special_amount";i:0;s:21:"have_weekday_relative";i:0;s:21:"have_special_relative";i:0;}"
DateInterval::__set_state(array(
\DateInterval::__set_state(array(
'y' => 0,
'm' => 0,
'd' => 0,
Expand Down
20 changes: 10 additions & 10 deletions ext/intl/tests/dateformat_format.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -318,79 +318,79 @@ Formatted localtime_array is : 12/17/95 12:13 AM
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
Formatted localtime_array is : 18951217 12:13 AM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : Thursday, December 31, 2009 3:02:03 PM GMT-10:00
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : December 31, 2009 3:02:03 PM GMT-10:00
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : Dec 31, 2009 3:02:03 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : 12/31/09 3:02 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : 20091231 03:02 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 2,
'timezone' => 'PDT',
))
------------
Formatted DateTime is : Saturday, December 30, 2000 5:04:05 PM GMT-10:00
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 2,
'timezone' => 'PDT',
))
------------
Formatted DateTime is : December 30, 2000 5:04:05 PM GMT-10:00
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 2,
'timezone' => 'PDT',
))
------------
Formatted DateTime is : Dec 30, 2000 5:04:05 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 2,
'timezone' => 'PDT',
))
------------
Formatted DateTime is : 12/30/00 5:04 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 2,
'timezone' => 'PDT',
Expand Down
20 changes: 10 additions & 10 deletions ext/intl/tests/dateformat_format_variant2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -318,79 +318,79 @@ Formatted localtime_array is : 12/17/95, 12:13 AM
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
Formatted localtime_array is : 18951217 12:13 AM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : Thursday, December 31, 2009 at 3:02:03 PM GMT-10:00
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : December 31, 2009 at 3:02:03 PM GMT-10
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : Dec 31, 2009, 3:02:03 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : 12/31/09, 3:02 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : 20091231 03:02 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 3,
'timezone' => 'America/Los_Angeles',
))
------------
Formatted DateTime is : Saturday, December 30, 2000 at 5:04:05 PM GMT-10:00
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 3,
'timezone' => 'America/Los_Angeles',
))
------------
Formatted DateTime is : December 30, 2000 at 5:04:05 PM GMT-10
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 3,
'timezone' => 'America/Los_Angeles',
))
------------
Formatted DateTime is : Dec 30, 2000, 5:04:05 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 3,
'timezone' => 'America/Los_Angeles',
))
------------
Formatted DateTime is : 12/30/00, 5:04 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 3,
'timezone' => 'America/Los_Angeles',
Expand Down
20 changes: 10 additions & 10 deletions ext/intl/tests/dateformat_format_variant3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -318,79 +318,79 @@ Formatted localtime_array is : 12/17/95, 12:13 AM
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
Formatted localtime_array is : 18951217 12:13 AM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : Thursday, December 31, 2009 at 3:02:03 PM GMT-10:00
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : December 31, 2009 at 3:02:03 PM GMT-10
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : Dec 31, 2009, 3:02:03 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : 12/31/09, 3:02 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : 20091231 03:02 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 2,
'timezone' => 'PDT',
))
------------
Formatted DateTime is : Saturday, December 30, 2000 at 5:04:05 PM GMT-10:00
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 2,
'timezone' => 'PDT',
))
------------
Formatted DateTime is : December 30, 2000 at 5:04:05 PM GMT-10
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 2,
'timezone' => 'PDT',
))
------------
Formatted DateTime is : Dec 30, 2000, 5:04:05 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 2,
'timezone' => 'PDT',
))
------------
Formatted DateTime is : 12/30/00, 5:04 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 2,
'timezone' => 'PDT',
Expand Down
2 changes: 1 addition & 1 deletion ext/spl/tests/bug65967.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ gc_collect_cycles();
var_export($objstore);
?>
--EXPECT--
SplObjectStorage::__set_state(array(
\SplObjectStorage::__set_state(array(
))
2 changes: 1 addition & 1 deletion ext/spl/tests/fixedarray_022.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ call_user_func(function () {
?>
--EXPECTF--
Warning: var_export does not handle circular references in %s on line 5
SplFixedArray::__set_state(array(
\SplFixedArray::__set_state(array(
0 => NULL,
))
object(SplFixedArray)#2 (1) refcount(4){
Expand Down
2 changes: 1 addition & 1 deletion ext/spl/tests/fixedarray_023.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ call_user_func(function () {
Warning: var_export does not handle circular references in %s on line 8

Warning: var_export does not handle circular references in %s on line 8
SplFixedArray::__set_state(array(
\SplFixedArray::__set_state(array(
0 => NAN,
1 => 0.0,
2 => NULL,
Expand Down
Loading

0 comments on commit 46d9157

Please sign in to comment.