Test Failed
Pull Request — master (#11)
by Alaa
02:11 queued 10s
created

ReplicaMasterAwareRecordIdsAcquirer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 126
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A acquireIds() 0 15 2
A findExistingRecords() 0 25 3
A insertNonExistingRecords() 0 22 4
A filterNonExistingRecords() 0 19 4
A calcRecordHash() 0 4 1
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\DBQueryError;
10
use Wikimedia\Rdbms\IDatabase;
11
12
class ReplicaMasterAwareRecordIdsAcquirer {
13
14
	/**
15
	 * @var Database $dbw
16
	 */
17
	private $dbw;
18
19
	/**
20
	 * @var Database $dbr
21
	 */
22
	private $dbr;
23
24
	/** @var string $table */
25
	private $table;
26
27
	/** @var string $idColumn */
28
	private $idColumn;
29
30
	/** @var LoggerInterface $logger */
31
	private $logger;
32
33
	public function __construct(
34
		IDatabase $dbw,
35
		IDatabase $dbr,
36
		string $table,
37
		string $idColumn,
38
		LoggerInterface $logger = null
39
	) {
40
		$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...
41
		$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...
42
		$this->table = $table;
43
		$this->idColumn = $idColumn;
44
		$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...
45
	}
46
47
	public function acquireIds( array $neededRecords ) {
48
		$existingRecords = $this->findExistingRecords( $this->dbr, $neededRecords );
49
		$neededRecords = $this->filterNonExistingRecords( $neededRecords, $existingRecords );
50
51
		while ( !empty( $neededRecords ) ) {
52
			$this->insertNonExistingRecords( $this->dbw, $neededRecords );
53
			$existingRecords = array_merge(
54
				$existingRecords,
55
				$this->findExistingRecords( $this->dbw, $neededRecords )
56
			);
57
			$neededRecords = $this->filterNonExistingRecords( $neededRecords, $existingRecords );
58
		}
59
60
		return $existingRecords;
61
	}
62
63
	private function findExistingRecords( IDatabase $dbr, array $neededRecords ) {
64
		$recordsSelectConditions = array_map( function ( $record ) use ( $dbr ) {
65
			return $dbr->makeList( $record, IDatabase::LIST_AND );
66
		}, $neededRecords );
67
68
		$selectColumns = array_keys( $neededRecords[0] );
69
		$selectColumns[] = $this->idColumn;
70
71
		$existingRows = $dbr->select(
72
			$this->table,
73
			$selectColumns,
74
			$dbr->makeList( $recordsSelectConditions, IDatabase::LIST_OR )
75
		);
76
77
		$existingRecords = [];
78
		foreach ( $existingRows as $row ) {
79
			$existingRecord = [];
80
			foreach ( $selectColumns as $column ) {
81
				$existingRecord[$column] = $row->$column;
82
			}
83
			$existingRecords[] = $existingRecord;
84
		}
85
86
		return $existingRecords;
87
	}
88
89
	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...
90
		$uniqueRecords = [];
91
		foreach ( $neededRecords as $record ) {
92
			$recordHash = $this->calcRecordHash( $record );
93
			if ( !isset( $uniqueRecords[$recordHash] ) ) {
94
				$uniqueRecords[$recordHash] = $record;
95
			}
96
		}
97
98
		try {
99
			$this->dbw->insert( $this->table, array_values( $uniqueRecords ) );
100
		} catch ( DBQueryError $dbError ) {
0 ignored issues
show
Bug introduced by
The class Wikimedia\Rdbms\DBQueryError does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
101
			$this->logger->info(
102
				'{method}: Inserting records {records} into {table} failed. {exception}',
103
				[
104
					'exception' => $dbError,
105
					'table' => $this->table,
106
					'records' => $uniqueRecords
107
				]
108
			);
109
		}
110
	}
111
112
	private function filterNonExistingRecords( $neededRecords, $existingRecords ) {
113
		$existingRecordsHashes = [];
114
		foreach ( $existingRecords as $record ) {
115
			unset( $record[$this->idColumn] );
116
			$recordHash = $this->calcRecordHash( $record );
117
			$existingRecordsHashes[$recordHash] = true;
118
		}
119
120
		$nonExistingRecords = [];
121
		foreach ( $neededRecords as $record ) {
122
			$recordHash = $this->calcRecordHash( $record );
123
124
			if ( !isset( $existingRecordsHashes[$recordHash] ) ) {
125
				$nonExistingRecords[] = $record;
126
			}
127
		}
128
129
		return $nonExistingRecords;
130
	}
131
132
	private function calcRecordHash( $record ) {
133
		ksort( $record );
134
		return md5( serialize( $record ) );
135
	}
136
137
}
138