Passed
Push — master ( 48c70a...6b0fb0 )
by Josh
02:34
created

index.php ➔ addDebugOutput()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 2
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 12 and the first side effect is on line 5.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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 function Caxy\HtmlDiff\AbstractDiff::setMatchThreshold() has been deprecated: since 0.1.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

40
        /** @scrutinizer ignore-deprecated */ $diff->setMatchThreshold($data['matchThreshold']);

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

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

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

42
    /** @scrutinizer ignore-deprecated */ $diff->setUseTableDiffing($useTableDiffing);

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

The explanatory message should give you some clue as to whether and when the function will be removed and what other function 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