AliasGroup::equals()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5
1
<?php
2
3
namespace Wikibase\DataModel\Term;
4
5
use Countable;
6
use InvalidArgumentException;
7
8
/**
9
 * Ordered set of aliases. Immutable value object.
10
 *
11
 * Duplicates and whitespace only values are removed. Values are trimmed.
12
 *
13
 * @since 0.7.3
14
 *
15
 * @license GPL-2.0-or-later
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class AliasGroup implements Countable {
19
20
	/**
21
	 * @var string Language code identifying the language of the aliases, but note that there is
22
	 * nothing this class can do to enforce this convention.
23
	 */
24
	private $languageCode;
25
26
	/**
27
	 * @var string[]
28
	 */
29
	private $aliases;
30
31
	/**
32
	 * @param string $languageCode Language of the aliases.
33
	 * @param string[] $aliases
34
	 *
35
	 * @throws InvalidArgumentException
36
	 */
37
	public function __construct( $languageCode, array $aliases = [] ) {
38 15
		if ( !is_string( $languageCode ) || $languageCode === '' ) {
39 15
			throw new InvalidArgumentException( '$languageCode must be a non-empty string' );
40 3
		}
41
42
		$this->languageCode = $languageCode;
43 12
		$this->aliases = array_values(
0 ignored issues
show
Documentation Bug introduced by
It seems like array_values(array_uniqu...m($alias) !== ''; })))) of type array<integer,?> is incompatible with the declared type array<integer,string> of property $aliases.

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...
44 12
			array_unique(
45 12
				array_map(
46 12
					'trim',
47 12
					array_filter(
48 12
						$aliases,
49 12
						function( $alias ) {
50 11
							if ( !is_string( $alias ) ) {
51 11
								throw new InvalidArgumentException( '$aliases must be an array of strings' );
52 1
							}
53
54
							return trim( $alias ) !== '';
55 10
						}
56
					)
57 12
				)
58 11
			)
59 11
		);
60 11
	}
61 11
62
	/**
63
	 * @return string
64
	 */
65
	public function getLanguageCode() {
66 1
		return $this->languageCode;
67 1
	}
68
69
	/**
70
	 * @return string[]
71
	 */
72
	public function getAliases() {
73 1
		return $this->aliases;
74 1
	}
75
76
	/**
77
	 * @return bool
78
	 */
79
	public function isEmpty() {
80 1
		return empty( $this->aliases );
81 1
	}
82
83
	/**
84
	 *
85
	 * @param mixed $target
86
	 *
87
	 * @return bool
88
	 */
89
	public function equals( $target ) {
90
		if ( $this === $target ) {
91 5
			return true;
92 5
		}
93 1
94
		return is_object( $target )
95
			&& get_called_class() === get_class( $target )
96 5
			&& $this->languageCode === $target->languageCode
97 5
			&& $this->aliases == $target->aliases;
98 5
	}
99 5
100
	/**
101
	 * @see Countable::count
102
	 *
103
	 * @return int
104
	 */
105
	public function count() {
106
		return count( $this->aliases );
107 1
	}
108 1
109
}
110