Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dd_when Helper to Conditionally Trigger dd() #53300

Open
wants to merge 1 commit into
base: 11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,3 +507,19 @@ function with($value, ?callable $callback = null)
return is_null($callback) ? $value : $callback($value);
}
}

if (!function_exists('dd_when')) {
/**
* Dump and die when a condition is met.
*
* @param bool $condition
* @param mixed ...$vars
* @return void
*/
function dd_when(bool $condition, mixed ...$vars): void
{
if ($condition) {
dd(...$vars);
}
}
}
12 changes: 12 additions & 0 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,18 @@ public function testPregReplaceArray($pattern, $replacements, $subject, $expecte
preg_replace_array($pattern, $replacements, $subject)
);
}

public function testDdWhenDoesNotDumpWhenConditionIsFalse()
{
// Ensure that when the condition is false, dd() does not execute.
try {
dd_when(false, 'This should not dump');
} catch (RuntimeException $e) {
$this->fail('dd_when should not have called dd when condition is false.');
}

$this->assertTrue(true); // If no exception, the test passes.
}
}

trait SupportTestTraitOne
Expand Down