Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
40.00% covered (danger)
40.00%
16 / 40
33.33% covered (danger)
33.33%
3 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
SenseDiffer
40.00% covered (danger)
40.00%
16 / 40
33.33% covered (danger)
33.33%
3 / 9
37.14
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 canDiffEntityType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 diffEntities
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
 getConstructionDiff
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getDestructionDiff
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 diff
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 getAddSenseDiff
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getRemoveSenseDiff
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 toSenseDiffArray
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikibase\Lexeme\Domain\Diff;
4
5use Diff\Differ\MapDiffer;
6use Diff\DiffOp\Diff\Diff;
7use DomainException;
8use InvalidArgumentException;
9use Wikibase\DataModel\Entity\EntityDocument;
10use Wikibase\DataModel\Services\Diff\EntityDiff;
11use Wikibase\DataModel\Services\Diff\EntityDifferStrategy;
12use Wikibase\DataModel\Services\Diff\StatementListDiffer;
13use Wikibase\DataModel\Statement\StatementList;
14use Wikibase\Lexeme\Domain\Model\Sense;
15
16/**
17 * @license GPL-2.0-or-later
18 * @phan-file-suppress PhanPluginNeverReturnMethod
19 */
20class SenseDiffer implements EntityDifferStrategy {
21
22    /**
23     * @var MapDiffer
24     */
25    private $recursiveMapDiffer;
26
27    /**
28     * @var StatementListDiffer
29     */
30    private $statementListDiffer;
31
32    public function __construct() {
33        $this->recursiveMapDiffer = new MapDiffer( true );
34        $this->statementListDiffer = new StatementListDiffer();
35    }
36
37    /**
38     * @param string $entityType
39     *
40     * @return bool
41     */
42    public function canDiffEntityType( $entityType ) {
43        return $entityType === 'sense';
44    }
45
46    /**
47     * @param EntityDocument $from
48     * @param EntityDocument $to
49     *
50     * @return EntityDiff
51     * @throws InvalidArgumentException
52     */
53    public function diffEntities( EntityDocument $from, EntityDocument $to ) {
54        if ( !( $from instanceof Sense ) || !( $to instanceof Sense ) ) {
55            throw new InvalidArgumentException( 'Can only diff Senses' );
56        }
57
58        return $this->diff( $from, $to );
59    }
60
61    /**
62     * @param EntityDocument $entity
63     *
64     * @return EntityDiff
65     * @throws InvalidArgumentException
66     */
67    public function getConstructionDiff( EntityDocument $entity ) {
68        throw new DomainException( 'Sense aren\'t stored as separate wiki pages, and can only show '
69            . 'up in regular diffs that add or remove a Sense' );
70    }
71
72    /**
73     * @param EntityDocument $entity
74     *
75     * @return EntityDiff
76     * @throws InvalidArgumentException
77     */
78    public function getDestructionDiff( EntityDocument $entity ) {
79        throw new DomainException( 'Senses aren\'t stored as separate wiki pages, and can only show '
80            . 'up in regular diffs that add or remove a Sense' );
81    }
82
83    /**
84     * @param Sense $old
85     * @param Sense $new
86     *
87     * @return ChangeSenseDiffOp
88     */
89    private function diff( Sense $old, Sense $new ) {
90        // TODO: Assert same ID
91        $diffOps = $this->recursiveMapDiffer->doDiff(
92            $this->toSenseDiffArray( $old ),
93            $this->toSenseDiffArray( $new )
94        );
95
96        $diffOps['claim'] = $this->statementListDiffer->getDiff(
97            $old->getStatements(),
98            $new->getStatements()
99        );
100
101        return new ChangeSenseDiffOp( $old->getId(), new Diff( $diffOps ) );
102    }
103
104    public function getAddSenseDiff( Sense $sense ) {
105        $diffOps = $this->recursiveMapDiffer->doDiff(
106            [],
107            $this->toSenseDiffArray( $sense )
108        );
109        $diffOps['claim'] = $this->statementListDiffer->getDiff(
110            new StatementList(),
111            $sense->getStatements()
112        );
113
114        return new AddSenseDiff( $sense, new Diff( $diffOps ) );
115    }
116
117    public function getRemoveSenseDiff( Sense $sense ) {
118        $diffOps = $this->recursiveMapDiffer->doDiff(
119            $this->toSenseDiffArray( $sense ),
120            []
121        );
122        $diffOps['claim'] = $this->statementListDiffer->getDiff(
123            $sense->getStatements(),
124            new StatementList()
125        );
126
127        return new RemoveSenseDiff( $sense->getId(), new Diff( $diffOps ) );
128    }
129
130    /**
131     * @param Sense $sense
132     *
133     * @return string[][]
134     */
135    private function toSenseDiffArray( Sense $sense ) {
136        $result = [];
137        $result['glosses'] = $sense->getGlosses()->toTextArray();
138
139        return $result;
140    }
141
142}