From 34373eeb019219ca291e9626d7a5658ec4eb7d44 Mon Sep 17 00:00:00 2001 From: Arnaud Le Blanc Date: Wed, 17 Nov 2010 21:47:21 +0100 Subject: [PATCH] handle spl_object_hash collisions --- lib/Twig/NodeVisitor/SafeAnalysis.php | 31 +++++++++++++++++++++++-------- 1 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/Twig/NodeVisitor/SafeAnalysis.php b/lib/Twig/NodeVisitor/SafeAnalysis.php index 7adfe11..3c5b456 100644 --- a/lib/Twig/NodeVisitor/SafeAnalysis.php +++ b/lib/Twig/NodeVisitor/SafeAnalysis.php @@ -2,21 +2,36 @@ class Twig_NodeVisitor_SafeAnalysis implements Twig_NodeVisitorInterface { - protected $data; - - public function __construct() - { - $this->data = array(); - } + protected $data = array(); public function getSafe(Twig_NodeInterface $node) { - return isset($this->data[spl_object_hash($node)]) ? $this->data[spl_object_hash($node)] : null; + $hash = spl_object_hash($node); + if (isset($this->data[$hash])) { + foreach($this->data[$hash] as $bucket) { + if ($bucket['key'] === $node) { + return $bucket['value']; + } + } + } + return null; } protected function setSafe(Twig_NodeInterface $node, array $safe) { - $this->data[spl_object_hash($node)] = $safe; + $hash = spl_object_hash($node); + if (isset($this->data[$hash])) { + foreach($this->data[$hash] as &$bucket) { + if ($bucket['key'] === $node) { + $bucket['value'] = $safe; + return; + } + } + } + $this->data[$hash][] = array( + 'key' => $node, + 'value' => $safe, + ); } public function enterNode(Twig_NodeInterface $node, Twig_Environment $env) -- 1.7.2.5