declare statement is not scoped when using curly braces
还没有人认领这个 Issue。
- 主要语言
- C
- 星标
- 40.4k
- 派生
- 8.2k
- 平均合并
- 2 天 13 小时
- 30 天内合并 PR
- 96
描述
Description
When writing tests for phpcs sniff using some useful helpers 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
declare(strict_types=1)
echo 'hi!';
{
// Code.
}
that threw a fatal error 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
declare(encoding='ISO-8859-15')
echo 'aäaß' . "\n";
{
echo 'aäaß' . "\n";
}
And I got out:
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
declare(encoding='ISO-8859-15') {
echo 'aäaß' . "\n";
}
echo 'aäaß' . "\n";
I expected to see
aÀaÃ
aäaß
But I got (again)
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)
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
该 issue 没有指明源文件或测试入口点。首先复现涉及 declare(encoding=...) 和花括号的 PHP 7.4.26 示例,然后跟踪 PHP 解释器中对 declare 的处理。完成的标准是该指令的作用域限定在花括号括起的语句上,并且报告的行为由回归测试覆盖。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- php
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100