Skip to content

Commit

Permalink
Fix circular reference handling and cleanup a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
EdeMeijer committed Sep 1, 2014
1 parent 4732952 commit cf1227f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/EdeMeijer/SerializeDebugger/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getDebugResult($data)
new ClosureResolver(),
new SerializableResolver(),
new ObjectResolver(),
new PrimitiveTypeResolver()
new PrimitiveTypeResolver(),
]
)
);
Expand Down
30 changes: 16 additions & 14 deletions src/EdeMeijer/SerializeDebugger/ObjectPropertyExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,12 @@ private function initializePropertyMap()

/**
* @param ReflectionClass $reflector
*/
private function initializeParentPrivateProperties(ReflectionClass $reflector)
{
$parentReflector = $reflector->getParentClass();
if ($parentReflector instanceof ReflectionClass) {
$reflectionProperties = $this->getPrivateReflectionProperties($parentReflector);
$this->initializePropertiesFromReflectionProperties($parentReflector, $reflectionProperties);
}
}

/**
* @param \ReflectionClass $reflector
* @param ReflectionProperty[] $reflectionProperties
*/
private function initializePropertiesFromReflectionProperties(ReflectionClass $reflector, $reflectionProperties)
{
private function initializePropertiesFromReflectionProperties(
ReflectionClass $reflector,
array $reflectionProperties
) {
foreach ($reflectionProperties as $reflectionProperty) {
$reflectionProperty->setAccessible(true);
$value = $reflectionProperty->getValue($this->object);
Expand All @@ -104,6 +94,18 @@ private function initializePropertiesFromReflectionProperties(ReflectionClass $r
$this->initializeParentPrivateProperties($reflector);
}

/**
* @param ReflectionClass $reflector
*/
private function initializeParentPrivateProperties(ReflectionClass $reflector)
{
$parentReflector = $reflector->getParentClass();
if ($parentReflector instanceof ReflectionClass) {
$reflectionProperties = $this->getPrivateReflectionProperties($parentReflector);
$this->initializePropertiesFromReflectionProperties($parentReflector, $reflectionProperties);
}
}

/**
* @param ReflectionClass $reflector
* @return ReflectionProperty[]
Expand Down
15 changes: 9 additions & 6 deletions src/EdeMeijer/SerializeDebugger/Resolver/ChainingResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ public function addResolver(ResolverInterface $resolver)
*/
public function resolve(Node $node, Context $context)
{
foreach ($this->resolvers as $resolver) {
try {
return $resolver->resolve($node, $context);
} catch (TypeNotSupportedException $ex) {
// No problem, just try the next one
if (!$node->hasType()) {
foreach ($this->resolvers as $resolver) {
try {
return $resolver->resolve($node, $context);
} catch (TypeNotSupportedException $ex) {
// No problem, just try the next one
}
}
throw new TypeNotSupportedException();
}
throw new TypeNotSupportedException();
return $node;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ public function __construct(ResolverInterface $baseResolver)
*/
public function resolve(Node $node, Context $context)
{
$node = $this->baseResolver->resolve($node, $context);
foreach ($node->getChildNodes() as $childNodes) {
$this->resolve($childNodes, $context);
if (!$node->hasType()) {
$node = $this->baseResolver->resolve($node, $context);
foreach ($node->getChildNodes() as $childNodes) {
$this->resolve($childNodes, $context);
}
}
return $node;
}
Expand Down
54 changes: 28 additions & 26 deletions src/EdeMeijer/SerializeDebugger/Result/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,37 @@ class Result implements ResultItemCollection
{
/** @var Node[] */
private $nodes = [];
/** @var Node[] */
private $nodesById = [];
/** @var (int[])[] */
private $references = [];
/** @var bool */
private $processed = false;
private $references;

/**
* @param Node[] $nodes
*/
public function __construct(array $nodes)
{
$this->nodes = $nodes;
foreach ($nodes as $node) {
$this->nodesById[$node->getId()] = $node;
}
}

/**
* @return Node[]
*/
public function getRawNodes()
{
$this->process();
return $this->nodes;
}

private function process()
{
if (!$this->processed) {
$this->processed = true;
foreach ($this->nodes as $node) {
$this->references[$node->getId()] = [];
}
foreach ($this->nodes as $parentNode) {
foreach ($parentNode->getChildNodes() as $key => $childNode) {
$this->references[$childNode->getId()][$parentNode->getId()] = $key;
}
}
}
}

/**
* @return ResultItem[]
*/
public function getItems()
{
$res = [];
foreach ($this->getRawNodes() as $node) {
if (!$node->hasType()) {
var_dump($node);
exit;
}
$res[] = new ResultItem(
$node->getData(),
$node->getType(),
Expand All @@ -78,8 +61,8 @@ private function getReferencePaths($node)
for ($pp = 0; $pp < count($path) - 1; $pp++) {
$parentId = $path[$pp];
$childId = $path[$pp + 1];
$parentNode = $this->nodes[$parentId];
$parentKey = $this->references[$childId][$parentId];
$parentNode = $this->nodesById[$parentId];
$parentKey = $this->getReferences()[$childId][$parentId];
$pathString .= $parentNode->getType()->formatKeyAccess($parentKey);
}
if ($pathString !== '') {
Expand All @@ -103,7 +86,7 @@ private function getReferencePathData($node)

foreach ($frontier as $path) {
$base = $path[0];
$parentRefs = $this->references[$base];
$parentRefs = $this->getReferences()[$base];
foreach (array_keys($parentRefs) as $parentId) {
if (!in_array($parentId, $path)) {
$newFrontier[] = array_merge([$parentId], $path);
Expand All @@ -118,4 +101,23 @@ private function getReferencePathData($node)

return $result;
}

/**
* @return array
*/
private function getReferences()
{
if ($this->references === null) {
$this->references = [];
foreach ($this->nodes as $node) {
$this->references[$node->getId()] = [];
}
foreach ($this->nodes as $parentNode) {
foreach ($parentNode->getChildNodes() as $key => $childNode) {
$this->references[$childNode->getId()][$parentNode->getId()] = $key;
}
}
}
return $this->references;
}
}

0 comments on commit cf1227f

Please sign in to comment.