Passed
Push — master ( d5f4f5...7ecb6b )
by Alaa
01:40
created

InMemoryTypeIdsStore::resolveTypeIds()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Wikibase\TermStore\MediaWiki\PackagePrivate;
4
5
/**
6
 * Acquires and resolves unique and constant ids of types, stored in memory.
7
 */
8
class InMemoryTypeIdsStore implements TypeIdsAcquirer, TypeIdsResolver {
9
	private $types = [];
10
	private $lastId = 0;
11
12
	public function acquireTypeIds( $types ) {
13
		$ids = [];
14
		foreach ( $types as $type ) {
15
			if ( !isset( $this->types[$type] ) ) {
16
				$this->types[$type] = ++$this->lastId;
17
			}
18
			$ids[$type] = $this->types[$type];
19
		}
20
21
		return $ids;
22
	}
23
24
	public function resolveTypeIds( array $typeIds ): array {
25
		$types = [];
26
		foreach ( $this->types as $typeName => $typeId ) {
27
			if ( in_array( $typeId, $typeIds ) ) {
28
				$types[$typeId] = $typeName;
29
			}
30
		}
31
		return $types;
32
	}
33
}
34