campanda/commons-ensure-bundle
assert(): Unlike PHP’s assert(), this is always enabled (no zend.assertions config), making it more reliable for production.Ensure class manually in Laravel’s service container (if needed).campanda\Commons\EnsureBundle vs. Laravel’s App\ namespace).Ensure::isNotNull() in test doubles).throw new \InvalidArgumentException().Validator facade) or packages like spatie/laravel-validation-extensions?
App\Exceptions\Handler)?
throw new \InvalidArgumentException($message) or customize EnsureException).assert() or custom validation logic?
assert().symfony/console), integration is seamless.App\Services\Ensure).laravel-ensurer (if available) or rolling a lightweight version.assert() usage and manual validation logic.UserService).assert() with Ensure::isNotNull(), etc., and log exceptions.Ensure class in composer.json:
"autoload": {
"psr-4": {
"App\\": "app/",
"Campanda\\": "vendor/campanda/commons-ensure-bundle"
}
}
app/Support/Ensure.php to avoid vendor namespace pollution.EnsureException to implement Laravel’s render() method in App\Exceptions\Handler:
public function render($request, Throwable $exception) {
if ($exception instanceof \Campanda\Commons\EnsureBundle\EnsureException) {
return response()->json(['error' => $exception->getMessage()], 400);
}
return parent::render($request, $exception);
}
Ensure class if using dependency injection:
$this->app->singleton(Ensure::class, function ($app) {
return new \Campanda\Commons\EnsureBundle\Ensure();
});
Ensure in PHPUnit tests to simulate failures:
$this->partialMock(Ensure::class, ['isNotEmpty'])
->expects($this->once())
->method('isNotEmpty')
->with($entityName)
->willThrowException(new \RuntimeException('Test failure'));
assert() calls with Ensure in internal services (non-HTTP).User::ensureValidEmail()).Ensure.EnsureException messages are verbose (good for dev), but may need sanitization for production (e.g., remove sensitive data from sprintf templates).vendor/campanda/ paths; consider custom exception classes to hide vendor details.EnsureException occurrences in Sentry/Laravel Horizon to identify invariant violations in production.try {
Ensure::isTrue($condition, 'Critical invariant failed');
} catch (EnsureException $e) {
report($e); // Uses Laravel’s error reporting
throw $e;
}
Ensure calls in critical paths (e.g., payment processing).| Scenario | Impact | Mitigation |
|---|---|---|
| Invalid precondition in service | Silent data corruption | Log + alert (e.g., Slack/PagerDuty) |
| Postcondition violation | Inconsistent state | Rollback transactions or retry |
| PHP 8+ incompatibility | Runtime errors | Fork/replace with custom solution |
Overuse of Ensure |
"Validation fatigue" | Document usage guidelines |
Ensure usage patterns.## Precondition Checks
Use `Ensure::isNotNull($user, 'User must exist')` before business logic.
Ensure usage in critical paths.assert() with Ensure in a PR.// Validations
Ensure::isNotEmpty($input, 'Input required');
Ensure::isInstanceOf(User::class, $user, 'Must be User');
Ensure::isTrue($user->isActive(), 'User must be
How can I help you explore Laravel packages today?