[php-src] Issue #9446: declare statement is not scoped when using curly braces
| From: | dingo-d | Date: | Mon, 29 Aug 2022 08:50:03 +0000 |
| Subject: | [php-src] Issue #9446: declare statement is not scoped when using curly braces | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-242328@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/9446
Author: dingo-d
### Description
When writing tests for phpcs sniff using [some useful
helpers](https://github.com/PHPCSStandards/PHPCSUtils/issues/339) regarding
declare
statements, I came up with an unusual behavior of the declare statements, when braces
are used.
I was testing out a code that looked like this:
```php
<?php
declare(strict_types=1)
echo 'hi!';
{
// Code.
}
```
that threw a [fatal error](https://3v4l.org/9WLmh) because you cannot use curly braces with
strict_types directive or alternative control structure syntax (declare: ...
enddeclare;)
So I tried to see what will happen if I use a directive that allows the use of curly braces, like
encoding. So I tried:
```php
<?php
declare(encoding='ISO-8859-15')
echo 'aäaß' . "\n";
{
echo 'aäaß' . "\n";
}
```
And I got out:
```bash
aÀaÃ
aÀaÃ
```
I thought I'd get a parse error like when you omit the : when using
if with endif; statement. Although, when testing the if
statement with the curly braces, like in the example above, I can also see the echoed value before
the condition, so I guess that's kinda expected.
What was a buggy behavior was this:
```php
<?php
declare(encoding='ISO-8859-15') {
echo 'aäaß' . "\n";
}
echo 'aäaß' . "\n";
```
I expected to see
```bash
aÀaÃ
aäaß
```
But I got (again)
```bash
aÀaÃ
aÀaÃ
```
So it seems that the declare statement, no matter if we are using curly braces, or
alternative control structure syntax, will be applied throughout the file, instead of being scoped.
### PHP Version
PHP 7.4.26
### Operating System
Ubuntu 20.04 (on WSL)