Issues (994)

src/MVC/Exception.php (2 issues)

1
<?php
2
3
namespace MVC;
4
5
class Exception extends \Exception
6
{
7
  // Redefine the exception so message isn't optional
8
  public function __construct($message, $code = 0, Exception $previous = null)
9
  {
10
    // some code
11
    // make sure everything is assigned properly
12
    parent::__construct($this->generateCallTrace(), $code, $previous);
13
  }
14
15
  // custom string representation of object
16
  public function __toString()
17
  {
18
    return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
19
  }
20
21
  public function generateCallTrace()
22
  {
23
    $e = new \Exception();
24
    $trace = explode("\n", $e->getTraceAsString());
25
    // reverse array to make steps line up chronologically
26
    $trace = array_reverse($trace);
27
    array_shift($trace); // remove {main}
28
    array_pop($trace); // remove call to this method
29
    $length = count($trace);
30
    $result = [];
31
32
    for ($i = 0; $i < $length; ++$i) {
33
      $result[] = ($i + 1) . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering
34
    }
35
36
    return nl2br("\t" . implode("\n\t", $result) . "\n\n" . $this->debug_string_backtrace());
37
  }
38
39
  public function debug_string_backtrace()
40
  {
41
    if (ob_get_level()) {
42
      ob_get_clean();
43
    }
44
    ob_start();
45
46
    echo "debug_print_backtrace:\n\n ";
47
    debug_print_backtrace();
48
    echo "\n\n";
49
    if ($this->is_admin() || isset($_REQUEST['dbg'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->is_admin() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
50
      echo "debug_backtrace (administrator only): \n\n ";
51
      echo '<pre>';
52
      var_dump(debug_backtrace());
0 ignored issues
show
Security Debugging Code introduced by
var_dump(debug_backtrace()) looks like debug code. Are you sure you do not want to remove it?
Loading history...
53
      echo '</pre>';
54
    }
55
56
    $trace = ob_get_contents();
57
    ob_end_clean();
58
59
    // Remove first item from backtrace as it's this function which
60
    // is redundant.
61
    //$trace = preg_replace('/^#0\s+' . __FUNCTION__ . "[^\n]*\n/", '', $trace, 1);
62
63
    // Renumber backtrace items.
64
    //$trace = preg_replace('/^#(\d+)/me', '\'#\' . ($1 - 1)', $trace);
65
66
    return $trace;
67
  }
68
69
  public function is_admin()
70
  {
71
    return is_admin();
72
  }
73
}
74