From 14d64e1163741d974dfe87a77dee64e123c7e380 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 25 Feb 2018 22:55:34 +0100 Subject: [PATCH] Minor performance gain by putting the ReflectionClass instance into a var instead of creating one each time we want to use it. --- src/functions.php | 3 ++- tests/MissingAttributes.php | 8 ++++++++ tests/ThrowableDecodeTest.php | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 tests/MissingAttributes.php diff --git a/src/functions.php b/src/functions.php index 58156f4..5cfefa4 100644 --- a/src/functions.php +++ b/src/functions.php @@ -57,9 +57,10 @@ function throwable_decode($json) array_pop($properties); + $class = new ReflectionClass($json['class']); $throwable = new $json['class'](); foreach ($properties as $key) { - if (!(new ReflectionClass($json['class']))->hasProperty($key)) { + if (!$class->hasProperty($key)) { continue; } diff --git a/tests/MissingAttributes.php b/tests/MissingAttributes.php new file mode 100644 index 0000000..11ad717 --- /dev/null +++ b/tests/MissingAttributes.php @@ -0,0 +1,8 @@ +getTrace()); self::assertSame('whoops', $exception->getMessage()); } + + public function testWithMissingAttributes() + { + $json = [ + 'message' => 'whoops', + 'code' => 13, + 'file' => __FILE__, + 'line' => 0, + 'trace' => [], + 'class' => 'WyriHaximus\Tests\MissingAttributes', + ]; + + /** @var MissingAttributes $exception */ + $exception = WyriHaximus\throwable_decode($json); + self::assertSame('whoops', $exception->message); + self::assertFalse(isset($exception->code)); + } }