Passed
Pull Request — master (#34)
by Anton
02:40
created

DiffRendererHtmlInline   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 58.7%

Importance

Changes 0
Metric Value
wmc 14
eloc 78
dl 0
loc 118
ccs 27
cts 46
cp 0.587
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C render() 0 108 14
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii\Component;
6
7
use Diff_Renderer_Html_Array;
8
9
/**
10
 * Renders diff to HTML. Output adjusted to be copy-paste friendly.
11
 *
12
 * @psalm-suppress PropertyNotSetInConstructor
13
 */
14
class DiffRendererHtmlInline extends Diff_Renderer_Html_Array
15
{
16
    /**
17
     * Render a and return diff with changes between the two sequences
18
     * displayed inline (under each other)
19
     *
20
     * @psalm-suppress ImplementedReturnTypeMismatch
21
     *
22
     * @return string The generated inline diff.
23
     */
24 1
    public function render(): string
25
    {
26 1
        $changes = parent::render();
27 1
        $html = '';
28 1
        if (empty($changes)) {
29
            return $html;
30
        }
31
32
        $html .= <<<HTML
33 1
<table class="Differences DifferencesInline">
34
    <thead>
35
        <tr>
36
            <th>Old</th>
37
            <th>New</th>
38
            <th>Differences</th>
39
        </tr>
40
    </thead>
41
HTML;
42 1
        foreach ($changes as $i => $blocks) {
43
            // If this is a separate block, we're condensing code so output ...,
44
            // indicating a significant portion of the code has been collapsed as
45
            // it is the same
46 1
            if ($i > 0) {
47
                $html .= <<<HTML
48
    <tbody class="Skipped">
49
        <th data-line-number="&hellip;"></th>
50
        <th data-line-number="&hellip;"></th>
51
        <td>&nbsp;</td>
52
    </tbody>
53
HTML;
54
            }
55
56 1
            foreach ($blocks as $change) {
57 1
                $tag = ucfirst($change['tag']);
58
                $html .= <<<HTML
59 1
    <tbody class="Change{$tag}">
60
HTML;
61
                // Equal changes should be shown on both sides of the diff
62 1
                if ($change['tag'] === 'equal') {
63 1
                    foreach ($change['base']['lines'] as $no => $line) {
64 1
                        $fromLine = $change['base']['offset'] + $no + 1;
65 1
                        $toLine = $change['changed']['offset'] + $no + 1;
66
                        $html .= <<<HTML
67 1
        <tr>
68 1
            <th data-line-number="{$fromLine}"></th>
69 1
            <th data-line-number="{$toLine}"></th>
70 1
            <td class="Left">{$line}</td>
71
        </tr>
72
HTML;
73
                    }
74
                } // Added lines only on the right side
75 1
                elseif ($change['tag'] === 'insert') {
76 1
                    foreach ($change['changed']['lines'] as $no => $line) {
77 1
                        $toLine = $change['changed']['offset'] + $no + 1;
78
                        $html .= <<<HTML
79 1
        <tr>
80
            <th data-line-number="&nbsp;"></th>
81 1
            <th data-line-number="{$toLine}"></th>
82 1
            <td class="Right"><ins>{$line}</ins>&nbsp;</td>
83
        </tr>
84
HTML;
85
                    }
86
                } // Show deleted lines only on the left side
87
                elseif ($change['tag'] === 'delete') {
88
                    foreach ($change['base']['lines'] as $no => $line) {
89
                        $fromLine = $change['base']['offset'] + $no + 1;
90
                        $html .= <<<HTML
91
        <tr>
92
            <th data-line-number="{$fromLine}"></th>
93
            <th data-line-number="&nbsp;"></th>
94
            <td class="Left"><del>{$line}</del>&nbsp;</td>
95
        </tr>
96
HTML;
97
                    }
98
                } // Show modified lines on both sides
99
                elseif ($change['tag'] === 'replace') {
100
                    foreach ($change['base']['lines'] as $no => $line) {
101
                        $fromLine = $change['base']['offset'] + $no + 1;
102
                        $html .= <<<HTML
103
        <tr>
104
            <th data-line-number="{$fromLine}"></th>
105
            <th data-line-number="&nbsp;"></th>
106
            <td class="Left"><span>{$line}</span></td>
107
        </tr>
108
HTML;
109
                    }
110
111
                    foreach ($change['changed']['lines'] as $no => $line) {
112
                        $toLine = $change['changed']['offset'] + $no + 1;
113
                        $html .= <<<HTML
114
        <tr>
115
            <th data-line-number="{$toLine}"></th>
116
            <th data-line-number="&nbsp;"></th>
117
            <td class="Right"><span>{$line}</span></td>
118
        </tr>
119
HTML;
120
                    }
121
                }
122
                $html .= <<<HTML
123 1
    </tbody>
124
HTML;
125
            }
126
        }
127
        $html .= <<<HTML
128 1
</table>
129
HTML;
130
131 1
        return $html;
132
    }
133
}
134