kozmaoliver / native-methods-fixer
A fun package that normalizes PHP's inconsistent native function parameter orders
Package info
github.com/kozmaoliver/php-native-methods-fixer
pkg:composer/kozmaoliver/native-methods-fixer
v1.0.1
2026-05-21 12:33 UTC
Requires
- php: >=8.2
Requires (Dev)
- phpunit/phpunit: ^11.0
README
Just for fun, I don't want to offend anyone. Enjoy!
A whimsical PHP package that fixes the inconsistent parameter ordering in PHP's native functions by providing wrapper functions with consistent signatures.
The Problem
PHP's native functions have inconsistent parameter ordering:
// Needle/haystack are swapped between array and string functions in_array($needle, $haystack) strpos($haystack, $needle) // Callback position varies array_map($callback, $array) array_filter($array, $callback) // Subject position in replacement functions preg_replace($pattern, $replacement, $subject) str_replace($search, $replace, $subject)
The Solution
This package provides fixed_* wrapper functions that consistently place the subject/haystack as the first parameter:
use function NativeMethodsFixer\fixed_in_array; use function NativeMethodsFixer\fixed_strpos; use function NativeMethodsFixer\fixed_array_map; use function NativeMethodsFixer\fixed_preg_replace; // All search functions: haystack first, needle second fixed_in_array($haystack, $needle) fixed_strpos($haystack, $needle) // All array functions: array first, callback second fixed_array_map($array, $callback) fixed_array_filter($array, $callback) // All replacement functions: subject first fixed_preg_replace($subject, $pattern, $replacement) fixed_str_replace($subject, $search, $replace)
Installation
composer require kozmaoliver/native-methods-fixer
Usage
The functions are automatically available after Composer autoload:
<?php require 'vendor/autoload.php'; use function NativeMethodsFixer\fixed_in_array; use function NativeMethodsFixer\fixed_strpos; $fruits = ['apple', 'banana', 'orange']; if (fixed_in_array($fruits, 'banana')) { echo "Found banana!"; } $text = 'Hello, World!'; $pos = fixed_strpos($text, 'World');
Available Functions
Search Functions (haystack, needle)
fixed_in_array()- Search for value in arrayfixed_array_search()- Search for value and return keyfixed_strpos()- Find position of substringfixed_strrpos()- Find last position of substringfixed_strstr()- Find first occurrence of stringfixed_stristr()- Case-insensitive strstrfixed_strrchr()- Find last occurrence of characterfixed_substr_count()- Count substring occurrencesfixed_str_contains()- Check if string contains substringfixed_str_starts_with()- Check if string starts with substringfixed_str_ends_with()- Check if string ends with substring
Array Functions (array, callback)
fixed_array_map()- Apply callback to array elementsfixed_array_filter()- Filter array with callbackfixed_array_reduce()- Reduce array to single valuefixed_array_walk()- Apply function to every element
Regex/Replace Functions (subject, pattern/search, ...)
fixed_preg_match()- Perform regex matchfixed_preg_match_all()- Perform global regex matchfixed_preg_replace()- Perform regex search and replacefixed_preg_filter()- Perform regex replace and filterfixed_str_replace()- Replace all occurrencesfixed_str_ireplace()- Case-insensitive replacefixed_substr_replace()- Replace part of string
Requirements
- PHP 8.2 or higher
License
MIT