Skip to content

Commit

Permalink
Optimized deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
sorinsarca committed Dec 25, 2024
1 parent fd6da9a commit 240fa3c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/ClassInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
class ClassInfo
{
public ReflectionClass $reflector;
public ReflectionClass $reflection;
public bool $box;
public bool $hasMagicSerialize;
public bool $hasMagicUnserialize;
Expand All @@ -27,14 +27,14 @@ class ClassInfo

public function __construct(string $className)
{
$reflector = $this->reflector = new ReflectionClass($className);
$this->box = empty($reflector->getAttributes(NoBox::class));
$this->hasMagicSerialize = $reflector->hasMethod("__serialize");
$this->hasMagicUnserialize = $reflector->hasMethod("__unserialize");
$reflection = $this->reflection = new ReflectionClass($className);
$this->box = empty($reflection->getAttributes(NoBox::class));
$this->hasMagicSerialize = $reflection->hasMethod("__serialize");
$this->hasMagicUnserialize = $reflection->hasMethod("__unserialize");
}

public function className(): string
{
return $this->reflector->name;
return $this->reflection->name;
}
}
4 changes: 2 additions & 2 deletions src/DeserializationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ private function unboxObject(Box $box): object
// handle
$this->handle($value);
}
}, $info->className(), $this);
}, $info->reflection, $this);
}

// create a new object
$object = $info->reflector->newInstanceWithoutConstructor();
$object = $info->reflection->newInstanceWithoutConstructor();

// we eagerly save cache
$this->unboxed[$box] = $object;
Expand Down
4 changes: 1 addition & 3 deletions src/GenericObjectSerialization.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ public static function serialize(object $object): array
return $data;
}

public static function unserialize(array &$data, callable $solve, string $class): object
public static function unserialize(array &$data, callable $solve, ReflectionClass $reflection): object
{
$reflection = new ReflectionClass($class);

$object = $reflection->newInstanceWithoutConstructor();

$solve($object, $data);
Expand Down
2 changes: 1 addition & 1 deletion src/SerializationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private function shouldBox(ClassInfo $info): bool
return $this->shouldBox[$info] = true;
}

if ($info->reflector->isInternal()) {
if ($info->reflection->isInternal()) {
// internal classes are supported with custom serializers only
return $this->shouldBox[$info] = false;
}
Expand Down

0 comments on commit 240fa3c

Please sign in to comment.