Page MenuHomeDevCentral

D1614.id4123.diff
No OneTemporary

D1614.id4123.diff

diff --git a/phpcs.xml b/phpcs.xml
--- a/phpcs.xml
+++ b/phpcs.xml
@@ -2,6 +2,11 @@
<ruleset name="Nasqueron">
<rule ref="vendor/nasqueron/codestyle/CodeSniffer/ruleset.xml" />
+ <!-- Allow dprint_r() and dieprint_r() legacy debug function names -->
+ <rule ref="Generic.NamingConventions.CamelCapsFunctionName.NotCamelCaps">
+ <exclude-pattern>*/_register_to_global_space.php</exclude-pattern>
+ </rule>
+
<file>src</file>
<file>tests</file>
</ruleset>
diff --git a/src/Debug/Debugger.php b/src/Debug/Debugger.php
new file mode 100644
--- /dev/null
+++ b/src/Debug/Debugger.php
@@ -0,0 +1,32 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Debug;
+
+class Debugger {
+
+ /**
+ * Prints human-readable information about a variable, wrapped in a <pre> block
+ *
+ * @param mixed $variable the variable to dump
+ */
+ public static function printVariable ($variable) : void {
+ echo "<pre class='debug'>";
+ print_r($variable);
+ echo "</pre>";
+ }
+
+ public static function printVariableAndDie ($variable) : void {
+ static::printVariable($variable);
+ die;
+ }
+
+ ///
+ /// Comfort debug helper to register debug method in global space
+ ///
+
+ public static function register () : void {
+ require_once '_register_to_global_space.php';
+ }
+
+}
diff --git a/src/Debug/_register_to_global_space.php b/src/Debug/_register_to_global_space.php
new file mode 100644
--- /dev/null
+++ b/src/Debug/_register_to_global_space.php
@@ -0,0 +1,17 @@
+<?php
+
+/* This code is intentionnally left in the global namespace. */
+
+use Keruald\OmniTools\Debug\Debugger;
+
+if (!function_exists("dprint_r")) {
+ function dprint_r ($variable) {
+ Debugger::printVariable($variable);
+ }
+}
+
+if (!function_exists("dieprint_r")) {
+ function dieprint_r ($variable) {
+ Debugger::printVariableAndDie($variable);
+ }
+}
diff --git a/tests/Debug/DebuggerTest.php b/tests/Debug/DebuggerTest.php
new file mode 100644
--- /dev/null
+++ b/tests/Debug/DebuggerTest.php
@@ -0,0 +1,27 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Tests\Debug;
+
+
+use Keruald\OmniTools\Debug\Debugger;
+use PHPUnit\Framework\TestCase;
+
+class DebuggerTest extends TestCase {
+
+ public function testRegister () {
+ $this->assertTestSuiteStateIsValid();
+
+ Debugger::register();
+
+ $this->assertTrue(function_exists("dprint_r"));
+ }
+
+ private function assertTestSuiteStateIsValid() : void {
+ $this->assertFalse(
+ function_exists("dprint_r"),
+ "Configure the test suite so dprint_r isn't in global space first."
+ );
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Thu, Jun 19, 02:15 (18 h, 24 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2746249
Default Alt Text
D1614.id4123.diff (2 KB)

Event Timeline