Repository navigation
refactor: Replace magic methods with explicit methods split into feature traits - #1
Merged
Merged
Conversation
…ure traits Remove __call/__callStatic dispatch and declare every method (name, nameAs, nameFrom, nameFromAs) directly, grouped into traits by feature. Code stays compatible with PHP 7.1 (guards for empty variadic calls to array_* functions).
- Add ArrayUtils::toArray() so generators and other iterators are unpacked instead of being cast to an empty array - keyExists now uses array_key_exists (null values count as existing) - reduceRight passes the original keys to the callback - Call exchangeArray() directly in instance methods - Move ext-libxml/ext-simplexml to require-dev (only used by benchmarks) - Add PHPUnit test suite
This was referenced Oct 7, 2026
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.
Summary
Replaces the
__call/__callStaticmagic dispatch with explicitly declared methods, split into feature traits, and fixes a few correctness issues found along the way.Changes
name,nameAs,nameFrom,nameFromAs) is a real method. Traits are flattened into the class at compile time, so they cost nothing at runtime.src/kim/present/lib/arrayutils/traits/):ConcatTrait,SetTrait,IterationTrait,SearchTrait,SortTrait,StackTrait,ShapeTrait.first/last/keyFirst/keyLastno longer buildarray_keys(),includes/indexOfusein_array/array_search,sliceusesarray_slice, and instance methods callexchangeArray()directly.push(),diff(), ...) are guarded.ArrayUtils::toArray()); previously(array)turned them into[].composer test);ext-libxml/ext-simplexmlmoved torequire-dev(benchmarks only).Behavior changes
keyExistsusesarray_key_exists(anullvalue now counts as existing).reduceRightpasses the original keys to the callback (previously integer keys were renumbered).first/last/randomreturnnullon an empty array instead of raising a warning;fill/sliceaccept$end = nullas documented (was aTypeError);flatMapworks with string-keyed arrays.__call(e.g.everyAs(),__map()) no longer exist, and unknown methods raise PHP's native "undefined method" error instead ofBadMethodCallException.popFrom/shiftFromstill do not modify the given array (unchanged).Benchmark:
mainvs this branchThe repo's own PHPBench suite (
phpbench run, 1000-element arrays;arrayutilsvariant, mean per call), run sequentially on the same machine with PHP 8.3. Not run on PHP 7.x.56 benchmarks: 51 faster by 1.1x or more, 5 unchanged, none slower. Geometric mean speedup is 2.8x.
The
nativecolumn is the plain PHP function from the same benchmark, measured in themainrun, for reference. Sub-microsecond native values are close to the measurement floor, so compare the main and branch columns with each other.Verification
As,From,FromAs) of the old and new classes on 8 inputs and about 1,100 calls. Results and instance state matched, except for the intentional changes listed above.vendor/bin/phpunit: 8 tests, 38 assertions pass (PHP 8.3).