php / php/php-src

Improve strict_types uptake by (optionally) ignoring if called from call_user_func,...

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

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

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

Mô tả

Description

To increase uptake and compatibility strict_types=1 currently already will only apply to function calls from within a file that declares it.

Most PHP applications provide filterables (e.g. WordPress' apply_filters for example), which calls callbacks using call_user_func(. Unfortunately, this means, that any 3rd party code can alter the filterable value/type, which then results in a fatal type error in our code.
Essentially, this resulted in lots of support tickets for us, caused by buggy 3rd party code - which wasn't our fault and we couldn't do anything about - except removing strict_types again from our own code.

https://3v4l.org/PMSFB#v8.4.6

<?php
declare(strict_types=1);

function bad_3rd_partycode( $p ) {
    return null;
}

function my_code( string $path ): string {
    if ( is_file( $path ) ) {
        unlink( $path );
    }
    
    return '';
}

$value = 'foo.log';
$value = call_user_func( 'bad_3rd_partycode', $value );
echo call_user_func( 'my_code', $value );

Fatal error: Uncaught TypeError: my_code(): Argument #1 ($path) must be of type string, null given

This, I assume, is one of the reasons, for the relatively low use of strict_types in non-standalone applications - essentially, you're being punished for someone else's mistakes.

I think it would make sense if either:

  1. there was a strict_types=2, that would be essentially like strict_types=0 if a function is called from call_user_func

Pro: explicit and no change for existing code
Con: not backwards compatible with older PHP versions, which means nobody will use it

  1. or strict_types=1 would by default ignore strict_types if a function is called from call_user_func (and there is a strict_types=2 added, to keep/enforce the current functionality of strict_types=1)

Pro: fully backwards compatible
Con: makes type checking weaker and requires change of strict_types=1 to strict_types=2 for those who do not want to allow weaker checks - which however, is something that takes 1 min to achieve (just search/replace all files in your code with a simple sed)

I assume this would require an RFC?

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

Bắt đầu với ví dụ call_user_func được cung cấp và lần theo hành vi strict_types và callback-call hiện có trong php-src; issue không nêu tên tệp hoặc test nào. Một contribution hoàn chỉnh trước hết sẽ xác định ngữ nghĩa strict_types được đề xuất thông qua một RFC, sau đó xác định phần triển khai và phạm vi bao phủ hồi quy cần thiết.

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
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
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
28/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.