Skip to content

[Validator] clarify stringable type annotations #36057

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
Mar 13, 2020

Conversation

nicolas-grekas
Copy link
Member

Q A
Branch? 3.4
Bug fix? no
New feature? no
Deprecations? no
Tickets -
License MIT
Doc PR -

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Mar 13, 2020
@nicolas-grekas nicolas-grekas merged commit cbb9d01 into symfony:3.4 Mar 13, 2020
@nicolas-grekas nicolas-grekas deleted the stringable-only branch March 13, 2020 10:27
@DavidBadura
Copy link
Contributor

In our projects psalm complains that the \Stringable class has not been seen. We fixed it by installing the PHP80 polyfill. Shouldn't you add it as a composer require when using it?

@nicolas-grekas
Copy link
Member Author

I considered that but that's not a requirement of the code, so it's not needed. If it's a requirement for you because of the tooling you use, this is legit and then you should add the polyfill.

@DavidBadura
Copy link
Contributor

Yes, I was also unsure, but you are right. The code doesn't need it. 👍

@gsdevme
Copy link
Contributor

gsdevme commented May 12, 2020

Having these methods return \Stringable or string makes things a little difficult in userland right now though because, in PHP7.1, PHP7.2, PHP7.3, PHP7.4 you cannot type it.. the RFC didn't include a special type and furthermore there isn't anything in the polyfill.

This seems to force all existing userland code that consumed any of these methods to remove string in favour of mixed. Granted this is mostly caused by static analyzers reading docblocks as gospel and the use of strict_types.

The userland flow basically is

  1. Symfony (Via docblock) said it's returning a string
  2. You type string and strict_types=1 (happy because you have a real type)
  3. Symfony change to string|\Stringable (docblock)
  4. Because you can't union type yet, you have to remove your real type and use a docblock of string|\Stringer
  5. PHP 8 is released
  6. You can revisit that code that update the docblock for a real union type

Is this the migration path for this? the polyfill while it fast-forwards the feature doesnt give the full benefit because we dont have union types :(

@gsdevme
Copy link
Contributor

gsdevme commented May 12, 2020

further to my comments, I can see some of this discussion did happen on the RFC

A virtual "stringable" type (that would be similar to "iterable)" would,
IMHO, be of a bigger benefit if it is to denote the fact that "a variable
can be transformed to a string".

As it is presented, Stringable alone seems of very little use as compared to union like "string|Stringable", so using the polyfill in a version of PHP that does not support union does not seem to make sense.

I guess my userland code is living through this scenario right now as described by Patrick ALLAERT

https://externals.io/message/108539#108545

@nicolas-grekas
Copy link
Member Author

nicolas-grekas commented May 12, 2020

Symfony change to string|\Stringable (docblock)

This has always been string|object with a comment telling "stringable object",
which means there is nothing new here, PHP7.* cannot use any native types before PHP8. stringable vs string|Stringable would also make no difference in this regard since both would be shipped by PHP8. PHP7.* can now use string|Stringable to accurately tell this. That's much better than "string|object with a comment".

@gsdevme
Copy link
Contributor

gsdevme commented May 12, 2020

This has always been string|object with a comment telling "stringable object",

I'm not totally sure I follow? I don't see string|object, its explicitly saying string (and string only) are you saying implicitly it was string|object?

Screenshot 2020-05-12 at 14 02 25

In regard to the userland problem steps 1-6, is that the expectation here? I can see some of this discussion bleeds into the RFC but from consumption of this interface, it seems that is the expected thing here. That is to bounce between typing string then no type then php8's union type?

@nicolas-grekas
Copy link
Member Author

That specific PR fixes wrong annotations you're right. They should have been using string|object in the first place.

See #31083 for background on this specific issue/PR.

See e.g. #36056 for what I had in mind.

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.

4 participants