HTML   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 21
c 1
b 0
f 0
dl 0
loc 56
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSeparator() 0 3 1
A getContainer() 0 3 1
A setSeparator() 0 5 1
A render() 0 30 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Mistrals\Diff\Renderer;
6
7
use Mistralys\Diff\Diff;
8
use Mistralys\Diff\DiffException;
9
10
class HTML extends Renderer
11
{
12
    private string $separator = '<br>';
13
    private string $container = '<div class="text-diff-container">%s</div>';
14
    
15
    public function setSeparator(string $separator) : HTML
16
    {
17
        $this->separator = $separator;
18
        
19
        return $this;
20
    }
21
    
22
    public function getSeparator() : string
23
    {
24
        return $this->separator;
25
    }
26
    
27
    public function getContainer() : string
28
    {
29
        return $this->container;
30
    }
31
32
    /**
33
     * @return string
34
     * @throws DiffException
35
     */
36
    public function render() : string
37
    {
38
        $html = '';
39
        
40
        $diff = $this->diff->toArray();
41
        
42
        // loop over the lines in the diff
43
        foreach ($diff as $line)
44
        {
45
            $element = '';
46
            
47
            // extend the HTML with the line
48
            switch ($line[1])
49
            {
50
                case Diff::UNMODIFIED : $element = 'span'; break;
51
                case Diff::DELETED    : $element = 'del';  break;
52
                case Diff::INSERTED   : $element = 'ins';  break;
53
            }
54
            
55
            $html .= sprintf(
56
                '<%1$s>%2$s</%1$s>',
57
                $element,
58
                htmlspecialchars((string)$line[0])
59
            );
60
            
61
            // extend the HTML with the separator
62
            $html .= $this->separator;
63
        }
64
        
65
        return sprintf($this->container, $html);
66
    }
67
}
68