Installation:
composer require ems-project/helpers
Add to composer.json under require-dev if using for PHPStan compliance only.
First Use Case: Replace loose PHP functions with type-safe alternatives. For example:
use Ems\Helpers\ArrayHelper;
// Before (loose)
$exists = isset($array['key']);
// After (type-safe)
$exists = ArrayHelper::has($array, 'key');
Where to Look First:
Ems\Helpers namespace for core helpers (e.g., ArrayHelper, StringHelper).phpstan.neon (if provided) for compliance rules.Type-Safe Array Operations:
// Safe access with default
$value = ArrayHelper::get($array, 'nested.key', 'default');
// Safe merge (avoids type warnings)
$merged = ArrayHelper::merge($array1, $array2);
String Manipulation:
// Slugify with custom rules
$slug = StringHelper::slugify('Hello World!', '-', ['!']);
// Pluralize/singularize
$plural = StringHelper::pluralize('user');
Collection Utilities:
// Group by with type hints
$grouped = CollectionHelper::groupBy($items, fn($item) => $item->category);
// Filter with callbacks
$filtered = CollectionHelper::filter($items, fn($item) => $item->active);
Integration with Laravel:
$this->app->bind('arrayHelper', fn() => new ArrayHelper());
Helper) to wrap multiple helpers:
class HelperFacade extends Facade {
protected static function getFacadeAccessor() { return 'helpers'; }
}
Custom Helpers:
Extend existing helpers or create new ones by implementing the HelperInterface:
class MyHelper extends AbstractHelper {
public function customMethod(mixed $input): string {
return (string) $input;
}
}
PHPStan Overhead:
array_key_exists) may trigger errors.ArrayHelper::has()).Performance:
StringHelper::slugify).Namespace Collisions:
use Ems\Helpers\ArrayHelper as EmsArrayHelper;
Deprecation:
7.0.0 may have BC breaks).PHPStan Errors:
phpstan analyse with --level=max to catch all compliance issues.@phpstan-ignore-next-line sparingly; prefer refactoring to wrappers.Dynamic Function Calls:
call_user_func() with helpers unless you’re certain about argument types.Custom Rules:
rules.neon to add custom rules for your helper methods:
includes:
- vendor/ems-project/helpers/phpstan.neon
Add New Helpers:
AbstractHelper pattern. Example:
class DateHelper extends AbstractHelper {
public function parse(string $date, string $format = 'Y-m-d'): ?DateTime {
return DateTime::createFromFormat($format, $date);
}
}
Override Defaults:
php artisan vendor:publish --tag=helpers-config
Testing:
Ems\Helpers\TestHelper for assertions:
TestHelper::assertArrayHas($array, ['key']);
Laravel-Specific:
Str, Arr, or Collection by creating proxy methods:
class LaravelHelper extends AbstractHelper {
public function laravelStr(string $input): string {
return Str::of($input)->snake()->value();
}
}
How can I help you explore Laravel packages today?