Skip to content

Commit 5318be6

Browse files
committed
feat: add dd() and d()
1 parent 19638f5 commit 5318be6

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

src/DebugHelper.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kenjis\PhpUnitHelper;
6+
7+
require_once __DIR__ . '/functions.php';
8+
9+
trait DebugHelper
10+
{
11+
}

src/functions.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* Helper Functions
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
/**
10+
* @SuppressWarnings(PHPMD.ExitExpression)
11+
* Does not work if I move to the function annotation
12+
* See https://github.com/phpmd/phpmd/issues/337
13+
*/
14+
if (! function_exists('dd')) {
15+
/**
16+
* Alias of var_dump() and exit()
17+
*
18+
* @param mixed ...$vars
19+
*
20+
* @psalm-suppress ForbiddenCode Unsafe var_dump
21+
* @codeCoverageIgnore
22+
*/
23+
function dd(...$vars): void
24+
{
25+
var_dump(...$vars);
26+
exit;
27+
}
28+
}
29+
30+
if (! function_exists('d')) {
31+
/**
32+
* Alias of var_dump()
33+
*
34+
* @param mixed ...$vars
35+
*
36+
* @psalm-suppress ForbiddenCode Unsafe var_dump
37+
*/
38+
function d(...$vars): void
39+
{
40+
var_dump(...$vars);
41+
}
42+
}

tests/DebugHelperTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kenjis\PhpUnitHelper;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
class DebugHelperTest extends TestCase
10+
{
11+
use DebugHelper;
12+
13+
public function test_d(): void
14+
{
15+
$this->expectOutputRegex(
16+
'/string\(4\) "var1"\n.*\nint\(100\)\n/'
17+
);
18+
19+
$var1 = 'var1';
20+
$var2 = 100;
21+
d($var1, $var2);
22+
}
23+
}

0 commit comments

Comments
 (0)