These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Wikibase\DataModel\Services\Diff; |
||
4 | |||
5 | use Diff\Differ\MapDiffer; |
||
6 | use Diff\DiffOp\DiffOp; |
||
7 | use InvalidArgumentException; |
||
8 | use Wikibase\DataModel\Entity\EntityDocument; |
||
9 | use Wikibase\DataModel\Entity\Property; |
||
10 | use Wikibase\DataModel\Statement\StatementList; |
||
11 | |||
12 | /** |
||
13 | * @since 1.0 |
||
14 | * |
||
15 | * @license GPL-2.0+ |
||
16 | * @author Jeroen De Dauw < [email protected] > |
||
17 | */ |
||
18 | class PropertyDiffer implements EntityDifferStrategy { |
||
19 | |||
20 | /** |
||
21 | * @var MapDiffer |
||
22 | */ |
||
23 | private $recursiveMapDiffer; |
||
24 | |||
25 | /** |
||
26 | * @var StatementListDiffer |
||
27 | */ |
||
28 | private $statementListDiffer; |
||
29 | |||
30 | public function __construct() { |
||
31 | 3 | $this->recursiveMapDiffer = new MapDiffer( true ); |
|
32 | 3 | $this->statementListDiffer = new StatementListDiffer(); |
|
33 | 3 | } |
|
34 | 3 | ||
35 | /** |
||
36 | * @param string $entityType |
||
37 | * |
||
38 | * @return bool |
||
39 | */ |
||
40 | public function canDiffEntityType( $entityType ) { |
||
41 | return $entityType === 'property'; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param EntityDocument $from |
||
46 | * @param EntityDocument $to |
||
47 | * |
||
48 | * @return EntityDiff |
||
49 | * @throws InvalidArgumentException |
||
50 | */ |
||
51 | public function diffEntities( EntityDocument $from, EntityDocument $to ) { |
||
52 | $this->assertIsProperty( $from ); |
||
53 | $this->assertIsProperty( $to ); |
||
54 | |||
55 | return $this->diffProperties( $from, $to ); |
||
0 ignored issues
–
show
$to of type object<Wikibase\DataModel\Entity\EntityDocument> is not a sub-type of object<Wikibase\DataModel\Entity\Property> . It seems like you assume a concrete implementation of the interface Wikibase\DataModel\Entity\EntityDocument to be always present.
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass. Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type. ![]() |
|||
56 | } |
||
57 | |||
58 | private function assertIsProperty( EntityDocument $property ) { |
||
59 | 2 | if ( !( $property instanceof Property ) ) { |
|
60 | 2 | throw new InvalidArgumentException( '$property must be an instance of Property' ); |
|
61 | } |
||
62 | } |
||
63 | 2 | ||
64 | public function diffProperties( Property $from, Property $to ) { |
||
65 | 1 | $diffOps = $this->diffPropertyArrays( |
|
66 | 1 | $this->toDiffArray( $from ), |
|
67 | 1 | $this->toDiffArray( $to ) |
|
68 | 1 | ); |
|
69 | 1 | ||
70 | $diffOps['claim'] = $this->statementListDiffer->getDiff( $from->getStatements(), $to->getStatements() ); |
||
71 | 1 | ||
72 | return new EntityDiff( $diffOps ); |
||
73 | 1 | } |
|
74 | |||
75 | /** |
||
76 | * @param array[] $from |
||
77 | * @param array[] $to |
||
78 | * |
||
79 | * @return DiffOp[] |
||
80 | */ |
||
81 | private function diffPropertyArrays( array $from, array $to ) { |
||
82 | 3 | return $this->recursiveMapDiffer->doDiff( $from, $to ); |
|
83 | 3 | } |
|
84 | |||
85 | /** |
||
86 | * @param Property $property |
||
87 | * |
||
88 | * @return array[] |
||
89 | */ |
||
90 | private function toDiffArray( Property $property ) { |
||
91 | 3 | $array = []; |
|
92 | 3 | ||
93 | $array['aliases'] = $property->getAliasGroups()->toTextArray(); |
||
94 | 3 | $array['label'] = $property->getLabels()->toTextArray(); |
|
95 | 3 | $array['description'] = $property->getDescriptions()->toTextArray(); |
|
96 | 3 | ||
97 | return $array; |
||
98 | 3 | } |
|
99 | |||
100 | /** |
||
101 | * @param EntityDocument $entity |
||
102 | * |
||
103 | * @return EntityDiff |
||
104 | * @throws InvalidArgumentException |
||
105 | */ |
||
106 | public function getConstructionDiff( EntityDocument $entity ) { |
||
107 | 1 | $this->assertIsProperty( $entity ); |
|
108 | 1 | ||
109 | /** @var Property $entity */ |
||
110 | $diffOps = $this->diffPropertyArrays( [], $this->toDiffArray( $entity ) ); |
||
111 | 1 | $diffOps['claim'] = $this->statementListDiffer->getDiff( new StatementList(), $entity->getStatements() ); |
|
112 | 1 | ||
113 | return new EntityDiff( $diffOps ); |
||
114 | 1 | } |
|
115 | |||
116 | /** |
||
117 | * @param EntityDocument $entity |
||
118 | * |
||
119 | * @return EntityDiff |
||
120 | * @throws InvalidArgumentException |
||
121 | */ |
||
122 | public function getDestructionDiff( EntityDocument $entity ) { |
||
123 | 1 | $this->assertIsProperty( $entity ); |
|
124 | 1 | ||
125 | /** @var Property $entity */ |
||
126 | $diffOps = $this->diffPropertyArrays( $this->toDiffArray( $entity ), [] ); |
||
127 | 1 | $diffOps['claim'] = $this->statementListDiffer->getDiff( $entity->getStatements(), new StatementList() ); |
|
128 | 1 | ||
129 | return new EntityDiff( $diffOps ); |
||
130 | 1 | } |
|
131 | |||
132 | } |
||
133 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.