Completed
Push — fix-2494 ( 3153ee...40d9bb )
by Sam
13:43 queued 06:38
created

CliDebugView::renderSourceFragment()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Dev;
4
5
use SilverStripe\Control\HTTPRequest;
6
7
/**
8
 * A basic HTML wrapper for stylish rendering of a developement info view.
9
 * Used to output error messages, and test results.
10
 *
11
 * @todo Perhaps DebugView should be an interface / ABC, implemented by HTMLDebugView and CliDebugView?
12
 */
13
class CliDebugView extends DebugView
14
{
15
16
    /**
17
     * Render HTML header for development views
18
     *
19
     * @param HTTPRequest $httpRequest
20
     * @return string
21
     */
22
    public function renderHeader($httpRequest = null)
23
    {
24
        return null;
25
    }
26
27
    /**
28
     * Render HTML footer for development views
29
     */
30
    public function renderFooter()
31
    {
32
    }
33
34
    /**
35
     * Write information about the error to the screen
36
     *
37
     * @param string $httpRequest
38
     * @param int $errno
39
     * @param string $errstr
40
     * @param string $errfile
41
     * @param int $errline
42
     * @return string
43
     */
44
    public function renderError($httpRequest, $errno, $errstr, $errfile, $errline)
45
    {
46
        if (!isset(self::$error_types[$errno])) {
47
            $errorTypeTitle = "UNKNOWN TYPE, ERRNO $errno";
48
        } else {
49
            $errorTypeTitle = self::$error_types[$errno]['title'];
50
        }
51
        $output = CLI::text("ERROR [" . $errorTypeTitle . "]: $errstr\nIN $httpRequest\n", "red", null, true);
52
        $output .= CLI::text("Line $errline in $errfile\n\n", "red");
53
54
        return $output;
55
    }
56
57
    /**
58
     * Write a fragment of the a source file
59
     *
60
     * @param array $lines An array of file lines; the keys should be the original line numbers
61
     * @param int $errline Index of the line in $lines which has the error
62
     * @return string
63
     */
64
    public function renderSourceFragment($lines, $errline)
65
    {
66
        $output = "Source\n======\n";
67
        foreach ($lines as $offset => $line) {
68
            $output .= ($offset == $errline) ? "* " : "  ";
69
            $output .= str_pad("$offset:", 5);
70
            $output .= wordwrap($line, self::config()->columns, "\n       ");
71
        }
72
        $output .= "\n";
73
74
        return $output;
75
    }
76
77
    /**
78
     * Write a backtrace
79
     *
80
     * @param array $trace
81
     * @return string
82
     */
83
    public function renderTrace($trace = null)
84
    {
85
        $output = "Trace\n=====\n";
86
        $output .= Backtrace::get_rendered_backtrace($trace ? $trace : debug_backtrace(), true);
87
88
        return $output;
89
    }
90
91
    public function renderParagraph($text)
92
    {
93
        return wordwrap($text, self::config()->columns) . "\n\n";
94
    }
95
96
    /**
97
     * Render the information header for the view
98
     *
99
     * @param string $title
100
     * @param string $subtitle
101
     * @param string $description
102
     * @return string
103
     */
104
    public function renderInfo($title, $subtitle, $description = null)
105
    {
106
        $output = wordwrap(strtoupper($title), self::config()->columns) . "\n";
107
        $output .= wordwrap($subtitle, self::config()->columns) . "\n";
108
        $output .= str_repeat('-', min(self::config()->columns, max(strlen($title), strlen($subtitle)))) . "\n";
109
        $output .= wordwrap($description, self::config()->columns) . "\n\n";
110
111
        return $output;
112
    }
113
114
    public function renderVariable($val, $caller)
115
    {
116
        $output = PHP_EOL;
117
        $output .= CLI::text(str_repeat('=', self::config()->columns), 'green');
118
        $output .= PHP_EOL;
119
        $output .= CLI::text($this->formatCaller($caller), 'blue', null, true);
120
        $output .= PHP_EOL.PHP_EOL;
121 View Code Duplication
        if (is_string($val)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
            $output .= wordwrap($val, self::config()->columns);
123
        } else {
124
            $output .= var_export($val, true);
125
        }
126
        $output .= PHP_EOL;
127
        $output .= CLI::text(str_repeat('=', self::config()->columns), 'green');
128
        $output .= PHP_EOL;
129
130
        return $output;
131
    }
132
}
133