Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
41.46% |
17 / 41 |
|
33.33% |
3 / 9 |
CRAP | |
0.00% |
0 / 1 |
FormDiffer | |
41.46% |
17 / 41 |
|
33.33% |
3 / 9 |
35.27 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
canDiffEntityType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
diffEntities | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
getConstructionDiff | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getDestructionDiff | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
diff | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
getAddFormDiff | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
getRemoveFormDiff | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
toFormDiffArray | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Wikibase\Lexeme\Domain\Diff; |
4 | |
5 | use Diff\Differ\MapDiffer; |
6 | use Diff\DiffOp\Diff\Diff; |
7 | use DomainException; |
8 | use InvalidArgumentException; |
9 | use Wikibase\DataModel\Entity\EntityDocument; |
10 | use Wikibase\DataModel\Services\Diff\EntityDiff; |
11 | use Wikibase\DataModel\Services\Diff\EntityDifferStrategy; |
12 | use Wikibase\DataModel\Services\Diff\StatementListDiffer; |
13 | use Wikibase\DataModel\Statement\StatementList; |
14 | use Wikibase\Lexeme\Domain\Model\Form; |
15 | |
16 | /** |
17 | * @license GPL-2.0-or-later |
18 | * @phan-file-suppress PhanPluginNeverReturnMethod |
19 | */ |
20 | class FormDiffer 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 === Form::ENTITY_TYPE; |
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 Form ) || !( $to instanceof Form ) ) { |
55 | throw new InvalidArgumentException( 'Can only diff Forms' ); |
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( 'Forms aren\'t stored as separate wiki pages, and can only show ' |
69 | . 'up in regular diffs that add or remove a Form' ); |
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( 'Forms aren\'t stored as separate wiki pages, and can only show ' |
80 | . 'up in regular diffs that add or remove a Form' ); |
81 | } |
82 | |
83 | /** |
84 | * @param Form $old |
85 | * @param Form $new |
86 | * |
87 | * @return ChangeFormDiffOp |
88 | */ |
89 | public function diff( Form $old, Form $new ) { |
90 | // TODO: Assert same ID |
91 | $diffOps = $this->recursiveMapDiffer->doDiff( |
92 | $this->toFormDiffArray( $old ), |
93 | $this->toFormDiffArray( $new ) |
94 | ); |
95 | |
96 | $diffOps['claim'] = $this->statementListDiffer->getDiff( |
97 | $old->getStatements(), |
98 | $new->getStatements() |
99 | ); |
100 | |
101 | return new ChangeFormDiffOp( $old->getId(), new Diff( $diffOps ) ); |
102 | } |
103 | |
104 | public function getAddFormDiff( Form $form ) { |
105 | $diffOps = $this->recursiveMapDiffer->doDiff( |
106 | [], |
107 | $this->toFormDiffArray( $form ) |
108 | ); |
109 | $diffOps['claim'] = $this->statementListDiffer->getDiff( |
110 | new StatementList(), |
111 | $form->getStatements() |
112 | ); |
113 | |
114 | return new AddFormDiff( $form, new Diff( $diffOps ) ); |
115 | } |
116 | |
117 | public function getRemoveFormDiff( Form $form ) { |
118 | $diffOps = $this->recursiveMapDiffer->doDiff( |
119 | $this->toFormDiffArray( $form ), |
120 | [] |
121 | ); |
122 | $diffOps['claim'] = $this->statementListDiffer->getDiff( |
123 | $form->getStatements(), |
124 | new StatementList() |
125 | ); |
126 | |
127 | return new RemoveFormDiff( $form->getId(), new Diff( $diffOps ) ); |
128 | } |
129 | |
130 | /** |
131 | * @param Form $form |
132 | * |
133 | * @return array |
134 | */ |
135 | private function toFormDiffArray( Form $form ) { |
136 | $result = []; |
137 | $result['representations'] = $form->getRepresentations()->toTextArray(); |
138 | $result['grammaticalFeatures'] = $form->getGrammaticalFeatures(); |
139 | |
140 | return $result; |
141 | } |
142 | |
143 | } |