1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Context; |
4
|
|
|
|
5
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
6
|
|
|
use Wikibase\DataModel\Reference; |
7
|
|
|
use Wikibase\DataModel\Snak\Snak; |
8
|
|
|
use Wikibase\DataModel\Statement\Statement; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A constraint check context for a snak of a reference of a statement. |
12
|
|
|
* |
13
|
|
|
* @license GNU GPL v2+ |
14
|
|
|
*/ |
15
|
|
|
class ReferenceContext extends ApiV2Context { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Statement |
19
|
|
|
*/ |
20
|
|
|
private $statement; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Reference |
24
|
|
|
*/ |
25
|
|
|
private $reference; |
26
|
|
|
|
27
|
|
|
public function __construct( |
28
|
|
|
EntityDocument $entity, |
29
|
|
|
Statement $statement, |
30
|
|
|
Reference $reference, |
31
|
|
|
Snak $snak |
32
|
|
|
) { |
33
|
|
|
parent::__construct( $entity, $snak ); |
34
|
|
|
$this->statement = $statement; |
35
|
|
|
$this->reference = $reference; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getType() { |
39
|
|
|
return self::TYPE_REFERENCE; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getSnakGroup() { |
43
|
|
|
$snaks = $this->reference->getSnaks(); |
44
|
|
|
return array_values( $snaks->getArrayCopy() ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public function getCursor() { |
|
|
|
|
48
|
|
|
return new ReferenceContextCursor( |
49
|
|
|
$this->entity->getId()->getSerialization(), |
50
|
|
|
$this->statement->getPropertyId()->getSerialization(), |
51
|
|
|
$this->statement->getGuid(), |
52
|
|
|
$this->snak->getHash(), |
53
|
|
|
$this->snak->getPropertyId()->getSerialization(), |
54
|
|
|
$this->reference->getHash() |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function &getMainArray( array &$container ) { |
59
|
|
|
$statementArray = &$this->getStatementArray( |
60
|
|
|
$container, |
61
|
|
|
$this->entity->getId()->getSerialization(), |
62
|
|
|
$this->statement->getPropertyId()->getSerialization(), |
63
|
|
|
$this->statement->getGuid() |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
if ( !array_key_exists( 'references', $statementArray ) ) { |
67
|
|
|
$statementArray['references'] = []; |
68
|
|
|
} |
69
|
|
|
$referencesArray = &$statementArray['references']; |
70
|
|
|
|
71
|
|
|
foreach ( $referencesArray as &$potentialReferenceArray ) { |
72
|
|
|
if ( $potentialReferenceArray['hash'] === $this->reference->getHash() ) { |
73
|
|
|
$referenceArray = &$potentialReferenceArray; |
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
if ( !isset( $referenceArray ) ) { |
78
|
|
|
$referenceArray = [ 'hash' => $this->reference->getHash(), 'snaks' => [] ]; |
79
|
|
|
$referencesArray[] = &$referenceArray; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$snaksArray = &$referenceArray['snaks']; |
83
|
|
|
|
84
|
|
|
$propertyId = $this->getSnak()->getPropertyId()->getSerialization(); |
85
|
|
|
if ( !array_key_exists( $propertyId, $snaksArray ) ) { |
86
|
|
|
$snaksArray[$propertyId] = []; |
87
|
|
|
} |
88
|
|
|
$propertyArray = &$snaksArray[$propertyId]; |
89
|
|
|
|
90
|
|
|
foreach ( $propertyArray as &$potentialSnakArray ) { |
91
|
|
|
if ( $potentialSnakArray['hash'] === $this->getSnak()->getHash() ) { |
92
|
|
|
$snakArray = &$potentialSnakArray; |
93
|
|
|
break; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
View Code Duplication |
if ( !isset( $snakArray ) ) { |
|
|
|
|
97
|
|
|
$snakArray = [ 'hash' => $this->getSnak()->getHash() ]; |
98
|
|
|
$propertyArray[] = &$snakArray; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $snakArray; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.