Skip to content

Commit

Permalink
Made \nspl\args PHP 7 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
ihor committed Jan 5, 2017
1 parent b1a3799 commit a8f95b8
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions nspl/args.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,19 +538,36 @@ function throwExpectsException($arg, $hadTo, $atPosition = null, $exception = '\
$has ? 'has' : 'must',
$hadTo,
is_scalar($arg)
? (', ' . \nspl\getType($arg) . ' ' . var_export($arg, true) . ' given')
? (', ' . \nspl\getType($arg) . ' ' . (is_double($arg) ? $arg : var_export($arg, true)) . ' given')
: (', ' . \nspl\getType($arg) . (is_array($arg) ? (' ' . json_encode($arg)): '') . ' given')
));
}

$setter = function($property, $arg) { $this->{$property} = $arg; };
if (PHP_MAJOR_VERSION >= 7) {
$filePropertyReflection = new \ReflectionProperty(get_class($exception), 'file');
$filePropertyReflection->setAccessible(true);
$filePropertyReflection->setValue($exception, $file);

$exceptionSetter = $setter->bindTo($exception, $exception);
$exceptionSetter('file', $file);
$exceptionSetter('line', $line);
$linePropertyReflection = new \ReflectionProperty(get_class($exception), 'line');
$linePropertyReflection->setAccessible(true);
$linePropertyReflection->setValue($exception, $line);

$baseExceptionSetter = $setter->bindTo($exception, 'Exception');
$baseExceptionSetter('trace', a\drop($exception->getTrace(), 2));
$tracePropertyReflection = new \ReflectionProperty('Exception', 'trace');
$tracePropertyReflection->setAccessible(true);
$tracePropertyReflection->setValue($exception, a\drop($exception->getTrace(), 2));
}
else {
$setter = function($property, $arg) {
$this->{$property} = $arg;
};

$exceptionSetter = $setter->bindTo($exception, $exception);
$exceptionSetter('file', $file);
$exceptionSetter('line', $line);

$baseExceptionSetter = $setter->bindTo($exception, 'Exception');
$baseExceptionSetter('trace', a\drop($exception->getTrace(), 2));
}

throw $exception;
}
Expand Down

0 comments on commit a8f95b8

Please sign in to comment.