DaisyDiff   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 31
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A diff() 0 22 1
1
<?php
2
/**
3
 * (c) Steve Nebes <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace SN\DaisyDiff;
12
13
use SN\DaisyDiff\Html\ChangeText;
14
use SN\DaisyDiff\Html\Dom\DomTreeBuilder;
15
use SN\DaisyDiff\Html\HtmlDiffer;
16
use SN\DaisyDiff\Html\HtmlSaxDiffOutput;
17
use SN\DaisyDiff\Html\TextNodeComparator;
18
use SN\DaisyDiff\Xml\XMLReader;
19
20
/**
21
 * Daisy Diff is a library that diffs (compares) HTML.
22
 */
23
class DaisyDiff
24
{
25
    /**
26
     * Diffs two HTML strings, returning the result.
27
     *
28
     * @param string $oldText
29
     * @param string $newText
30
     * @return string
31
     */
32 34
    public function diff(string $oldText, string $newText): string
33
    {
34
        // Parse $old XML.
35 34
        $oldHandler = new DomTreeBuilder();
36 34
        $reader1 = new XMLReader($oldHandler);
37 34
        $reader1->parse($oldText);
38
39
        // Parse $new XML.
40 34
        $newHandler = new DomTreeBuilder();
41 34
        $reader2 = new XMLReader($newHandler);
42 34
        $reader2->parse($newText);
43
44
        // Comparators.
45 34
        $leftComparator = new TextNodeComparator($oldHandler);
46 34
        $rightComparator = new TextNodeComparator($newHandler);
47
48 34
        $changeText = new ChangeText();
49 34
        $output = new HtmlSaxDiffOutput($changeText);
50 34
        $differ = new HtmlDiffer($output);
51 34
        $differ->diff($leftComparator, $rightComparator);
52
53 34
        return $changeText->getText();
54
    }
55
}
56