Skip to content

Commit

Permalink
Avoiding overwriting of private members redeclared in derived classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastiano Degan committed May 6, 2020
1 parent efa4b59 commit fcc13b2
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions src/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,28 +144,13 @@ public function toArray($value)
}

$array = [];

$className = get_class($value);
foreach ((array) $value as $key => $val) {
// Exception traces commonly reference hundreds to thousands of
// objects currently loaded in memory. Including them in the result
// has a severe negative performance impact.
if ("\0Error\0trace" === $key || "\0Exception\0trace" === $key) {
continue;
}

// properties are transformed to keys in the following way:
// private $property => "\0Classname\0property"
// protected $property => "\0*\0property"
// public $property => "property"
if (\preg_match('/^\0.+\0(.+)$/', (string) $key, $matches)) {
$key = $matches[1];
}

// See https://github.com/php/php-src/commit/5721132
if ($key === "\0gcdata") {
if ($this->ignoreKey($key)) {
continue;
}

$key = $this->getExportedKey($key, $className);
$array[$key] = $val;
}

Expand All @@ -184,6 +169,40 @@ public function toArray($value)
return $array;
}

private function ignoreKey($key) {
// Exception traces commonly reference hundreds to thousands of
// objects currently loaded in memory. Including them in the result
// has a severe negative performance impact.
if ("\0Error\0trace" === $key || "\0Exception\0trace" === $key) {
return true;
}

// See https://github.com/php/php-src/commit/5721132
if ($key === "\0gcdata") {
return true;
}

return false;
}

private function getExportedKey($key, $className)
{
// properties are transformed to keys in the following way:
// private $property => "\0Classname\0property"
// protected $property => "\0*\0property"
// public $property => "property"
$ignoredPrefixes = array("", "*", $className);
if (\preg_match('/^\0(.+)\0(.+)$/', (string) $key, $matches)) {
if (in_array($matches[1], $ignoredPrefixes)) {
$key = $matches[2];
} else {
$key = "{$matches[1]}::{$matches[2]}";

Check warning on line 199 in src/Exporter.php

View check run for this annotation

Codecov / codecov/patch

src/Exporter.php#L199

Added line #L199 was not covered by tests
}
}

return $key;
}

/**
* Recursive implementation of export
*
Expand Down

0 comments on commit fcc13b2

Please sign in to comment.