Skip to content

Conversation

@jrfnl
Copy link
Member

@jrfnl jrfnl commented Mar 9, 2022

New sniff which addresses the following deprecation in PHP 8.1:

Implementing Serializable without __serialize() and __unserialize()

Either only the new methods should be implemented, if no support for PHP prior to version 7.4 is provided, or both should be implemented.

This sniff will throw a warning when a class implements the Serializable interface, but does not implement the magic __serialize() and __unserialize() methods.

The sniff contains a public $serializableInterfaces property for users to provide their own list of additional interfaces (which extend the Serializable interface) to scan for.

If the sniff comes across an interface declaration extending the Serializable interface and the interface is not in the user provided $serializableInterfaces list, a warning will be thrown for the user to add the interface to the property via a custom ruleset.

Additionally, when the testVersion has a minimum PHP version of 7.4 or higher, an additional check is executed:

  • For interfaces directly extending Serializable AND containing a declaration of both the __serialize() as well as the __unserialize() magic method, thus enforcing the implementation of these methods in classes implementing the interface, a warning will be thrown that the extension of the Serializable interface can be removed.
  • For classes which implement the Serializable interface AND contain a declaration of both the __serialize() as well as the __unserialize() magic method, a warning will be thrown that the implementation of the Serializable interface can be removed.

Includes unit tests.
Includes updating the section about available custom properties in the README.

Refs:

Related #1299, #1310

@jrfnl
Copy link
Member Author

jrfnl commented Mar 9, 2022

I need to investigate the test failure. It may well be that this sniff needs PHPCSUtils 1.0-alpha4.

New sniff which addresses the following deprecation in PHP 8.1:

> **Implementing Serializable without `__serialize()` and `__unserialize()`**
>
> Either only the new methods should be implemented, if no support for PHP prior to version 7.4 is provided, or both should be implemented.

This sniff will throw a warning when a class implements the `Serializable` interface, but does not implement the magic `__serialize()` and `__unserialize()` methods.

The sniff contains a `public` `$serializableInterfaces` property for users to provide their own list of additional interfaces (which extend the `Serializable` interface) to scan for.

If the sniff comes across an interface declaration extending the `Serializable` interface and the interface is not in the user provided `$serializableInterfaces` list, a warning will be thrown for the user to add the interface to the property via a custom ruleset.

Additionally, when the `testVersion` has a minimum PHP version of `7.4` or higher, an additional check is executed:
* For interfaces directly extending `Serializable` AND containing a declaration of both the `__serialize()` as well as the `__unserialize()` magic method, thus enforcing the implementation of these methods in classes implementing the interface, a warning will be thrown that the extension of the `Serializable` interface can be removed.
* For classes which implement the `Serializable` interface AND contain a declaration of both the `__serialize()` as well as the `__unserialize()` magic method, a warning will be thrown that the implementation of the `Serializable` interface can be removed.

Includes unit tests.
Includes updating the section about available custom properties in the README.

Refs:
 * https://www.php.net/manual/en/migration81.deprecated.php#migration81.deprecated.core.serialize-interface
 * https://wiki.php.net/rfc/phase_out_serializable
 * https://www.php.net/manual/en/class.serializable.php
 * https://www.php.net/manual/en/language.oop5.magic.php#object.serialize
 * php/php-src#6494
 * php/php-src@3e6b447
@jrfnl jrfnl force-pushed the php-8.1/new-removed-serializable-sniff branch from fd52b76 to 6558b5c Compare March 9, 2022 04:54
@jrfnl
Copy link
Member Author

jrfnl commented Mar 9, 2022

Fixed it. Had nothing to do with PHPCSUtils, but was related to the fact that this sniff has a custom property which can be set from a ruleset and the tests using that custom property.
The way the property can be set was changed in PHPCS 3.2/3.3 and I was using the new syntax. Had to make some provisions in the sniff to support the old syntax, but should be all good now.

@jrfnl jrfnl marked this pull request as ready for review March 9, 2022 04:56
@wimg wimg merged commit c31d530 into develop Mar 19, 2022
@wimg wimg deleted the php-8.1/new-removed-serializable-sniff branch March 19, 2022 22:34
@pardeep26k
Copy link

HI Do we have plan to release new version to check for pHP compatibility?

@wimg
Copy link
Member

wimg commented Jun 10, 2022

A new version will come when it is ready.
You're always free to help out...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants