Skip to content

Leverage str_contains/str_starts_with #41576

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
Jul 21, 2021

Conversation

derrabus
Copy link
Member

@derrabus derrabus commented Jun 6, 2021

Q A
Branch? 4.4
Bug fix? no
New feature? no
Deprecations? no
Tickets N/A
License MIT
Doc PR N/A

I'd like to use str_contains() and str_starts_with() whenever possible. On PHP 8, this is a native function and for all earlier versions, we can maintain the most efficient way to perform those operations in the polyfill package. And apart from that, I find the new functions more intuitive than the strpos() expressions I'm replacing here.

All code changes in this PR were automated, see PHP-CS-Fixer/PHP-CS-Fixer#5754

Of course, this is more than just a CS change. If you don't feel comfortable merging this change into 4.4, I can easily redo the PR for 5.4.

@carsonbot
Copy link

Hey!

I think @fancyweb has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

throw new \RuntimeException(sprintf('Template name "%s" contains invalid characters.', $name));
}

if (!preg_match('/^(?:([^:]*):([^:]*):)?(.+)\.([^\.]+)\.([^\.]+)$/', $name, $matches) || 0 === strpos($name, '@')) {
if (!preg_match('/^(?:([^:]*):([^:]*):)?(.+)\.([^\.]+)\.([^\.]+)$/', $name, $matches) || str_starts_with($name, '@')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not directly related and maybe micro optimisation, but wouldn't it be faster to do:

Suggested change
if (!preg_match('/^(?:([^:]*):([^:]*):)?(.+)\.([^\.]+)\.([^\.]+)$/', $name, $matches) || str_starts_with($name, '@')) {
if (str_starts_with($name, '@') || !preg_match('/^(?:([^:]*):([^:]*):)?(.+)\.([^\.]+)\.([^\.]+)$/', $name, $matches)) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, probably. But I'd like to keep the change list straightforward and not include additional manual changes for now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes definitely not part of this PR 👍

nicolas-grekas added a commit that referenced this pull request Jun 8, 2021
This PR was merged into the 4.4 branch.

Discussion
----------

[FrameworkBundle] Remove duplicate catch block

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

Spottet while reviewing #41576 by `@derrabus`

Commits
-------

32cac1c Remove duplicate catch block
@derrabus derrabus force-pushed the improvement/str-contains branch from 79a37f3 to 2ee828b Compare June 8, 2021 20:19
Copy link
Member

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

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

A big 👍 from me ... new code is so much more readable 😍

@fabpot
Copy link
Member

fabpot commented Jul 6, 2021

Even if I appreciate the effort in modernizing the code base, I don't see any benefits of such a change. The code is not really different and it introduces a new polyfill as a dep (which might make the code slower).
Pondering the pros and the cons, I'm 👎 for such a change until we require 8.1+.

@noniagriconomie
Copy link
Contributor

@fabpot hi,

The code is not really different and it introduces a new polyfill as a dep

according to https://packagist.org/packages/symfony/polyfill-php80/dependents?order_by=downloads, this "internal" package is required on major symfony packages already

(which might make the code slower)

this indeed can be the main reason, when running on sf v4 or v5 without php v8n thus leveraging the polyfill, but I can not tell if it has an impact or not :s

until we require 8.1+.

I do not understand the 8.1+, sf v6 which require php v8.0+ could be a good candidate no?

thx

@fabpot
Copy link
Member

fabpot commented Jul 6, 2021

until we require 8.1+.

I do not understand the 8.1+, sf v6 which require php v8.0+ could be a good candidate no?

That's a typo, I meant 8.0

@Tobion
Copy link
Contributor

Tobion commented Jul 6, 2021

@fabpot as already said by @noniagriconomie, the php80 polyfill is already a requirement in 4.4 by major symfony components like httpkernel and console. So it's already part of every symfony app. So there is no additional dependency.

I'd also argue it really makes the code mroe readable and it clarifies the intent. There are several workarounds implementations, some better suited than others (see rfc). By using the standard functions it also normalizes those inconsistencies.

You said it might make the code slower. I'd say it makes it faster, especially on PHP 8.0+. One of the arguments for introducing the functions was that the alternatives are not efficient.
Another argument is that SF 4.4 will be maintained a year longer than PHP 7.4. So at one point symfony 4.4 users are likely to run on PHP 8. Then they will get the performance increase automatically.

So to me there is no downside here and it makes the code future-proof. Please reconsider.

@fabpot
Copy link
Member

fabpot commented Jul 6, 2021

ok, let's do it then, but we need to enable the rule on PHP CS fixer to be sure we will catch "old" usages in PRs.

@derrabus
Copy link
Member Author

derrabus commented Jul 6, 2021

We need to get the rule merged into CS Fixer then. 😓

nicolas-grekas added a commit that referenced this pull request Jul 21, 2021
This PR was merged into the 4.4 branch.

Discussion
----------

Leverage str_ends_with

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       |
| License       | MIT
| Doc PR        |

added the php80 polyfill to requirements when necessary. some components already had the requirement anyway.
Related to #41576

Commits
-------

9d80729 Leverage str_ends_with
Signed-off-by: Alexander M. Turek <me@derrabus.de>
@nicolas-grekas nicolas-grekas force-pushed the improvement/str-contains branch from 2ee828b to e585b26 Compare July 21, 2021 12:20
@nicolas-grekas
Copy link
Member

Thank you @derrabus.

@nicolas-grekas nicolas-grekas merged commit c7dc7f8 into symfony:4.4 Jul 21, 2021
@@ -166,7 +166,7 @@ public function add($messages, $domain = 'messages')
}
$intlDomain = $domain;
$suffixLength = \strlen(self::INTL_DOMAIN_SUFFIX);
if (\strlen($domain) < $suffixLength || false === strpos($domain, self::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
if (\strlen($domain) < $suffixLength || !str_contains($domain, self::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
Copy link
Member

Choose a reason for hiding this comment

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

this change is wrong, it might be good to report it to php-cs-fixer

@derrabus derrabus deleted the improvement/str-contains branch July 21, 2021 16:25
fabpot added a commit that referenced this pull request Jul 29, 2021
This PR was merged into the 4.4 branch.

Discussion
----------

[Dotenv][Yaml] Remove PHP 8.0 polyfill

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #42280
| License       | MIT
| Doc PR        | N/A

This is a partial revert of #41576 and #41973.

Commits
-------

08ecbf5 Remove polyfills from Yaml and Dotenv
fabpot added a commit that referenced this pull request Aug 13, 2021
…ndrik Luup)

This PR was merged into the 4.4 branch.

Discussion
----------

Do not use str_starts_with in translation-status.php

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | N/A
| License       | MIT
| Doc PR        | N/A

`translation-status.php` is a standalone script that might be run with PHP version that is less than <8.0 where `str_starts_with` is not available. I propose that we revert the change that was made in #41576 instead of trying to load the polyfill somehow.

Commits
-------

94d843f Do not use str_start_with
@nicolas-grekas
Copy link
Member

Linking to composer/composer#10024 for references.

nicolas-grekas added a commit that referenced this pull request Aug 19, 2021
…lmann)

This PR was submitted for the 5.3 branch but it was merged into the 4.4 branch instead.

Discussion
----------

[ExpressionLanguage] [Lexer] Remove PHP 8.0 polyfill

| Q             | A
| ------------- | ---
| Branch       | 5.3
| Bug fix      | yes
| New feature  | no
| Deprecations | no
| Tickets       | Fix #42280
| License       | MIT
| Doc PR        | N/A

This is a partial revert of #41576 and is a followup to #42296

Commits
-------

d2f39e9 Remove polyfills from ExpressionLanguage
hultberg pushed a commit to hultberg/symfony that referenced this pull request Sep 17, 2021
This PR was merged into the 4.4 branch.

Discussion
----------

Leverage str_ends_with

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       |
| License       | MIT
| Doc PR        |

added the php80 polyfill to requirements when necessary. some components already had the requirement anyway.
Related to symfony#41576

Commits
-------

9d80729 Leverage str_ends_with
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.

8 participants