Completed
Push — search ( 076ba7...2a5267 )
by Simon
16:36 queued 11:48
created

DebugHelper::get_debug_backtrace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Helpers;
10
11
/**
12
 * DebugHelper provides backtrace utilities for debugging and error logging
13
 */
14
class DebugHelper
15
{
16
    /**
17
     * Internal mockable method wrapper for debug_backtrace.
18
     *
19
     * As mocking out debug_backtrace uses debug_backtrace internally, we need this in order to not cause a recursive
20
     * cascade until the runtime explodes.
21
     *
22
     * Instead, we mock this method, which allows debug_backtrace to still be called correctly.
23
     *
24
     * @return array
25
     */
26
    public function get_debug_backtrace()
27
    {
28
        return debug_backtrace();
29
    }
30
31
    /**
32
     * Returns a string representation of the current backtrace for display.
33
     *
34
     * Note that this explicitly excludes the top two frames, which will be methods from this class.
35
     *
36
     * @return string
37
     */
38 1
    public function getBacktrace()
39
    {
40 1
        $backtrace = $this->get_debug_backtrace();
41
42 1
        $output = "";
43
44 1
        $count = 0;
45 1
        foreach ($backtrace as $line) {
46 1
            if ($count <= 1) {
47 1
                $count++;
48 1
                continue;
49
            }
50
51 1
            $output .= "#{$count}: ";
52
53 1
            if (isset($line['type']) && $line['type'] != "") {
54
                $output .= $line['class'] . $line['type'];
55
            }
56
57 1
            $output .= $line['function'] . "(...)";
58 1
            $output .= " [{$line['file']}#{$line['line']}\r\n";
59
60 1
            $count++;
61
        }
62
63 1
        return $output;
64
    }
65
}
66