-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix twig_test_empty and twig_length_filter to not trigger autoloading #2438
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
@@ -1241,7 +1241,7 @@ function twig_test_empty($value) | |||
return 0 == count($value); | |||
} | |||
|
|||
if (method_exists($value, '__toString')) { | |||
if (is_object($value) && method_exists($value, '__toString')) { |
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.
can be optimized, as the is_object
test is now always done there is not point for the test at line 1280 (return '' === $value || false === $value || null === $value || array() === $value;
) if is_object
returned true
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.
You mean like this?
if (is_object($value)) {
if (method_exists($value, '__toString')) {
return '' === (string) $value;
} else {
return false;
}
}
return '' === $value || false === $value || null === $value || array() === $value;
Should I use ternary operator?
if (is_object($value)) {
return method_exists($value, '__toString') ? '' === (string) $value : false;
}
return '' === $value || false === $value || null === $value || array() === $value;
@mpdude Right... twig_length_filter doesn't need to be changed because of is_scalar check. So the only thing left is the optimization that @SpacePossum suggested. |
the optimize might not be worth a PR I guess |
Alright. In that case I'll just wait for the 1.x branch to be merged into 2.x. Thank you both. |
This PR was submitted for the 2.8 branch but it was merged into the 2.7 branch instead (closes #22307). Discussion ---------- [Debug] Fix php notice | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Of course autoloading of an empty string should not actually happen (fixed that in twigphp/Twig#2438) but if it does happen it should not throw a php notice. ``` Notice: Uninitialized string offset 0 ``` Commits ------- e333a1a [Debug] Fix php notice
This PR was submitted for the 2.8 branch but it was merged into the 2.7 branch instead (closes #22307). Discussion ---------- [Debug] Fix php notice | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Of course autoloading of an empty string should not actually happen (fixed that in twigphp/Twig#2438) but if it does happen it should not throw a php notice. ``` Notice: Uninitialized string offset 0 ``` Commits ------- e333a1a [Debug] Fix php notice
Introduced in #2420, partially fixed in #2433.
cc @mpdude
Also because of this I found another bug in both Symfony and Composer - autoloading fails for an empty string. I'll send pull requests to both shortly.