Advertisement
Shaun_B

Generic PHP Debug function

Jan 24th, 2019
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.95 KB | None | 0 0
  1. <?php
  2. ## Remember to define the debug constant accordingly
  3. ## For dev and local enviroments:
  4. define('API_DEBUG_MODE', TRUE);
  5.  
  6. ## For live environments DO NOT DEFINE THE CONSTANT
  7.  
  8. /**
  9.  * Reducing code smells and making more readable;
  10.  * Usage:
  11.  * Replace
  12.  *  if($value === false){ ... }
  13.  * with
  14.  *  if(isFalse($value)){ ... }
  15.  *
  16.  * @param mixed $value
  17.  * @author Shaun B
  18.  * @date 4 Jul 2018 16:28:20
  19.  * @return bool
  20.  */
  21. function isFalse($value = false): bool
  22. {
  23.     return (bool) ($value === FALSE) ? 1 : 0;
  24. }
  25.  
  26. /**
  27.  * Reducing code smells and making more readable;
  28.  * Usage:
  29.  * Replace
  30.  * if($value === true){ ... }
  31.  * with
  32.  * if(isTrue($value)){ ... }
  33.  *
  34.  * @param mixed $value
  35.  * @author Shaun B
  36.  * @date 4 Jul 2018 16:29:27
  37.  * @return boolean
  38.  */
  39. function isTrue($value = true): bool
  40. {
  41.     return (bool) ($value === TRUE) ? 1 : 0;
  42. }
  43.  
  44. /**
  45.  * Generic debug to see the contents of an object, resource or scalar
  46.  * Not intended to be deployed live
  47.  *
  48.  * @param mixed $value
  49.  * @param bool $die
  50.  * @param string $message
  51.  * @param string $file
  52.  * @param string $line
  53.  * @param string $header
  54.  * @author Shaun B
  55.  * @date 3 Apr 2018 13:37:42
  56.  * @return void
  57.  */
  58. function debug($value = null, bool $die = false, string $message = null, string $file = null, string $line = null, string $header = null): void
  59. {
  60.     if (isTrue(defined('API_DEBUG_MODE'))) {
  61.         $openDiv = '<div>';
  62.         $closeDiv = '</div>';
  63.         $openPre = '<pre>';
  64.         $closePre = '</pre>';
  65.  
  66.         echo (strlen($header) > 0) ? "{$openDiv}<h1>{$header}</h1>" : "";
  67.         echo $openPre . print_r($value, true) . $closePre;
  68.         echo (! empty($file)) ? $openPre . 'File: ' . print_r($file, true) . $closePre : "";
  69.         echo (! empty($line)) ? $openPre . 'Line: ' . print_r($line, true) . $closePre : "";
  70.         echo (strlen($header) > 0) ? "{$closeDiv}" : "";
  71.         if ($die === true) {
  72.             die($message ?? '');
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement