Test Failed
Pull Request — master (#11)
by Alaa
02:35
created

ReplicaMasterAwareRecordIdsAcquirer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 5
1
<?php
2
3
namespace Wikibase\TermStore\MediaWiki\PackagePrivate\Util;
4
5
use AppendIterator;
6
use ArrayIterator;
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\NullLogger;
9
use Wikimedia\Rdbms\IDatabase;
10
11
class ReplicaMasterAwareRecordIdsAcquirer {
12
13
	/**
14
	 * @var Database $dbw
15
	 */
16
	private $dbw;
17
18
	/**
19
	 * @var Database $dbr
20
	 */
21
	private $dbr;
22
23
	/** @var string $table */
24
	private $table;
25
26
	/** @var string $idColumn */
27
	private $idColumn;
28
29
	/** @var LoggerInterface $logger */
30
	private $logger;
31
32
	public function __construct(
33
		IDatabase $dbw,
34
		IDatabase $dbr,
35
		string $table,
36
		string $idColumn,
37
		LoggerInterface $logger = null
38
	) {
39
		$this->dbw = $dbw;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dbw of type object<Wikimedia\Rdbms\IDatabase> is incompatible with the declared type object<Wikibase\TermStor...ePrivate\Util\Database> of property $dbw.

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...
40
		$this->dbr = $dbr;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dbr of type object<Wikimedia\Rdbms\IDatabase> is incompatible with the declared type object<Wikibase\TermStor...ePrivate\Util\Database> of property $dbr.

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...
41
		$this->table = $table;
42
		$this->idColumn = $idColumn;
43
		$this->logger = $logger ?? new NullLogger();
0 ignored issues
show
Documentation Bug introduced by
It seems like $logger ?? new \Psr\Log\NullLogger() can also be of type object<Psr\Log\NullLogger>. However, the property $logger is declared as type object<Psr\Log\LoggerInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
44
	}
45
46
	public function acquireIds( array $neededRecords ) {
47
		$existingRecords = $this->findExistingRecords( $this->dbr, $neededRecords );
48
		$neededRecords = $this->filterNonExistingRecords( $neededRecords, $existingRecords );
49
50
		while ( !empty( $neededRecords ) ) {
51
			$this->insertNonExistingRecords( $this->dbw, $neededRecords );
52
			$existingRecords = array_merge(
53
				$existingRecords,
54
				$this->findExistingRecords( $this->dbw, $neededRecords )
55
			);
56
			$neededRecords = $this->filterNonExistingRecords( $neededRecords, $existingRecords );
57
		}
58
59
		return $existingRecords;
60
	}
61
62
	private function findExistingRecords( IDatabase $dbr, array $neededRecords ) {
63
		$recordsSelectConditions = array_map( function ( $record ) use ( $dbr ) {
64
			return $dbr->makeList( $record, IDatabase::LIST_AND );
65
		}, $neededRecords );
66
67
		$selectColumns = array_keys( $neededRecords[0] );
68
		$selectColumns[] = $this->idColumn;
69
70
		$existingRows = $dbr->select(
71
			$this->table,
72
			$selectColumns,
73
			$dbr->makeList( $recordsSelectConditions, IDatabase::LIST_OR )
74
		);
75
76
		$existingRecords = [];
77
		foreach ( $existingRows as $row ) {
78
			$existingRecord = [];
79
			foreach ( $selectColumns as $column ) {
80
				$existingRecord[$column] = $row->$column;
81
			}
82
			$existingRecords[] = $existingRecord;
83
		}
84
85
		return $existingRecords;
86
	}
87
88
	private function insertNonExistingRecords( IDatabase $dbw, array $neededRecords ) {
0 ignored issues
show
Unused Code introduced by
The parameter $dbw is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
		$uniqueRecords = [];
90
		foreach ( $neededRecords as $record ) {
91
			$recordHash = $this->calcRecordHash( $record );
92
			if ( !isset( $uniqueRecords[$recordHash] ) ) {
93
				$uniqueRecords[$recordHash] = $record;
94
			}
95
		}
96
97
		$this->dbw->insert( $this->table, array_values( $uniqueRecords ) );
98
	}
99
100
	private function filterNonExistingRecords( $neededRecords, $existingRecords ) {
101
		$existingRecordsHashes = [];
102
		foreach ( $existingRecords as $record ) {
103
			unset( $record[$this->idColumn] );
104
			$recordHash = $this->calcRecordHash( $record );
105
			$existingRecordsHashes[$recordHash] = true;
106
		}
107
108
		$nonExistingRecords = [];
109
		foreach ( $neededRecords as $record ) {
110
			$recordHash = $this->calcRecordHash( $record );
111
112
			if ( !isset( $existingRecordsHashes[$recordHash] ) ) {
113
				$nonExistingRecords[] = $record;
114
			}
115
		}
116
117
		return $nonExistingRecords;
118
	}
119
120
	private function calcRecordHash( $record ) {
121
		ksort( $record );
122
		return md5( serialize( $record ) );
123
	}
124
125
}
126