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

DebugHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
wmc 6
eloc 16
dl 0
loc 50
ccs 14
cts 17
cp 0.8235
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get_debug_backtrace() 0 3 1
A getBacktrace() 0 26 5
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