Skip to content

Instantly share code, notes, and snippets.

@richardDobron
Created November 3, 2023 20:41
Show Gist options
  • Save richardDobron/b4204b550ee162330b92e42b4f3fb933 to your computer and use it in GitHub Desktop.
Save richardDobron/b4204b550ee162330b92e42b4f3fb933 to your computer and use it in GitHub Desktop.
<?php
/**
* Use invariant() to assert state which your program assumes to be true.
*
* @throws Exception
*/
function invariant(mixed $condition, string $message = 'Invariant Violation', ...$params): void
{
if (! $condition) {
if (func_num_args() > 2) {
$message = sprintf($message, ...$params);
}
throw new Exception($message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment