Skip to content

feat: diff を追加する - #78

Open
Javakky wants to merge 1 commit into
masterfrom
javakky/diff
Open

feat: diff を追加する#78
Javakky wants to merge 1 commit into
masterfrom
javakky/diff

Conversation

@Javakky

@Javakky Javakky commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

概要(What / Why)

$other に含まれない値だけを残す diff を追加します。

変更点

  • src/diff.php — 厳密比較 (===) で判定。ASSOC はキー保持、LIST は連番へ再付番
  • tests/DiffTest.php — 13 ケース
  • composer.jsonautoload.files に 1 件追加

動作確認

  • 手動:diff([0, 1, 2], ['0'])[0, 1, 2] (array_diff なら [1, 2])
  • 自動:
    • vendor/bin/php-cs-fixer fix --dry-run --diff / vendor/bin/phpstan analyse -c phpstan.neon / vendor/bin/phpunit tests
    • 結果:OK (110 tests, 116 assertions) / PHPStan level 10 [OK] No errors / CS Fixer Fixed 0 of 31 files

補足(任意)

array_diff に委譲していません

issue のタイトルは「array_diff のラッパー」ですが、意図的に外しました。array_diff は要素を文字列化して比較するため 0'0'1'1'true'1' を同一視し、配列やオブジェクトを含む入力では Array to string conversion が出ます。declare(strict_types=1) + PHPStan level 10 のこのライブラリでは厳密比較が妥当と判断しました。この差異は phpdoc に明記し、テストでも固定しています。

$other の値型を mixed にしています

intersect に揃えて @param list<V>|array<array-key, V> $other と書くと、diff([1,2,3], ['1','2']) の戻り型が list<1|2|3|'1'|'2'> と実際 ([1,2,3]) より広く推論されることを \PHPStan\dumpType() で確認したため、mixed にしました。diff は「型の違う $other を渡して、違うから残る」使い方が本命なので、V を共有させると型が壊れます。

既存の intersect との非対称

src/intersect.php は LIST 側が in_array($v, $other, false) (緩い比較)、ASSOC 側が array_intersect() (文字列化比較) で、同じ関数の中で 2 つの比較規則が使われています。今回の diff が厳密比較なので現状は非対称です。intersect 側を揃えるかは別 issue で相談させてください。

その他

  • Scala の diff (multiset 差) ではなく array_diff と同じ集合差です (重複する値はまとめて除去)。phpdoc に明記
  • @phpstan-param の条件型は付けていません (「assoc 入力 + MODE_LIST」が型エラーになるため。slice.php / map.php / intersect.php と同じ形)

Closes #15

🤖 Generated with Claude Code

array_diff のラッパーとして、input のうち other に含まれない値だけを残す関数を追加する。
array_diff は要素を文字列化して比較するため、本ライブラリの型安全志向に合わせて
in_array の厳密比較(===)モードで判定するよう意図的に変更している。

Closes #15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: array_diff のラッパー

1 participant