Passed
Push — master ( 44c26f...43a260 )
by Alaa
01:53 queued 11s
created

InMemoryTermIdsAcquirer::cleanTerms()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8333
c 0
b 0
f 0
cc 7
nc 15
nop 1
1
<?php
2
3
namespace Wikibase\TermStore\MediaWiki\PackagePrivate;
4
5
class InMemoryTermIdsAcquirer implements TermIdsAcquirer, TermCleaner {
6
7
	private $terms = [];
8
	private $lastId = 0;
9
10
	public function acquireTermIds( array $termsArray ): array {
11
		$ids = [];
12
13
		foreach ( $termsArray as $type => $termsOfType ) {
14
			foreach ( $termsOfType as $lang => $terms ) {
15
				$terms = (array)$terms;
16
17
				foreach ( $terms as $term ) {
18
					if ( !isset( $this->terms[$type][$lang][$term] ) ) {
19
						$this->terms[$type][$lang][$term] = ++$this->lastId;
20
					}
21
22
					$ids[] = $this->terms[$type][$lang][$term];
23
				}
24
			}
25
		}
26
27
		return $ids;
28
	}
29
30
	public function cleanTerms( array $termInLangIds ) {
31
		$termInLangIdsAsKeys = array_flip( $termInLangIds );
32
		foreach ( $this->terms as $type => &$termsOfType ) {
33
			foreach ( $termsOfType as $lang => &$termsOfLang ) {
34
				foreach ( $termsOfLang as $term => $id ) {
35
					if ( array_key_exists( $id, $termInLangIdsAsKeys ) ) {
36
						unset( $termsOfLang[$term] );
37
					}
38
				}
39
				if ( $termsOfLang === [] ) {
40
					unset( $termsOfType[$lang] );
41
				}
42
			}
43
			if ( $termsOfType === [] ) {
44
				unset( $this->terms[$type] );
45
			}
46
		}
47
	}
48
49
}
50