Completed
Push — master ( e48e23...d723f6 )
by
unknown
02:34
created

ReferenceContext::getCursor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
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() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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 ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
97
			$snakArray = [ 'hash' => $this->getSnak()->getHash() ];
98
			$propertyArray[] = &$snakArray;
99
		}
100
101
		return $snakArray;
102
	}
103
104
}
105