diff --git a/src/Mustache/Compiler.php b/src/Mustache/Compiler.php index 9161c64..8d079fb 100644 --- a/src/Mustache/Compiler.php +++ b/src/Mustache/Compiler.php @@ -609,9 +609,9 @@ class Mustache_Compiler { if ($this->customEscape) { return sprintf(self::CUSTOM_ESCAPE, $value); - } else { - return sprintf(self::DEFAULT_ESCAPE, $value, var_export($this->entityFlags, true), var_export($this->charset, true)); } + + return sprintf(self::DEFAULT_ESCAPE, $value, var_export($this->entityFlags, true), var_export($this->charset, true)); } /** @@ -631,11 +631,13 @@ class Mustache_Compiler { if ($id === '.') { return 'last'; - } elseif (strpos($id, '.') === false) { - return 'find'; - } else { - return 'findDot'; } + + if (strpos($id, '.') === false) { + return 'find'; + } + + return 'findDot'; } const IS_CALLABLE = '!is_string(%s) && is_callable(%s)'; diff --git a/src/Mustache/Context.php b/src/Mustache/Context.php index 0115ff0..db03acc 100644 --- a/src/Mustache/Context.php +++ b/src/Mustache/Context.php @@ -172,19 +172,32 @@ class Mustache_Context private function findVariableInStack($id, array $stack) { for ($i = count($stack) - 1; $i >= 0; $i--) { - if (is_object($stack[$i]) && !($stack[$i] instanceof Closure)) { + $frame = &$stack[$i]; - // Note that is_callable() *will not work here* - // See https://github.com/bobthecow/mustache.php/wiki/Magic-Methods - if (method_exists($stack[$i], $id)) { - return $stack[$i]->$id(); - } elseif (isset($stack[$i]->$id)) { - return $stack[$i]->$id; - } elseif ($stack[$i] instanceof ArrayAccess && isset($stack[$i][$id])) { - return $stack[$i][$id]; - } - } elseif (is_array($stack[$i]) && array_key_exists($id, $stack[$i])) { - return $stack[$i][$id]; + switch (gettype($frame)) { + case 'object': + if (!($frame instanceof Closure)) { + // Note that is_callable() *will not work here* + // See https://github.com/bobthecow/mustache.php/wiki/Magic-Methods + if (method_exists($frame, $id)) { + return $frame->$id(); + } + + if (isset($frame->$id)) { + return $frame->$id; + } + + if ($frame instanceof ArrayAccess && isset($frame[$id])) { + return $frame[$id]; + } + } + break; + + case 'array': + if (array_key_exists($id, $frame)) { + return $frame[$id]; + } + break; } } diff --git a/src/Mustache/Template.php b/src/Mustache/Template.php index 00b074c..e7156cf 100644 --- a/src/Mustache/Template.php +++ b/src/Mustache/Template.php @@ -113,19 +113,22 @@ abstract class Mustache_Template */ protected function isIterable($value) { - if (is_object($value)) { - return $value instanceof Traversable; - } elseif (is_array($value)) { - $i = 0; - foreach ($value as $k => $v) { - if ($k !== $i++) { - return false; - } - } + switch (gettype($value)) { + case 'object': + return $value instanceof Traversable; - return true; - } else { - return false; + case 'array': + $i = 0; + foreach ($value as $k => $v) { + if ($k !== $i++) { + return false; + } + } + + return true; + + default: + return false; } }