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

Add __serialize/__unserialize #5

Merged

Conversation

ChristophWurst
Copy link

@ChristophWurst ChristophWurst commented May 24, 2022

As of PHP 8.1.0, a class which implements Serializable without also implementing __serialize() and __unserialize() will generate a deprecation warning.

This was more work than I want to admit. Yet this is fully untested.

Upstream PR: horde#21

@ChristophWurst
Copy link
Author

Yay, failing test

@ChristophWurst ChristophWurst force-pushed the fix/magic-serialize-unserialize branch 3 times, most recently from 2b983ac to 7bb8c4e Compare May 25, 2022 11:35
public function __serialize()
{
return array(
'rights' => $this->_rights
Copy link
Author

Choose a reason for hiding this comment

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

strictly speaking this is not compatible with the old serialization methods but https://www.php.net/manual/en/language.oop5.magic.php#object.serialize has to return an array and $this->_rights is false|array.

/**
* @return array
*/
public function __serialize()
Copy link
Author

Choose a reason for hiding this comment

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

same here. the old one serialized to a string, now need to use an array of string

@codecov-commenter
Copy link

Codecov Report

Merging #5 (a983366) into master (01ab0cf) will decrease coverage by 0.32%.
The diff coverage is 52.17%.

@@             Coverage Diff              @@
##             master       #5      +/-   ##
============================================
- Coverage     32.34%   32.01%   -0.33%     
- Complexity     3256     3286      +30     
============================================
  Files            80       80              
  Lines          7721     7777      +56     
============================================
- Hits           2497     2490       -7     
- Misses         5224     5287      +63     
Impacted Files Coverage Δ
lib/Horde/Imap/Client/Base.php 5.30% <0.00%> (-0.01%) ⬇️
lib/Horde/Imap/Client/Cache/Backend.php 0.00% <0.00%> (ø)
lib/Horde/Imap/Client/Cache/Backend/Cache.php 0.00% <0.00%> (ø)
lib/Horde/Imap/Client/Data/AclRights.php 31.81% <0.00%> (-2.61%) ⬇️
lib/Horde/Imap/Client/Data/Thread.php 53.62% <0.00%> (-2.44%) ⬇️
lib/Horde/Imap/Client/Ids.php 90.60% <59.09%> (-2.09%) ⬇️
lib/Horde/Imap/Client/Data/Capability.php 80.64% <66.66%> (-5.57%) ⬇️
lib/Horde/Imap/Client/Data/Envelope.php 76.84% <66.66%> (-4.68%) ⬇️
lib/Horde/Imap/Client/Data/Namespace.php 90.90% <66.66%> (-9.10%) ⬇️
lib/Horde/Imap/Client/Data/SearchCharset.php 63.82% <66.66%> (-5.94%) ⬇️
... and 19 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 01ab0cf...a983366. Read the comment docs.

@ChristophWurst
Copy link
Author

I think this is OK now. @bytestream please have another look :)

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
@ChristophWurst ChristophWurst force-pushed the fix/magic-serialize-unserialize branch from 9d09302 to b94b163 Compare June 9, 2022 13:56
@ChristophWurst
Copy link
Author

Squashed

$data = @unserialize($data);
if (!is_array($data) ||
!isset($data['v']) ||
$this->__unserialize(@unserialize($data));
Copy link
Owner

Choose a reason for hiding this comment

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

I'm slightly uneasy about @ and lack of is_array(), because it will now throw a Throwable instead of an Exception('Cache version changed'). It's unclear to me why @ is used as it's not used in all classes, and also unclear whether the Exception is caught somewhere... but catch (\Exception) is pretty bad anyway so 🤷‍♂️

Copy link
Author

Choose a reason for hiding this comment

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

Yeah 🤷

}

/**
*/
public function unserialize($data)
{
list($this->_required, $this->_optional) = json_decode($data);
$this->__unserialize(json_decode($data, true));
Copy link
Author

Choose a reason for hiding this comment

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

I've also added the true param here. This was obviously broken. json_decode would have deserialized into an object and list expects an array …

@bytestream
Copy link
Owner

LGTM 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants