Skip to content

[Intl][5.0] Add parameters type-hints #32525

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 8, 2019
Merged

[Intl][5.0] Add parameters type-hints #32525

merged 1 commit into from
Aug 8, 2019

Conversation

tigitz
Copy link
Contributor

@tigitz tigitz commented Jul 12, 2019

Q A
Branch? master
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets continuation of #24722 and checks for #32179
License MIT
Doc PR N/A

This PR replaces docblocks by type hints in the Intl component considering #32179. Some docblocks without valuable information got also removed.

@@ -384,7 +384,6 @@ public function formatFractionDigitsProvider()
[1.123, '1.1', 1, 1],
[1.123, '1.12', 2, 2],
[1.123, '1.123', -1, 0],
[1.123, '1', 'abc', 0],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

'abc' is passed as the value of the setAttribute method here:

https://github.com/symfony/symfony/pull/32525/files#diff-2a5ae15ec68c5d666ce08a09d75690efR564

It's type-hinted at int so it throws a TypeError, however method's logic seems to handle case where it could be a string:

* cast to Boolean and then to int again. This way, negative values are converted to 1 and string values to 0.

I decided to keep the type hint as int and remove the use case of a string value

@tigitz
Copy link
Contributor Author

tigitz commented Jul 13, 2019

I decided at first to skip type-hints on methods throwing MethodNotImplementedException anyway.

But since #32525 (review) was asked I've decided to be consistent through the rest of the codebase:
61576fa

@xabbuh xabbuh added this to the 5.0 milestone Jul 13, 2019
@tigitz tigitz mentioned this pull request Jul 22, 2019
57 tasks
* @param string $currency The 3-letter ISO 4217 currency code indicating the currency to use
*
* @return string The formatted currency value
*
* @see http://www.php.net/manual/en/numberformatter.formatcurrency.php
* @see https://en.wikipedia.org/wiki/ISO_4217#Active_codes
*/
public function formatCurrency($value, $currency)
public function formatCurrency(float $value, string $currency)
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 sure if we should actually force float here. Storing a decimal value as string is a valid pattern, especially when it comes to currency amounts.

Copy link
Contributor Author

@tigitz tigitz Aug 3, 2019

Choose a reason for hiding this comment

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

AFAIU, this NumberFormatter implementation is a sort of polyfill for Intl::NumberFormatter where $value is typed as float and generates the following error if passed as a string with strict types:
Fatal error: Uncaught TypeError: NumberFormatter::formatCurrency() expects parameter 1 to be float, string given in

So question is, should we accept a wider range of type compared to the implementation it's trying to polyfill ?

Personally I would say no, I would expect this function to also throws a TypeError on string arg if the whole point of this class is to be as close as the "real" one.

I'll leave it type-hinted as a float for now, open to modification if team says otherwise.

Copy link
Member

Choose a reason for hiding this comment

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

All right, thanks for checking.

@fabpot
Copy link
Member

fabpot commented Jul 27, 2019

@tigitz Can you have a look at the remaining comment here? Thank you.

@tigitz
Copy link
Contributor Author

tigitz commented Jul 28, 2019

@fabpot yep will do tonight 👌

@tigitz
Copy link
Contributor Author

tigitz commented Aug 3, 2019

Status: Needs Review

@derrabus When you have time ;)

@Tobion
Copy link
Contributor

Tobion commented Aug 4, 2019

It seems almost all classes in intl which are affected here are internal classes, e.g. for polyfill that are not meant to be extended. So we can add type hints in 4.4 to them because we don't need parameter variance from 7.2. What do you think @nicolas-grekas ?
And we could add return types as well without having to worry.

@nicolas-grekas
Copy link
Member

@Tobion I was about to answer yes, but ./src/Symfony/Component/Intl/Resources/stubs/Collator.php contains a non-internal class, which extends the internal Collator. As such we cannot do more in 4.4.

nicolas-grekas added a commit that referenced this pull request Aug 8, 2019
…y of internal class (Tobion)

This PR was merged into the 3.4 branch.

Discussion
----------

[Intl] fix nullable phpdocs and useless method visibility of internal class

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets |
| License       | MIT
| Doc PR        |

Fix stuff found in #32525

Commits
-------

63b71b5 [Intl] fix nullable phpdocs and useless method visibility of internal class
Copy link
Contributor

@Tobion Tobion left a comment

Choose a reason for hiding this comment

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

Rebased

@nicolas-grekas
Copy link
Member

Thank you @tigitz.

@nicolas-grekas nicolas-grekas merged commit ce79f4b into symfony:master Aug 8, 2019
nicolas-grekas added a commit that referenced this pull request Aug 8, 2019
…, Tobion)

This PR was squashed before being merged into the 5.0-dev branch (closes #32525).

Discussion
----------

[Intl][5.0] Add parameters type-hints

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | continuation of #24722 and checks for  #32179
| License       | MIT
| Doc PR        | N/A

This PR replaces docblocks by type hints in the Intl component considering #32179. Some docblocks without valuable information got also removed.

Commits
-------

ce79f4b [Intl][5.0] Add parameters type-hints
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.

9 participants