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

Implement Stringable interface on Enum #160

Merged
merged 1 commit into from
Aug 4, 2022
Merged

Implement Stringable interface on Enum #160

merged 1 commit into from
Aug 4, 2022

Conversation

jdecool
Copy link
Contributor

@jdecool jdecool commented Apr 16, 2022

Enum class has a __toString, so it would be great to implements the new Stringable interface.

As the interface is not available for php < 8, I've had the symfony/polyfill-php-80 implemented a polyfill.

@mnapoli
Copy link
Member

mnapoli commented Apr 18, 2022

Hey hey, thanks!

I'd love to skip requiring the polyfill: this package is used everywhere, and polyfills add extra (often useless) code to download and packaged applications.

I'm torn between:

  • we shouldn't do anything
  • or we should do a new release with a PHP 8 requirement, but since this is a highly used library this could cause issues for a lot of people

How is the Stringable useful in practice?

@jdecool
Copy link
Contributor Author

jdecool commented Apr 18, 2022

I'm torn between:

  • we shouldn't do anything
  • or we should do a new release with a PHP 8 requirement, but since this is a highly used library this could cause issues for a lot of people

Maybe another possibility is to implement the polyfill directly in the library, it's just a single interface with a classmap definition for the autoloading.

How is the Stringable useful in practice?

In a project I work on, I use a library which types a lot of arguments method with the Stringable interface.
So I need to implement the interface on each enums. As they already implement the __toString method, I thought it might be a good idea to do it right here in the library. Don't you think ?

@mnapoli
Copy link
Member

mnapoli commented Apr 18, 2022

👍 I'm actually +1 on re-implementing the polyfill interface. That's usually stuff I don't want to maintain, but here given the impact of this project I'd rather not take any risk.

@jdecool
Copy link
Contributor Author

jdecool commented Apr 18, 2022

I've just updated the PR

@willemstuursma
Copy link
Contributor

LGTM

@jdecool
Copy link
Contributor Author

jdecool commented May 16, 2022

Hi @mnapoli, do you have an opinion on this PR ?

Just to know, if I should closed it or not :)

@@ -0,0 +1,11 @@
<?php

if (\PHP_VERSION_ID < 80000 && !interface_exists('Stringable')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

symfony/polyfill-php80 has no if condition there: https://github.com/symfony/polyfill-php80/blob/main/Resources/stubs/Stringable.php
Is there a way to control what would composer use in the classmap for this interface as a file? I guess it should be only 1 file and it doesn't matter which one of them will get here if both would be available.
Do you see a issue where symfony might be included after this one and having no if condition it would fail?

Copy link
Member

Choose a reason for hiding this comment

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

Oh good point 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't matter which one is used as both polyfill "emulate" the same interface.

The classmap autoloads will result in an associative array which associate a class with a file which implement this one.
From the Composer autoload view, only one file will be loaded.

@mnapoli
Copy link
Member

mnapoli commented Aug 4, 2022

Sorry for the delay, I lost track of it. Let's do this.

Thanks @jdecool

@mnapoli mnapoli merged commit a867478 into myclabs:master Aug 4, 2022
@jdecool jdecool deleted the stringable-implementation branch August 4, 2022 10:14
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.

None yet

4 participants