Autowiring: resolve union and intersection parameter types in DiContainer

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

メンテナーはふだん 1 日以内に返信

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

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
68/100
issue の種類
機能追加
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
php
領域
backend

調査の方向性

Start by reading resolveParameter() in src/Di/DiContainer.php and note where it only handles ReflectionNamedType; also read autowire() since it shares that path. Add the new named constructor and message template in src/Di/Enums/ExceptionMessages.php, then write failing cases in tests/Unit/Di/ for each scenario listed in Acceptance. Done means those tests pass, ambiguous unions are not guessed, unsatisfied intersections are rejected, and CHANGELOG.md records the behavior change.

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

説明

di enhancement

Problem

Quantum\Di\DiContainer::resolveParameter() reads a parameter type only when it is a ReflectionNamedType. For a union (A|B) or an intersection (Reader&Writer) type the type is ignored, so the parameter falls through to the passed arguments, then the default value, then null.

Result (checked on PHP 8.1):

  • public function __construct(A|B $dep) fails with a TypeError that gives no hint about the container.
  • public function __construct(A|B|null $dep) silently receives null, even when A is registered in the container.
  • Reader&Writer is never resolved, even when an implementing class is registered.

Proposal

Resolve a union or intersection parameter only when the choice is unambiguous. Never guess from the declaration order and never pass null silently.

  1. Union: drop null and builtin members. If exactly one remaining member can be resolved (it is registered in the container, or it is an instantiable class), use it. If several can, do not pick one.
  2. Intersection: use a registered or instantiable member and check that the result satisfies every part of the type (instanceof for each). A result that does not satisfy all parts is not used.
  3. Otherwise: keep the existing order (passed argument, default value). If neither exists, throw a DiException with a clear message (new named constructor and message template in src/Di/Enums/ExceptionMessages.php) that names the class, the parameter and the type, instead of a TypeError or a silent null.
  4. The same rules apply to autowire() of a closure, which shares resolveParameter().

Why not the order of declaration

Other containers differ: one rejects union types (Marko, a clear exception), one tries members in declaration order (Tempest, which also does not check that an intersection is satisfied as a whole), one tries each branch and verifies it (Canvas). Declaration order makes A|B and B|A behave differently, so it is not used here.

Acceptance

  • Tests in tests/Unit/Di/ for: union with one registered member, union with none, union with several resolvable members, A|B|null with and without a default, intersection with a registered implementation that satisfies all parts, intersection that does not, closure autowiring.
  • CHANGELOG.md entry. A parameter that failed before with a TypeError now resolves or throws a DiException; the exception type changes, so it is noted as a behavior change.

Milestone 3.2.0 (a different exception type on a path that used to throw TypeError).

主要言語
PHP
スター
43
フォーク
22
平均マージ
54分
マージ済み PR(30日)
57

環境構築

はじめの一歩

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

quantum-php/framework のほかの issue

quantum-php/framework の issue をすべて見る

似ている issue

PHP の issue をもっと見る

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

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