dontdrinkandroot/common
A small utility library with commonly used PHP helpers and extensions, including a PHPStan extension. Intended to provide shared building blocks for projects, with CI and code quality tooling support via SonarCloud badges.
Installation Add the package via Composer:
composer require dontdrinkandroot/common
Register the service provider in config/app.php:
'providers' => [
// ...
DontDrinkAndRoot\Common\CommonServiceProvider::class,
],
First Use Case
Use the Helper facade for quick utilities:
use DontDrinkAndRoot\Common\Facades\Helper;
// Example: Generate a UUID
$uuid = Helper::uuid();
Key Entry Points
Helper (for most utilities)HasCommon (for reusable methods in classes)dontdrinkandroot\common\helpers\* (if not exposed via facade)Data Validation & Sanitization
Use Helper::sanitizeInput() or Helper::validateEmail() in controllers/forms:
$cleanedInput = Helper::sanitizeInput($request->all(), ['field' => 'string']);
File & Path Handling
Leverage Helper::path() or Helper::fileExists() in service classes:
if (Helper::fileExists($filePath)) {
$content = Helper::fileGetContents($filePath);
}
Date/Time Utilities Format dates consistently:
$formattedDate = Helper::formatDate($date, 'Y-m-d H:i:s');
Array Manipulation
Use Helper::arrayPluck() or Helper::arrayMergeRecursive():
$plucked = Helper::arrayPluck($collection, 'key');
Security Helpers Hash passwords or generate secure tokens:
$hashed = Helper::hash('password');
$token = Helper::generateToken();
Laravel Blade: Extend Blade directives for reusable components:
Blade::directive('uuid', function () {
return "<?php echo DontDrinkAndRoot\Common\Facades\Helper::uuid(); ?>";
});
Usage: @uuid in views.
Service Layer: Inject Helper into services for reusable logic:
public function __construct(private Helper $helper) {}
Middleware: Use Helper::ip() or Helper::userAgent() for request metadata:
$ip = Helper::ip();
Facade vs. Direct Calls
Helper facade. Check the helpers directory for standalone functions.dontdrinkandroot\common\helpers\StringHelper::camelCase() may not be facaded.Namespace Conflicts
dontdrinkandroot\common namespace. Ensure no naming collisions with your project’s classes.Configuration Overrides
Helper::uuid()) may rely on default configurations. Override via:
config(['common.uuid_version' => 4]);
Deprecation Warnings
Log Helper Outputs
Use Helper::log() for debugging:
Helper::log('Debug message', 'debug');
Check for Null Returns
Some helpers (e.g., Helper::fileGetContents()) return false on failure. Validate:
$content = Helper::fileGetContents($path);
if ($content === false) {
// Handle error
}
Custom Helpers
Extend the Helper facade by publishing config:
php artisan vendor:publish --provider="DontDrinkAndRoot\Common\CommonServiceProvider" --tag="config"
Add your methods to config/common.php under helpers.
Traits for Reusability
Use HasCommon trait in models/services:
use DontDrinkAndRoot\Common\Traits\HasCommon;
class MyModel {
use HasCommon;
// Now has access to all helper methods
}
Override Default Behavior Bind custom implementations to the container:
$this->app->bind('dontdrinkandroot\common\helpers\StringHelper', function () {
return new CustomStringHelper();
});
$data = Cache::remember('helper_key', 60, function () {
return Helper::expensiveOperation();
});
How can I help you explore Laravel packages today?