Skip to content

[Uid] Add support for binary, base-32 and base-58 representations in Uuid::isValid() #57940

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

Merged
merged 1 commit into from
Aug 13, 2024

Conversation

alexandre-daubois
Copy link
Member

Q A
Branch? 7.2
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #57398
License MIT

Allows to use a bitfield in Uuid::isValid() to accept one or many formats:

Uuid::isValid('...', Uuid::FORMAT_BASE_32 | Uuid::FORMAT_RFC_4122);
Uuid::isValid('...', Uuid::FORMAT_ALL);
UUid::isValid('...'); // by default, FORMAT_RFC_4122 is used to match the current behavior

@@ -130,8 +121,14 @@ final public static function v8(string $uuid): UuidV8
return new UuidV8($uuid);
}

public static function isValid(string $uuid): bool
public static function isValid(string $uuid, int $format = self::FORMAT_RFC_4122): bool
Copy link
Member

Choose a reason for hiding this comment

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

I'm not a big fan of bitfields in options (e.g. Uuid::FORMAT_BASE_32 | Uuid::FORMAT_RFC_4122). So, I wonder if we could replace this int by a string|array (maybe renamed the option too to $allowedFormats).

This would be similar to:

Copy link
Member Author

Choose a reason for hiding this comment

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

I like bit-fields because it reads well, it is quick and easy to test a flag, but also because it offers nice auto-completion and static analysis thanks to @param int-mask-of.

Those seems a bit more difficult with a string|array. I'm curious to understand why you're not a big fan of bit-field and why'd you prefer an array of options? 🙂

Copy link
Member

Choose a reason for hiding this comment

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

A bit-field is more appropriate to me also.

Note that adding an argument is a BC break, we should comment it and use func_get_arg instead

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh yes, I got confused with private methods. It's updated now

@nicolas-grekas
Copy link
Member

Thank you @alexandre-daubois.

@nicolas-grekas nicolas-grekas merged commit d310802 into symfony:7.2 Aug 13, 2024
5 of 10 checks passed
Comment on lines +131 to +136
if (36 === \strlen($uuid) && !($format & self::FORMAT_RFC_4122)) {
return false;
}

$uuid = self::transformToRfc4122($uuid, $format);

Copy link
Member

Choose a reason for hiding this comment

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

Hi @alexandre-daubois sorry to dig this up, but while 7.2 is not release..

I find this early condition non "optimal", as default case will be in fact $format = FORMAT_RFC_4122..

What if we did something like this here (before? instead?), avoiding the regexp if the string is not 36 chars long ?

    if (($format & self::FORMAT_RFC_4122) && 36 !== \strlen($uuid)) {
        return false;
    }

What do you think ?

Copy link
Member

Choose a reason for hiding this comment

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

(before line 131, i messed up my selection 😅 )

Copy link
Member Author

Choose a reason for hiding this comment

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

I did try a few things but it would require some refactoring because of how the control flow is currently. I'm not sure it would worth it and if performances would greatly improve 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Yep that is very true!

*
* @param int-mask-of<Uuid::FORMAT_*> $format
*
* @return non-empty-string
Copy link
Member

Choose a reason for hiding this comment

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

@alexandre-daubois this is currently not true, afaik ... should we enforce it (or remove it) ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, but more than that, it's a bit confusing. I proposed this update: #58089

@fabpot fabpot mentioned this pull request Oct 27, 2024
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.

[Uid] isValid fails when passing non Rfc4122 IDs
6 participants