-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Conversation
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, '@')) { |
There was a problem hiding this comment.
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:
if (!preg_match('/^(?:([^:]*):([^:]*):)?(.+)\.([^\.]+)\.([^\.]+)$/', $name, $matches) || str_starts_with($name, '@')) { | |
if (str_starts_with($name, '@') || !preg_match('/^(?:([^:]*):([^:]*):)?(.+)\.([^\.]+)\.([^\.]+)$/', $name, $matches)) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 👍
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
79a37f3
to
2ee828b
Compare
There was a problem hiding this 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 😍
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). |
@fabpot hi,
according to https://packagist.org/packages/symfony/polyfill-php80/dependents?order_by=downloads, this "internal" package is required on major symfony packages already
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
I do not understand the thx |
That's a typo, I meant 8.0 |
@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. So to me there is no downside here and it makes the code future-proof. Please reconsider. |
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. |
We need to get the rule merged into CS Fixer then. 😓 |
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>
2ee828b
to
e585b26
Compare
Thank you @derrabus. |
@@ -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)) { |
There was a problem hiding this comment.
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
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
…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
Linking to composer/composer#10024 for references. |
…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
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
I'd like to use
str_contains()
andstr_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 thestrpos()
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.