uuid icon indicating copy to clipboard operation
uuid copied to clipboard

UuidInterface generates deprecated message with PHP 8.1

Open jokaorgua opened this issue 2 years ago • 8 comments

UuidInterface generates deprecated messasge when using in PHP 8.1

Description

Current UuidInterface implementation is

interface UuidInterface extends
    DeprecatedUuidInterface,
    JsonSerializable,
    Serializable

and it generates deprecation message

Mock_UuidInterface_e896a8df implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary)

Steps to reproduce

Mock an UuidInterface with phpunit mock

$this->getMockBuilder(UuidInterface::class)->disableOriginalConstructor()->getMock();

Expected behavior

No deprecation message

Environment details

php: 8.1.1 OS: Linux Arch: Arm

jokaorgua avatar Jan 18 '22 11:01 jokaorgua

possible fix

Add to UuidInterface

    public function __serialize(): array;

    public function __unserialize(array $data): void;

jokaorgua avatar Jan 18 '22 13:01 jokaorgua

This will require a major version bump, since it's a change to an interface. 😕

ramsey avatar Jan 18 '22 13:01 ramsey

@ramsey maybe this ticket should be closed and new one should be opened with 'Php 8.1 compatibility' subject?

jokaorgua avatar Jan 18 '22 13:01 jokaorgua

I'm not following. Why does this need a new ticket?

ramsey avatar Jan 18 '22 22:01 ramsey

Seems to me a lot of things will be changed to be compatible with PHP 8.1. Not only interface fixing but also the phpdocs for phpstan.  That’s why I’ve suggested a new ticket.

Of course everything could be done in this ticket :)

Best regards, Dmytro Kovalenko @.*** 18 янв. 2022 г., 23:25 +0100, Ben Ramsey @.***>, писал:

I'm not following. Why does this need a new ticket? — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

jokaorgua avatar Jan 19 '22 06:01 jokaorgua

This will require a major version bump, since it's a change to an interface.

Hi @ramsey

Wouldn't using #[\ReturnTypeWillChange] suffice in this case?

Edit: nevermind, initially I assumed it was because the return types, I see now is tied to the Serializable interface.

agustingomes avatar Jan 19 '22 15:01 agustingomes

The request is to add it to UuidInterface. Any change to the interface requires a major version bump.

#[\ReturnTypeWillChange] won't fix deprecation warnings for Serializable, which is an interface that's going away.

ramsey avatar Jan 19 '22 15:01 ramsey

@jokaorgua probably this can be fixed only in PHPUnit (which generates mock implementation without __serialize/__unserialize methods)

Introducing new methods to the interface would be the wrong choice.

andrew-demb avatar Apr 26 '22 07:04 andrew-demb

I'm going to close this issue, since I don't think there's anything that can be done about it in version 4.x of ramsey/uuid.

ramsey avatar Dec 19 '22 23:12 ramsey

I've found a convenient way to make UuidInterface mockable for PHP 8.1. You can override the original UuidInterface by yours via composer. Just add these two lines in your composer.json:

"autoload-dev": {
    "psr-4": {
        <...>
    },
    "exclude-from-classmap": ["vendor/ramsey/uuid/src/UuidInterface.php"],
    "files": ["src/overrides/ramsey/uuid/UuidInterface.php"]
}

Then copy the original UuidInterface into src/overrides/ramsey/uuid/UuidInterface.php file and append Serializable interface methods:

public function __serialize(): array;

public function __unserialize(array $data): void;

Do not forget to call composer dump-autoload at the end.

acelot avatar Jun 18 '23 06:06 acelot

My stance on this matter is that there is no need to mock a UUID. If you need the UUID to have a fixed value, you can create one using Uuid::fromString() to pass to your SUT. If your SUT generates the UUID inside the unit, you shouldn't need to replace it with a mock. Instead, in whatever value your SUT returns, you can assert the UUID property/method/etc. has an instance of Ramsey\Uuid\UuidInterface. That should be enough to validate the behavior.

ramsey avatar Jun 19 '23 04:06 ramsey

@ramsey Agree with you. Unfortunately, I need to do this override, because my project have too many (thousands) test-cases that uses mocked UuidInterface :(

acelot avatar Jun 19 '23 06:06 acelot

Can't you just bulk replace $this->getMockBuilder(UuidInterface::class)->disableOriginalConstructor()->getMock() with Uuid::random() or something?

enumag avatar Jun 19 '23 06:06 enumag

Sure, it's possible. But first you need to set up a rule so that other developers do not write in the old way.

P.S. Random values in test-cases? )))

acelot avatar Jun 19 '23 06:06 acelot