Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backward compatible usage of the new __serialize/__deserialize methods #105

Merged
merged 2 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
"dev-master": "2.x-dev"
}
}
}
36 changes: 18 additions & 18 deletions src/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ public function isFresh(?int $timestamp = null): bool
*/
public function serialize()
{
return serialize($this->__serialize());
return serialize([
$this->name,
$this->methodMetadata,
$this->propertyMetadata,
$this->fileResources,
$this->createdAt,
]);
}

/**
Expand All @@ -96,34 +102,28 @@ public function serialize()
*/
public function unserialize($str)
{
$this->__unserialize((array) unserialize((string) $str));
[
$this->name,
$this->methodMetadata,
$this->propertyMetadata,
$this->fileResources,
$this->createdAt,
] = unserialize($str);
}

/**
* @return array<mixed>
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
*/
public function __serialize(): array
{
return [
$this->name,
$this->methodMetadata,
$this->propertyMetadata,
$this->fileResources,
$this->createdAt,
];
return [$this->serialize()];
}

/**
* @param array<mixed> $data
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification
*/
public function __unserialize(array $data): void
{
[
$this->name,
$this->methodMetadata,
$this->propertyMetadata,
$this->fileResources,
$this->createdAt,
] = $data;
$this->unserialize($data[0]);
}
}
12 changes: 6 additions & 6 deletions src/MethodMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function invoke(object $obj, array $args = [])
*/
public function serialize()
{
return serialize($this->__serialize());
return serialize([$this->class, $this->name]);
}

/**
Expand All @@ -69,23 +69,23 @@ public function serialize()
*/
public function unserialize($str)
{
$this->__unserialize((array) unserialize((string) $str));
[$this->class, $this->name] = unserialize($str);
}

/**
* @return array<string>
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
*/
public function __serialize(): array
{
return [$this->class, $this->name];
return [$this->serialize()];
}

/**
* @param array<string> $data
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification
*/
public function __unserialize(array $data): void
{
[$this->class, $this->name] = $data;
$this->unserialize($data[0]);
}

/**
Expand Down
18 changes: 9 additions & 9 deletions src/PropertyMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public function __construct(string $class, string $name)
*/
public function serialize()
{
return serialize($this->__serialize());
return serialize([
$this->class,
$this->name,
]);
}

/**
Expand All @@ -53,25 +56,22 @@ public function serialize()
*/
public function unserialize($str)
{
$this->__unserialize((array) unserialize((string) $str));
[$this->class, $this->name] = unserialize($str);
}

/**
* @return array<string>
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
*/
public function __serialize(): array
{
return [
$this->class,
$this->name,
];
return [$this->serialize()];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems to trigger the parent $this->serialize method which will then end in a segmentation fault. Instead $this->serialize I think we need to go with implementing it seperatly. Still this would crash applicatons not implementing __serialize and __unserialize method so I would still suggest great a new major release and revert this changes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we do a major release there is no way to allow applications to support both v2 and v3 versions of this library.

while with this implementation people can just add to their code

    public function __serialize(): array
    {
        return [$this->serialize()];
    }

    public function __unserialize(array $data): void
    {
        $this->unserialize($data[0]);
    }

and they got it working, or if they do not care about deprecations, it works anyway even without adding this code.

}

/**
* @param array<string> $data
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification
*/
public function __unserialize(array $data): void
{
[$this->class, $this->name] = $data;
$this->unserialize($data[0]);
}
}