feat: diff を追加する - #78
Open
Javakky wants to merge 1 commit into
Open
Conversation
array_diff のラッパーとして、input のうち other に含まれない値だけを残す関数を追加する。 array_diff は要素を文字列化して比較するため、本ライブラリの型安全志向に合わせて in_array の厳密比較(===)モードで判定するよう意図的に変更している。 Closes #15
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要(What / Why)
$otherに含まれない値だけを残すdiffを追加します。変更点
src/diff.php— 厳密比較 (===) で判定。ASSOC はキー保持、LIST は連番へ再付番tests/DiffTest.php— 13 ケースcomposer.jsonのautoload.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 testsOK (110 tests, 116 assertions)/ PHPStan level 10[OK] No errors/ CS FixerFixed 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 で相談させてください。その他
diff(multiset 差) ではなくarray_diffと同じ集合差です (重複する値はまとめて除去)。phpdoc に明記@phpstan-paramの条件型は付けていません (「assoc 入力 +MODE_LIST」が型エラーになるため。slice.php/map.php/intersect.phpと同じ形)Closes #15
🤖 Generated with Claude Code