-
-
Notifications
You must be signed in to change notification settings - Fork 7
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
Weird behavior of ControlStructures::getDeclareScopeOpenClose method #339
Comments
To be honest, this sounds like a bug which should be reported to PHP Core. The If PHP doesn't throw a parse error for those code samples you showed above, to me, that would mean that either the docs needs to be updated or a bug needs to be fixed in PHP. I'm not sure how the P.S.: nice find! |
Note: in the above code samples, I'm missing one, which I'd be interested to see the result of: <?php
declare(encoding='ISO-8859-15') {
echo 'aäaß' . "\n";
}
echo 'aäaß' . "\n"; |
☝🏻 This should help determine whether PHP respects the braces, but only if directly after the |
Based on this bit in the manual, I have a niggly feeling that a |
Oh... this is also a lovely comment... https://www.php.net/manual/en/control-structures.declare.php#124553
Never come across that before. guess I better fix the PHPCS upstream sniff for that as well.... |
This one also returns the aÀaÃ
aÀaà I'll open a bug report at php-src to see what the response will be. |
@dingo-d As this method is redundant now PHPCS 3.7.1 is the minimum supported PHPCS version, it has been deprecated in #347 (in favour of checking the With that in mind, can this issue be closed, as I don't think there is anything actionable for PHPCSUtils ? The only thing from the above thread which I think might be interesting for PHPCSUtils could be to offer a function to parse This could help standards which examine declare statements to handle multi-directive statements correctly. Then again, it may be better/more straight-forward to add What do you think ? If you think this would be a good idea, we should probably open a separate issue for that. |
We can close this issue. As for the parsing the declare statements, that could be useful, although currently i'm just checking string toke s inside the directive openers and closers, so it's not that extra of an overhead that would require a new utility method for 🤷♂️ I think reusing |
See: #378 |
When writing a sniff for detecting and allowing or disallowing brace usage in the
declare
construct I used theControlStructures::getDeclareScopeOpenClose
helper to detect ifdeclare
is using the braces or not.One of the cases I'm trying to block out early is the usage of
strict_types
directive with curly braces, as this is forbidden and will throw a fatal error.One code that I've tested is this one
At first, I thought I'd get a parse error if I put anything before the curly brace and the
declare
keyword, but I got theFatal error: strict_types declaration must not use block mode
instead.So I tried seeing if the same would be correct for
encoding
directive (as this one allows curly brace usage). I had to test this one locally because it requires the usage of the Zend multibyte extension which is off in the 3v4l.By default
when ran will just echo out these characters. Then I change the encoding
And I get
aÀaÃ
out. Then I wrap it in the braces to confirm it's working:And the result is like the previous one. Then I add an echo statement between the declare and the opening brace
And (to my surprise) I get:
Then I add this
And I get
So it turns out that, no matter how this construct is called it's going to be applied to the entire file. Regardless of the scope.
I'm not sure if this is by design or a bug.
But the issue is that when you have anything else that is not a comment between the declare statement and the opening brace, the
getDeclareScopeOpenClose
will then returnfalse
, and it will behave like there is no curly braces at all.This in turn makes it so that the original code in question:
Will return false for
getDeclareScopeOpenClose
, when it should be caught (as it will error out).Again, I'm not sure if this is intentional, or if I should just ignore the code like that when checking for the scope opener and closer for the
declare
construct 🤷🏼♂️The text was updated successfully, but these errors were encountered: