ReferencedEntityPageIterator::key()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Queryr\Replicator\EntitySource;
4
5
use Deserializers\Deserializer;
6
use Deserializers\Exceptions\DeserializationException;
7
use Iterator;
8
use Queryr\Replicator\Model\EntityPage;
9
use Wikibase\DataModel\Entity\EntityDocument;
10
use Wikibase\DataModel\Entity\EntityId;
11
12
/**
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class ReferencedEntityPageIterator implements Iterator {
17
18
	private $innerIterator;
19
	private $fetcher;
20
	private $entityDeserializer;
21
22
	/**
23
	 * @var ReferencedEntitiesFinder
24
	 */
25
	private $referencedEntitiesFinder;
26
27
	/**
28
	 * @var string[]
29
	 */
30
	private $extraEntitiesToFetch = [];
31
32
	public function __construct( Iterator $entityPageIterator, BatchingEntityPageFetcher $fetcher,
33
			Deserializer $entityDeserializer ) {
34
35
		$this->innerIterator = $entityPageIterator;
36
		$this->fetcher = $fetcher;
37
		$this->entityDeserializer = $entityDeserializer;
38
39
		$this->referencedEntitiesFinder = new ReferencedEntitiesFinder();
40
	}
41
42
	/**
43
	 * @return EntityPage|null
44
	 */
45
	public function current() {
46
		/**
47
		 * @var EntityPage|null $page
48
		 */
49
		$page = $this->innerIterator->current();
50
51
		if ( $page !== null ) {
52
			$this->addEntitiesReferencedByPage( $page );
53
		}
54
55
		return $page;
56
	}
57
58
	private function addEntitiesReferencedByPage( EntityPage $page ) {
59
		try {
60
			/**
61
			 * @var EntityDocument $entity
62
			 */
63
			$entity = $this->entityDeserializer->deserialize( @json_decode( $page->getEntityJson(), true ) );
64
		}
65
		catch ( DeserializationException $ex ) {
66
			return;
67
		}
68
69
		if ( !$this->entityWasFetchedDueToReference( $entity->getId() ) ) {
0 ignored issues
show
Bug introduced by
It seems like $entity->getId() can be null; however, entityWasFetchedDueToReference() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
70
			$this->addEntitiesReferencedByEntity( $entity );
71
		}
72
	}
73
74
	private function entityWasFetchedDueToReference( EntityId $entityId ) {
75
		return in_array( $entityId->getSerialization(), $this->extraEntitiesToFetch );
76
	}
77
78
	private function addEntitiesReferencedByEntity( EntityDocument $entity ) {
79
		if ( $entity->getType() !== 'item' ) {
80
			return;
81
		}
82
83
		$referencedEntities = $this->referencedEntitiesFinder->findForItem( $entity );
0 ignored issues
show
Compatibility introduced by
$entity of type object<Wikibase\DataModel\Entity\EntityDocument> is not a sub-type of object<Wikibase\DataModel\Entity\Item>. 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.

Loading history...
84
85
		$extraEntitiesToFetch = [];
86
87
		foreach ( $referencedEntities as $referencedEntityId ) {
88
			$extraEntitiesToFetch[] = $referencedEntityId->getSerialization();
89
		}
90
91
		$this->extraEntitiesToFetch = array_merge( $this->extraEntitiesToFetch, $extraEntitiesToFetch );
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->extra... $extraEntitiesToFetch) of type array is incompatible with the declared type array<integer,string> of property $extraEntitiesToFetch.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
92
93
		$this->fetcher->addPagesToFetch( $extraEntitiesToFetch );
94
95
	}
96
97
	public function next() {
98
		$this->innerIterator->next();
99
	}
100
101
	public function key() {
102
		return $this->innerIterator->key();
103
	}
104
105
	public function valid() {
106
		return $this->innerIterator->valid();
107
	}
108
109
	public function rewind() {
110
		$this->next();
111
		$this->extraEntitiesToFetch = [];
112
	}
113
114
}