protected function ensureContracts(MethodInvocation $invocation, array $contracts, $instance, $scope, array $args)
{
static $invoker = null;
if (!$invoker) {
$invoker = function () {
extract(func_get_arg(0));
return eval('return ' . func_get_arg(1) . '; ?>');
};
}
$instance = is_object($instance) ? $instance : null;
$boundInvoker = $invoker->bindTo($instance, $scope);
foreach ($contracts as $contract) {
$contractExpression = $contract->value;
try {
$invocationResult = $boundInvoker->__invoke($args, $contractExpression);
// we accept as a result only true or null
// null may be a result of assertions from beberlei/assert which passed
if ($invocationResult !== null && $invocationResult !== true) {
$errorMessage = 'Invalid return value received from the assertion body,' . ' only boolean or void can be returned';
throw new DomainException($errorMessage);
}
} catch (\Error $internalError) {
// PHP-7 friendly interceptor for fatal errors
throw new ContractViolation($invocation, $contractExpression, $internalError);
} catch (\Exception $internalException) {
throw new ContractViolation($invocation, $contractExpression, $internalException);
}
}
}