Passed
Push — master ( d304b3...db8bd2 )
by Sven
01:20 queued 11s
created

addDebugOutput()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Caxy\HtmlDiff\HtmlDiff;
4
5
ini_set('display_errors', 1);
6
error_reporting(E_ALL);
7
8
require __DIR__.'/../vendor/autoload.php';
9
10
$debugOutput = array();
11
12
function addDebugOutput($value, $key = 'general')
13
{
14
    global $debugOutput;
15
16
    if (!is_string($value)) {
17
        $value = var_export($value, true);
18
    }
19
20
    if (!array_key_exists($key, $debugOutput)) {
21
        $debugOutput[$key] = array();
22
    }
23
24
    $debugOutput[$key][] = $value;
25
}
26
27
$input = file_get_contents('php://input');
28
29
if ($input) {
30
    header('Content-Type: application/json');
31
32
    $data = json_decode($input, true);
33
34
    $oldText = $data['oldText'];
35
    $newText = $data['newText'];
36
    $useTableDiffing = isset($data['tableDiffing']) ? $data['tableDiffing'] : true;
37
38
    $diff = new HtmlDiff($oldText, $newText, 'UTF-8', array());
39
    if (array_key_exists('matchThreshold', $data)) {
40
        $diff->setMatchThreshold($data['matchThreshold']);
0 ignored issues
show
Deprecated Code introduced by
The method Caxy\HtmlDiff\AbstractDiff::setMatchThreshold() has been deprecated with message: since 0.1.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
41
    }
42
    $diff->setUseTableDiffing($useTableDiffing);
0 ignored issues
show
Deprecated Code introduced by
The method Caxy\HtmlDiff\HtmlDiff::setUseTableDiffing() has been deprecated with message: since 0.1.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
43
    $diffOutput = $diff->build();
44
    $diffOutput = iconv('UTF-8', 'UTF-8//IGNORE', $diffOutput);
45
46
    $jsonOutput = json_encode(array('diff' => $diffOutput, 'debug' => $debugOutput));
47
48
    if (false === $jsonOutput) {
49
        throw new \Exception('Failed to encode JSON: '.json_last_error_msg());
50
    }
51
52
    echo $jsonOutput;
53
} else {
54
    header('Content-Type: text/html');
55
    echo file_get_contents('demo.html');
56
}
57