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