Skip to content

(feat) Add blank, filled, with, rescue, retry and benchmark helpers - #344

Open
techmahedy wants to merge 1 commit into
doppar:4.xfrom
techmahedy:techmahedy-4.x
Open

techmahedy wants to merge 1 commit into
doppar:4.xfrom
techmahedy:techmahedy-4.x

Conversation

@techmahedy

@techmahedy techmahedy commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

Pull Request Checklist

Q A
Branch? 4.x
Bug fix? no
New feature? Yes
Deprecations? no
Issues -
License MIT

Description

Summary

Adds six global helper functions to helpers.php. Each is wrapped in function_exists, so an application that already defines one of them is unaffected.

New helpers

Helper Description
blank($value) True for null, empty or whitespace-only strings, empty arrays and empty Countable objects. 0, 0.0, '0' and false are not blank, unlike empty().
filled($value) Inverse of blank().
with($value, $callback = null) Returns the callback's result, or the value itself when no callback is given. Unlike tap(), which always returns the original value.
rescue($callback, $default = null, $report = true) Returns $default if the callback throws. A Closure default receives the exception. The exception is logged unless $report is false, and a failing logger does not hide the fallback value.
retry($times, $callback, $sleepMs = 0, $when = null) Runs the callback up to $times attempts and passes it the attempt number (from 1). Rethrows the last exception when all attempts fail. $sleepMs may be a Closure for backoff. $when returns false to stop retrying and rethrow at once. $times below 1 throws InvalidArgumentException.
benchmark($callback) Runs the callback once and returns result, time_ms, memory_bytes and peak_memory_bytes. Exceptions are not caught.
$name = filled($request->input('name')) ? $request->input('name') : 'guest';

$response = retry(4, fn($attempt) => $client->get($url), fn($attempt) => 100 * (2 ** ($attempt - 1)));

$config = rescue(fn() => json_decode($raw, flags: JSON_THROW_ON_ERROR), []);

$report = benchmark(fn() => User::query()->chunk(500, fn($users) => null));

Docs

helpers.md in the docs repo has a section for each helper and they are added to the Core Helpers index

Checklist

  • Tests have been added or updated
  • Documentation has been updated if necessary
  • Code follows the project coding standards
  • All tests pass locally

@techmahedy techmahedy added the feat new feature label Oct 1, 2026
@techmahedy
techmahedy requested a review from rrr63 October 1, 2026 13:30
@techmahedy techmahedy self-assigned this Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant