Page MenuHomePhabricator

D539.id1716.diff
No OneTemporary

D539.id1716.diff

diff --git a/core/functions.php b/core/functions.php
--- a/core/functions.php
+++ b/core/functions.php
@@ -63,6 +63,57 @@
return $app;
}
+/**
+ * Raises an exception whenever the application runs into an scenario that was not
+ * expected. This function functions in a very similar manner to an assertion (with
+ * the exception of this being executed in production).
+ *
+ * The second parameter contains the name of the exception to be raised on failure,
+ * an exception instance or a closure to be executed, which should then either end
+ * execution or throw an exception itself.
+ *
+ * This is intended to reduce the boilerplate on code that looks like this:
+ * if (!$condition) { throw new Exception(); }
+ *
+ * The code becomes a bit more readable and drops the negation, making it look like this:
+ * assume($condition, Exception::class);
+ *
+ * Overall reducing the strain of visually analyzing the code.
+ *
+ * @param bool $condition If the condition is true, the code will continue being executed
+ * @param string|Closure|Exception $failure
+ * @return void
+ */
+function assume(bool $condition, $failure) : void
+{
+ /*
+ * If the condition is met, the function doesn't do anything. It just returns and allows
+ * the application to continue.
+ */
+ if ($condition) { return; }
+
+ /**
+ * Otherwise, we need to stop the execution. This should be done in the closure, but if the
+ * user does not raise any exception in the closure, our code will do.
+ */
+ if ($failure instanceof Closure) {
+ $failure();
+ throw new Exception('Failed to meet assumption');
+ }
+
+ /**
+ * If the user provided an exception instance, we throw the provided exception.
+ */
+ if ($failure instanceof Exception) { throw $failure; }
+
+ /**
+ * The last case (if the failure code was a string) will instance a new exception without message,
+ * this is the most common way to use this, since it will be the mechanism activated when using
+ * the ::class magic constant.
+ */
+ throw new $failure();
+}
+
/**
* Shorthand function to create / retrieve the model the application is using
* to store data. We could consider this a little DB handler factory.
diff --git a/tests/core/AssumeTest.php b/tests/core/AssumeTest.php
new file mode 100644
--- /dev/null
+++ b/tests/core/AssumeTest.php
@@ -0,0 +1,49 @@
+<?php namespace tests\spitfire\core;
+
+use Exception;
+use PHPUnit\Framework\TestCase;
+
+/*
+ * The MIT License
+ *
+ * Copyright 2019 César de la Cal Bretschneider <cesar@magic3w.com>.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+
+class AssumeTest extends TestCase
+{
+
+ public function testAssumeTrue()
+ {
+ $raised = false;
+
+ try { assume(true === true, Exception::class); }
+ catch (Exception $ex) { $raised = true; }
+
+ $this->assertFalse($raised);
+ }
+
+ public function testAssumeFalse()
+ {
+ $this->expectException(Exception::class);
+ assume(false === true, Exception::class);
+ }
+}
\ No newline at end of file

File Metadata

Mime Type
text/plain
Expires
Apr 12 2021, 3:32 AM (9 w, 1 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5620
Default Alt Text
D539.id1716.diff (3 KB)

Event Timeline