Skip to content

Commit

Permalink
Add wrapper for call_user_func_array() with try/catch block.
Browse files Browse the repository at this point in the history
Catch TypeError and rethrow LiquidException because Filter is called
with too few arguments.
A LiquidException is more meaningful as TypeError and developer can
handle this like liquid template error.
  • Loading branch information
PATROMO committed Jun 14, 2019
1 parent 034d499 commit aa26c48
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/Liquid/Filterbank.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function invoke($name, $value, array $args = array())

// If we have a callback
if (is_callable($class)) {
return call_user_func_array($class, $args);
return $this->callUserFuncArrayInTryCatch($class, $args);
}

// If we have a registered object for the class, use that instead
Expand All @@ -144,10 +144,29 @@ public function invoke($name, $value, array $args = array())

// If we're calling a function
if ($class === false) {
return call_user_func_array($name, $args);
return $this->callUserFuncArrayInTryCatch($name, $args);
}

// Call a class or an instance method
return call_user_func_array(array($class, $name), $args);
return $this->callUserFuncArrayInTryCatch(array($class, $name), $args);
}

/**
* This is a wrapper for call_user_func_array() with try/catch
* to cast TypeError in LiquidException.
*
* @param callback $function
* @param array $param_arr
*
* @throws \Liquid\LiquidException
* @return mixed
*/
private function callUserFuncArrayInTryCatch($function, array $param_arr)
{
try {
return call_user_func_array($function, $param_arr);
} catch (\TypeError $typeError) {
throw new LiquidException($typeError->getMessage(), 0, $typeError);
}
}
}

0 comments on commit aa26c48

Please sign in to comment.