MemoryManager: eliminate unnecessary reference abuse in continueDump()

this makes the flow of data easier to understand, and also sidesteps some PHPStan bugs.
This commit is contained in:
Dylan K. Taylor 2020-02-05 17:06:05 +00:00
parent ad87c11ae1
commit df8e0cf1f5

View File

@ -337,7 +337,7 @@ class MemoryManager{
} }
$staticCount++; $staticCount++;
self::continueDump($property->getValue(), $staticProperties[$className][$property->getName()], $objects, $refCounts, 0, $maxNesting, $maxStringSize); $staticProperties[$className][$property->getName()] = self::continueDump($property->getValue(), $objects, $refCounts, 0, $maxNesting, $maxStringSize);
} }
if(count($staticProperties[$className]) === 0){ if(count($staticProperties[$className]) === 0){
@ -370,14 +370,14 @@ class MemoryManager{
} }
$globalCount++; $globalCount++;
self::continueDump($value, $globalVariables[$varName], $objects, $refCounts, 0, $maxNesting, $maxStringSize); $globalVariables[$varName] = self::continueDump($value, $objects, $refCounts, 0, $maxNesting, $maxStringSize);
} }
file_put_contents($outputFolder . "/globalVariables.js", json_encode($globalVariables, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); file_put_contents($outputFolder . "/globalVariables.js", json_encode($globalVariables, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
$logger->info("[Dump] Wrote $globalCount global variables"); $logger->info("[Dump] Wrote $globalCount global variables");
} }
self::continueDump($startingObject, $data, $objects, $refCounts, 0, $maxNesting, $maxStringSize); $data = self::continueDump($startingObject, $objects, $refCounts, 0, $maxNesting, $maxStringSize);
do{ do{
$continue = false; $continue = false;
@ -425,7 +425,7 @@ class MemoryManager{
$property->setAccessible(true); $property->setAccessible(true);
} }
self::continueDump($property->getValue($object), $info["properties"][$name], $objects, $refCounts, 0, $maxNesting, $maxStringSize); $info["properties"][$name] = self::continueDump($property->getValue($object), $objects, $refCounts, 0, $maxNesting, $maxStringSize);
} }
} }
@ -452,14 +452,14 @@ class MemoryManager{
/** /**
* @param mixed $from * @param mixed $from
* @param mixed $data reference parameter
* @param object[] $objects reference parameter * @param object[] $objects reference parameter
* @param int[] $refCounts reference parameter * @param int[] $refCounts reference parameter
*
* @return mixed
*/ */
private static function continueDump($from, &$data, array &$objects, array &$refCounts, int $recursion, int $maxNesting, int $maxStringSize) : void{ private static function continueDump($from, array &$objects, array &$refCounts, int $recursion, int $maxNesting, int $maxStringSize){
if($maxNesting <= 0){ if($maxNesting <= 0){
$data = "(error) NESTING LIMIT REACHED"; return "(error) NESTING LIMIT REACHED";
return;
} }
--$maxNesting; --$maxNesting;
@ -475,12 +475,11 @@ class MemoryManager{
$data = "(object) $hash@" . get_class($from); $data = "(object) $hash@" . get_class($from);
}elseif(is_array($from)){ }elseif(is_array($from)){
if($recursion >= 5){ if($recursion >= 5){
$data = "(error) ARRAY RECURSION LIMIT REACHED"; return "(error) ARRAY RECURSION LIMIT REACHED";
return;
} }
$data = []; $data = [];
foreach($from as $key => $value){ foreach($from as $key => $value){
self::continueDump($value, $data[$key], $objects, $refCounts, $recursion + 1, $maxNesting, $maxStringSize); $data[$key] = self::continueDump($value, $objects, $refCounts, $recursion + 1, $maxNesting, $maxStringSize);
} }
}elseif(is_string($from)){ }elseif(is_string($from)){
$data = "(string) len(" . strlen($from) . ") " . substr(Utils::printable($from), 0, $maxStringSize); $data = "(string) len(" . strlen($from) . ") " . substr(Utils::printable($from), 0, $maxStringSize);
@ -489,5 +488,7 @@ class MemoryManager{
}else{ }else{
$data = $from; $data = $from;
} }
return $data;
} }
} }