Skip to content

Commit

Permalink
Making debug functions return the same variable
Browse files Browse the repository at this point in the history
Also removing polyfill for json_last_error_msg as we require PHP >= 5.5
  • Loading branch information
lorenzo committed Mar 24, 2016
1 parent 6abb45a commit 90bdf22
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 38 deletions.
12 changes: 10 additions & 2 deletions src/Core/functions.php
Expand Up @@ -127,19 +127,23 @@ function namespaceSplit($class)
* In terminals this will act similar to using print_r() directly, when not run on cli
* print_r() will also wrap <pre> tags around the output of given variable. Similar to debug().
*
* This function returns the same variable that was passed.
*
* @param mixed $var Variable to print out.
* @return void
* @see debug()
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#pr
* @return mixed the same $var that was passed to this function
*/
function pr($var)
{
if (!Configure::read('debug')) {
return;
return $var;
}

$template = (PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg') ? '<pre class="pr">%s</pre>' : "\n%s\n\n";
printf($template, trim(print_r($var, true)));
return $var;
}

}
Expand All @@ -151,19 +155,23 @@ function pr($var)
* In terminals this will act similar to using json_encode() with JSON_PRETTY_PRINT directly, when not run on cli
* will also wrap <pre> tags around the output of given variable. Similar to pr().
*
* This function returns the same variable that was passed.
*
* @param mixed $var Variable to print out.
* @return void
* @see pr()
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#pj
* @return mixed the same $var that was passed to this function
*/
function pj($var)
{
if (!Configure::read('debug')) {
return;
return $var;
}

$template = (PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg') ? '<pre class="pj">%s</pre>' : "\n%s\n\n";
printf($template, trim(json_encode($var, JSON_PRETTY_PRINT)));
return $var;
}

}
Expand Down
30 changes: 6 additions & 24 deletions src/basics.php
Expand Up @@ -28,7 +28,8 @@

if (!function_exists('debug')) {
/**
* Prints out debug information about given variable.
* Prints out debug information about given variable and returns the
* variable that was passed.
*
* Only runs if debug level is greater than zero.
*
Expand All @@ -38,13 +39,15 @@
* @return void
* @link http://book.cakephp.org/3.0/en/development/debugging.html#basic-debugging
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#debug
* @return mixed The same $var that was passed
*/
function debug($var, $showHtml = null, $showFrom = true)
{
if (!Configure::read('debug')) {
return;
return $var;
}

$originalVar = $var;
$file = '';
$line = '';
$lineInfo = '';
Expand Down Expand Up @@ -91,6 +94,7 @@ function debug($var, $showHtml = null, $showFrom = true)
}
}
printf($template, $lineInfo, $var);
return $originalVar;
}

}
Expand Down Expand Up @@ -122,28 +126,6 @@ function stackTrace(array $options = [])

}

if (!function_exists('json_last_error_msg')) {
/**
* Provides the fallback implementation of json_last_error_msg() available in PHP 5.5 and above.
*
* @return string Error message.
*/
function json_last_error_msg()
{
static $errors = [
JSON_ERROR_NONE => '',
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
];
$error = json_last_error();
return array_key_exists($error, $errors) ? $errors[$error] : "Unknown error ({$error})";
}

}

if (!function_exists('breakpoint')) {
/**
* Command to return the eval-able code to startup PsySH in interactive debugger
Expand Down
26 changes: 14 additions & 12 deletions tests/TestCase/BasicsTest.php
Expand Up @@ -224,7 +224,7 @@ public function testH()
public function testDebug()
{
ob_start();
debug('this-is-a-test', false);
$this->assertEquals('this-is-a-test', debug('this-is-a-test', false));
$result = ob_get_clean();
$expectedText = <<<EXPECTED
%s (line %d)
Expand All @@ -238,7 +238,8 @@ public function testDebug()
$this->assertEquals($expected, $result);

ob_start();
debug('<div>this-is-a-test</div>', true);
$value = '<div>this-is-a-test</div>';
$this->assertSame($value, debug($value, true));
$result = ob_get_clean();
$expectedHtml = <<<EXPECTED
<div class="cake-debug-output">
Expand Down Expand Up @@ -369,7 +370,7 @@ public function testDebug()
$this->assertEquals($expected, $result);

ob_start();
debug(false, false, false);
$this->assertFalse(debug(false, false, false));
$result = ob_get_clean();
$expected = <<<EXPECTED
Expand All @@ -390,25 +391,25 @@ public function testDebug()
public function testPr()
{
ob_start();
pr(true);
$this->assertTrue(pr(true));
$result = ob_get_clean();
$expected = "\n1\n\n";
$this->assertEquals($expected, $result);

ob_start();
pr(false);
$this->assertFalse(pr(false));
$result = ob_get_clean();
$expected = "\n\n\n";
$this->assertEquals($expected, $result);

ob_start();
pr(null);
$this->assertNull(pr(null));
$result = ob_get_clean();
$expected = "\n\n\n";
$this->assertEquals($expected, $result);

ob_start();
pr(123);
$this->assertSame(123, pr(123));
$result = ob_get_clean();
$expected = "\n123\n\n";
$this->assertEquals($expected, $result);
Expand Down Expand Up @@ -440,25 +441,25 @@ public function testPr()
public function testPj()
{
ob_start();
pj(true);
$this->assertTrue(pj(true));
$result = ob_get_clean();
$expected = "\ntrue\n\n";
$this->assertEquals($expected, $result);

ob_start();
pj(false);
$this->assertFalse(pj(false));
$result = ob_get_clean();
$expected = "\nfalse\n\n";
$this->assertEquals($expected, $result);

ob_start();
pj(null);
$this->assertNull(pj(null));
$result = ob_get_clean();
$expected = "\nnull\n\n";
$this->assertEquals($expected, $result);

ob_start();
pj(123);
$this->assertSame(123, pj(123));
$result = ob_get_clean();
$expected = "\n123\n\n";
$this->assertEquals($expected, $result);
Expand All @@ -476,7 +477,8 @@ public function testPj()
$this->assertEquals($expected, $result);

ob_start();
pj(['this' => 'is', 'a' => 'test', 123 => 456]);
$value = ['this' => 'is', 'a' => 'test', 123 => 456];
$this->assertSame($value, pj($value));
$result = ob_get_clean();
$expected = "\n{\n \"this\": \"is\",\n \"a\": \"test\",\n \"123\": 456\n}\n\n";
$this->assertEquals($expected, $result);
Expand Down

0 comments on commit 90bdf22

Please sign in to comment.