php / php/php-src

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

オープン
#18,445 コメント 8 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Feature Status: Needs Triage
主要言語
C
スター
40.4k
フォーク
8.2k
平均マージ
2日 13時間
マージ済み PR(30日)
96

説明

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?

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

提供された call_user_func の例から始め、php-src における既存の strict_types および callback-call の動作を追跡してください。この issue ではファイルもテストも指定されていません。完全なコントリビューションでは、まず RFC を通じて提案された strict_types のセマンティクスを確定し、その後、必要な実装とリグレッションカバレッジを特定します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
php
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
28/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。