Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.88% covered (success)
96.88%
31 / 32
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ItemReferenceDifferenceVisualizer
96.88% covered (success)
96.88%
31 / 32
66.67% covered (warning)
66.67%
2 / 3
8
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 visualize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 visualizeDifference
96.67% covered (success)
96.67%
29 / 30
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Wikibase\Lexeme\Presentation\Diff;
4
5use Diff\DiffOp\Diff\Diff;
6use Diff\DiffOp\DiffOp;
7use Diff\DiffOp\DiffOpAdd;
8use Diff\DiffOp\DiffOpChange;
9use Diff\DiffOp\DiffOpRemove;
10use Wikibase\DataModel\Services\EntityId\EntityIdFormatter;
11use Wikibase\Repo\Diff\DiffOpValueFormatter;
12
13/**
14 * @license GPL-2.0-or-later
15 */
16class ItemReferenceDifferenceVisualizer {
17
18    /**
19     * @var EntityIdFormatter
20     */
21    private $idFormatter;
22
23    public function __construct( EntityIdFormatter $idFormatter ) {
24        $this->idFormatter = $idFormatter;
25    }
26
27    public function visualize( $headerText, Diff $diff ) {
28        return $this->visualizeDifference( $headerText, $diff );
29    }
30
31    private function visualizeDifference( $headerText, DiffOp $diff ) {
32        if ( !$diff->isAtomic() ) {
33            $html = '';
34            // @phan-suppress-next-line PhanTypeNoPropertiesForeach
35            foreach ( $diff as $op ) {
36                $html .= $this->visualizeDifference( $headerText, $op );
37            }
38            return $html;
39        }
40
41        if ( $diff instanceof DiffOpChange ) {
42            $valueFormatter = new DiffOpValueFormatter(
43                $headerText,
44                $headerText,
45                $this->idFormatter->formatEntityId( $diff->getOldValue() ),
46                $this->idFormatter->formatEntityId( $diff->getNewValue() )
47            );
48            return $valueFormatter->generateHtml();
49        }
50        if ( $diff instanceof DiffOpAdd ) {
51            $valueFormatter = new DiffOpValueFormatter(
52                '',
53                $headerText,
54                null,
55                $this->idFormatter->formatEntityId( $diff->getNewValue() )
56            );
57            return $valueFormatter->generateHtml();
58        }
59        if ( $diff instanceof DiffOpRemove ) {
60            $valueFormatter = new DiffOpValueFormatter(
61                $headerText,
62                '',
63                $this->idFormatter->formatEntityId( $diff->getOldValue() ),
64                null
65            );
66            return $valueFormatter->generateHtml();
67        }
68
69        return '';
70    }
71
72}