Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.50% covered (warning)
72.50%
29 / 40
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SenseDiffView
72.50% covered (warning)
72.50%
29 / 40
75.00% covered (warning)
75.00%
3 / 4
14.99
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 generateOpHtml
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 generateSenseOpHtml
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
2
 getClaimDiffHtml
21.43% covered (danger)
21.43%
3 / 14
0.00% covered (danger)
0.00%
0 / 1
17.13
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 InvalidArgumentException;
11use MessageLocalizer;
12use Wikibase\Lexeme\Domain\Diff\SenseDiff;
13use Wikibase\Repo\Diff\BasicDiffView;
14use Wikibase\Repo\Diff\ClaimDiffer;
15use Wikibase\Repo\Diff\ClaimDifferenceVisualizer;
16
17/**
18 * Class for generating views of DiffOp objects of senses.
19 *
20 * @license GPL-2.0-or-later
21 */
22class SenseDiffView extends BasicDiffView {
23
24    /**
25     * @var ClaimDiffer
26     */
27    private $claimDiffer;
28
29    /**
30     * @var ClaimDifferenceVisualizer
31     */
32    private $claimDiffVisualizer;
33
34    /**
35     * @var MessageLocalizer
36     */
37    private $messageLocalizer;
38
39    /**
40     * @param string[] $path
41     * @param Diff $diff
42     * @param ClaimDiffer $claimDiffer
43     * @param ClaimDifferenceVisualizer $claimDiffVisualizer
44     * @param MessageLocalizer $messageLocalizer
45     */
46    public function __construct(
47        array $path,
48        Diff $diff,
49        ClaimDiffer $claimDiffer,
50        ClaimDifferenceVisualizer $claimDiffVisualizer,
51        MessageLocalizer $messageLocalizer
52    ) {
53        parent::__construct( $path, $diff );
54
55        $this->claimDiffer = $claimDiffer;
56        $this->claimDiffVisualizer = $claimDiffVisualizer;
57        $this->messageLocalizer = $messageLocalizer;
58    }
59
60    /**
61     * @param string[] $path
62     * @param DiffOp $op
63     *
64     * @return string HTML
65     */
66    protected function generateOpHtml( array $path, DiffOp $op ) {
67        if ( $op->isAtomic() ) {
68            return parent::generateOpHtml( $path, $op );
69        }
70
71        $html = '';
72
73        // @phan-suppress-next-line PhanTypeNoPropertiesForeach
74        foreach ( $op as $key => $subOp ) {
75            if ( $subOp instanceof SenseDiff ) {
76                $html .= $this->generateSenseOpHtml( $path, $subOp, $key );
77            } else {
78                $html .= $this->generateOpHtml( array_merge( $path, [ $key ] ), $subOp );
79            }
80        }
81
82        return $html;
83    }
84
85    private function generateSenseOpHtml( array $path, SenseDiff $op, $key ) {
86        $html = '';
87
88        $html .= parent::generateOpHtml(
89            array_merge(
90                $path,
91                [ $key, $this->messageLocalizer->msg( 'wikibaselexeme-diffview-gloss' )->text() ]
92            ),
93            $op->getGlossesDiff()
94        );
95
96        foreach ( $op->getStatementsDiff() as $claimDiffOp ) {
97            $html .= $this->getClaimDiffHtml(
98                $claimDiffOp,
99                array_merge( $path, [ $key ] )
100            );
101        }
102
103        return $html;
104    }
105
106    /**
107     * @param DiffOp $diffOp
108     *
109     * @return string HTML
110     */
111    private function getClaimDiffHtml( DiffOp $diffOp, array $path ) {
112        switch ( true ) {
113            case $diffOp instanceof DiffOpChange:
114                return $this->claimDiffVisualizer->visualizeClaimChange(
115                    $this->claimDiffer->diffClaims(
116                        $diffOp->getOldValue(),
117                        $diffOp->getNewValue()
118                    ),
119                    $diffOp->getNewValue(),
120                    $path
121                );
122
123            case $diffOp instanceof DiffOpAdd:
124                return $this->claimDiffVisualizer->visualizeNewClaim( $diffOp->getNewValue(), $path );
125
126            case $diffOp instanceof DiffOpRemove:
127                return $this->claimDiffVisualizer->visualizeRemovedClaim( $diffOp->getOldValue(), $path );
128
129            default:
130                throw new InvalidArgumentException( 'Encountered an unexpected diff operation type for a claim' );
131        }
132    }
133
134}