Skip to content

Commit

Permalink
Minor performance gain by putting the ReflectionClass instance into a…
Browse files Browse the repository at this point in the history
… var instead of creating one each time we want to use it.
  • Loading branch information
WyriHaximus committed Feb 25, 2018
1 parent c280e48 commit 14d64e1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/functions.php
Expand Up @@ -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;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/MissingAttributes.php
@@ -0,0 +1,8 @@
<?php

namespace WyriHaximus\Tests;

final class MissingAttributes
{
public $message;
}
17 changes: 17 additions & 0 deletions tests/ThrowableDecodeTest.php
Expand Up @@ -27,4 +27,21 @@ public function test()
self::assertSame([], $exception->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));
}
}

0 comments on commit 14d64e1

Please sign in to comment.