Replace is_object/is_array with switch on gettype
When checking multiple types, this is ~10% slower in the worst case, and ~30-33% faster for the average and best case. See #218
This commit is contained in:
+19
-12
@@ -172,19 +172,26 @@ class Mustache_Context
|
|||||||
private function findVariableInStack($id, array $stack)
|
private function findVariableInStack($id, array $stack)
|
||||||
{
|
{
|
||||||
for ($i = count($stack) - 1; $i >= 0; $i--) {
|
for ($i = count($stack) - 1; $i >= 0; $i--) {
|
||||||
if (is_object($stack[$i]) && !($stack[$i] instanceof Closure)) {
|
switch (gettype($stack[$i])) {
|
||||||
|
case 'object':
|
||||||
|
if (!($stack[$i] instanceof Closure)) {
|
||||||
|
// 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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
// Note that is_callable() *will not work here*
|
case 'array':
|
||||||
// See https://github.com/bobthecow/mustache.php/wiki/Magic-Methods
|
if (array_key_exists($id, $stack[$i])) {
|
||||||
if (method_exists($stack[$i], $id)) {
|
return $stack[$i][$id];
|
||||||
return $stack[$i]->$id();
|
}
|
||||||
} elseif (isset($stack[$i]->$id)) {
|
break;
|
||||||
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];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+15
-12
@@ -113,19 +113,22 @@ abstract class Mustache_Template
|
|||||||
*/
|
*/
|
||||||
protected function isIterable($value)
|
protected function isIterable($value)
|
||||||
{
|
{
|
||||||
if (is_object($value)) {
|
switch (gettype($value)) {
|
||||||
return $value instanceof Traversable;
|
case 'object':
|
||||||
} elseif (is_array($value)) {
|
return $value instanceof Traversable;
|
||||||
$i = 0;
|
|
||||||
foreach ($value as $k => $v) {
|
|
||||||
if ($k !== $i++) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
case 'array':
|
||||||
} else {
|
$i = 0;
|
||||||
return false;
|
foreach ($value as $k => $v) {
|
||||||
|
if ($k !== $i++) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user