php / php/php-src

declare statement is not scoped when using curly braces

Đang mở
#9,446 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Bug Category: Engine Status: Needs Triage
Ngôn ngữ chính
C
Star
40.4k
Fork
8.2k
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
96

Mô tả

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)

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Issue không nêu tệp mã nguồn hoặc điểm vào của bài kiểm thử. Hãy bắt đầu bằng cách tái hiện các ví dụ PHP 7.4.26 liên quan đến declare(encoding=...) và dấu ngoặc nhọn, sau đó lần theo cách xử lý declare trong trình thông dịch PHP. Được xem là hoàn tất khi chỉ thị này có phạm vi áp dụng cho câu lệnh trong dấu ngoặc nhọn và hành vi được báo cáo được bao phủ bởi một bài kiểm thử hồi quy.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
php
Lĩnh vực
compilers
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.