Skip to content

Commit 651c9c6

Browse files
lucaswerkmeisterWMDE bot
authored andcommitted
Use strict types for TermList, AliasGroupList
Bug: T423185 Change-Id: I85984edcddff91baed8cc9fa3302586e7ce072ab
1 parent ed97ff9 commit 651c9c6

4 files changed

Lines changed: 49 additions & 143 deletions

File tree

src/Term/AliasGroupList.php

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace Wikibase\DataModel\Term;
46

57
use ArrayIterator;
@@ -26,7 +28,7 @@ class AliasGroupList implements Countable, IteratorAggregate {
2628
/**
2729
* @var AliasGroup[]
2830
*/
29-
private $groups = [];
31+
private array $groups = [];
3032

3133
/**
3234
* @param AliasGroup[] $aliasGroups
@@ -44,7 +46,6 @@ public function __construct( array $aliasGroups = [] ) {
4446

4547
/**
4648
* @see Countable::count
47-
* @return int
4849
*/
4950
public function count(): int {
5051
return count( $this->groups );
@@ -65,17 +66,14 @@ public function getIterator(): Traversable {
6566
*
6667
* @return AliasGroup[] Array indexed by language code.
6768
*/
68-
public function toArray() {
69+
public function toArray(): array {
6970
return $this->groups;
7071
}
7172

7273
/**
73-
* @param string $languageCode
74-
*
75-
* @return AliasGroup
7674
* @throws OutOfBoundsException
7775
*/
78-
public function getByLanguage( $languageCode ) {
76+
public function getByLanguage( string $languageCode ): AliasGroup {
7977
if ( !array_key_exists( $languageCode, $this->groups ) ) {
8078
throw new OutOfBoundsException( 'AliasGroup with languageCode "' . $languageCode . '" not found' );
8179
}
@@ -87,25 +85,20 @@ public function getByLanguage( $languageCode ) {
8785
* @since 2.5
8886
*
8987
* @param string[] $languageCodes
90-
*
91-
* @return self
9288
*/
93-
public function getWithLanguages( array $languageCodes ) {
89+
public function getWithLanguages( array $languageCodes ): self {
9490
return new self( array_intersect_key( $this->groups, array_flip( $languageCodes ) ) );
9591
}
9692

97-
/**
98-
* @param string $languageCode
99-
*/
100-
public function removeByLanguage( $languageCode ) {
93+
public function removeByLanguage( string $languageCode ): void {
10194
unset( $this->groups[$languageCode] );
10295
}
10396

10497
/**
10598
* If the group is empty, it will not be stored.
10699
* In case the language of that group had an associated group, that group will be removed.
107100
*/
108-
public function setGroup( AliasGroup $group ) {
101+
public function setGroup( AliasGroup $group ): void {
109102
if ( $group->isEmpty() ) {
110103
unset( $this->groups[$group->getLanguageCode()] );
111104
} else {
@@ -115,12 +108,8 @@ public function setGroup( AliasGroup $group ) {
115108

116109
/**
117110
* @since 0.7.4
118-
*
119-
* @param mixed $target
120-
*
121-
* @return bool
122111
*/
123-
public function equals( $target ) {
112+
public function equals( mixed $target ): bool {
124113
if ( $this === $target ) {
125114
return true;
126115
}
@@ -142,33 +131,23 @@ public function equals( $target ) {
142131

143132
/**
144133
* @since 2.4.0
145-
*
146-
* @return bool
147134
*/
148-
public function isEmpty() {
135+
public function isEmpty(): bool {
149136
return $this->groups === [];
150137
}
151138

152139
/**
153140
* @since 0.7.4
154-
*
155-
* @param AliasGroup $group
156-
*
157-
* @return bool
158141
*/
159-
public function hasAliasGroup( AliasGroup $group ) {
142+
public function hasAliasGroup( AliasGroup $group ): bool {
160143
return array_key_exists( $group->getLanguageCode(), $this->groups )
161144
&& $this->groups[$group->getLanguageCode()]->equals( $group );
162145
}
163146

164147
/**
165148
* @since 0.8
166-
*
167-
* @param string $languageCode
168-
*
169-
* @return bool
170149
*/
171-
public function hasGroupForLanguage( $languageCode ) {
150+
public function hasGroupForLanguage( string $languageCode ): bool {
172151
return array_key_exists( $languageCode, $this->groups );
173152
}
174153

@@ -178,7 +157,7 @@ public function hasGroupForLanguage( $languageCode ) {
178157
* @param string $languageCode
179158
* @param string[] $aliases
180159
*/
181-
public function setAliasesForLanguage( $languageCode, array $aliases ) {
160+
public function setAliasesForLanguage( string $languageCode, array $aliases ): void {
182161
$this->setGroup( new AliasGroup( $languageCode, $aliases ) );
183162
}
184163

@@ -189,7 +168,7 @@ public function setAliasesForLanguage( $languageCode, array $aliases ) {
189168
*
190169
* @return array[]
191170
*/
192-
public function toTextArray() {
171+
public function toTextArray(): array {
193172
$array = [];
194173

195174
foreach ( $this->groups as $group ) {
@@ -204,7 +183,7 @@ public function toTextArray() {
204183
*
205184
* @since 7.0
206185
*/
207-
public function clear() {
186+
public function clear(): void {
208187
$this->groups = [];
209188
}
210189

src/Term/TermList.php

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace Wikibase\DataModel\Term;
46

57
use ArrayIterator;
@@ -23,19 +25,18 @@ class TermList implements Countable, IteratorAggregate {
2325
/**
2426
* @var Term[]
2527
*/
26-
private $terms = [];
28+
private array $terms = [];
2729

2830
/**
2931
* @param iterable|Term[] $terms Can be a non-array since 8.1
3032
* @throws InvalidArgumentException
3133
*/
32-
public function __construct( /* iterable */ $terms = [] ) {
34+
public function __construct( iterable $terms = [] ) {
3335
$this->addAll( $terms );
3436
}
3537

3638
/**
3739
* @see Countable::count
38-
* @return int
3940
*/
4041
public function count(): int {
4142
return count( $this->terms );
@@ -46,7 +47,7 @@ public function count(): int {
4647
*
4748
* @return string[]
4849
*/
49-
public function toTextArray() {
50+
public function toTextArray(): array {
5051
$array = [];
5152

5253
foreach ( $this->terms as $term ) {
@@ -65,12 +66,9 @@ public function getIterator(): ArrayIterator {
6566
}
6667

6768
/**
68-
* @param string $languageCode
69-
*
70-
* @return Term
7169
* @throws OutOfBoundsException
7270
*/
73-
public function getByLanguage( $languageCode ) {
71+
public function getByLanguage( string $languageCode ): Term {
7472
if ( !array_key_exists( $languageCode, $this->terms ) ) {
7573
throw new OutOfBoundsException( 'Term with languageCode "' . $languageCode . '" not found' );
7674
}
@@ -82,33 +80,23 @@ public function getByLanguage( $languageCode ) {
8280
* @since 2.5
8381
*
8482
* @param string[] $languageCodes
85-
*
86-
* @return self
8783
*/
88-
public function getWithLanguages( array $languageCodes ) {
84+
public function getWithLanguages( array $languageCodes ): self {
8985
return new self( array_intersect_key( $this->terms, array_flip( $languageCodes ) ) );
9086
}
9187

92-
/**
93-
* @param string $languageCode
94-
*/
95-
public function removeByLanguage( $languageCode ) {
88+
public function removeByLanguage( string $languageCode ): void {
9689
unset( $this->terms[$languageCode] );
9790
}
9891

99-
/**
100-
* @param string $languageCode
101-
*
102-
* @return bool
103-
*/
104-
public function hasTermForLanguage( $languageCode ) {
92+
public function hasTermForLanguage( string $languageCode ): bool {
10593
return array_key_exists( $languageCode, $this->terms );
10694
}
10795

10896
/**
10997
* Replaces non-empty or removes empty terms.
11098
*/
111-
public function setTerm( Term $term ) {
99+
public function setTerm( Term $term ): void {
112100
if ( $term->getText() === '' ) {
113101
unset( $this->terms[$term->getLanguageCode()] );
114102
} else {
@@ -118,22 +106,15 @@ public function setTerm( Term $term ) {
118106

119107
/**
120108
* @since 0.8
121-
*
122-
* @param string $languageCode
123-
* @param string $termText
124109
*/
125-
public function setTextForLanguage( $languageCode, $termText ) {
110+
public function setTextForLanguage( string $languageCode, string $termText ): void {
126111
$this->setTerm( new Term( $languageCode, $termText ) );
127112
}
128113

129114
/**
130115
* @since 0.7.4
131-
*
132-
* @param mixed $target
133-
*
134-
* @return bool
135116
*/
136-
public function equals( $target ) {
117+
public function equals( mixed $target ): bool {
137118
if ( $this === $target ) {
138119
return true;
139120
}
@@ -155,21 +136,15 @@ public function equals( $target ) {
155136

156137
/**
157138
* @since 2.4.0
158-
*
159-
* @return bool
160139
*/
161-
public function isEmpty() {
140+
public function isEmpty(): bool {
162141
return $this->terms === [];
163142
}
164143

165144
/**
166145
* @since 0.7.4
167-
*
168-
* @param Term $term
169-
*
170-
* @return bool
171146
*/
172-
public function hasTerm( Term $term ) {
147+
public function hasTerm( Term $term ): bool {
173148
return array_key_exists( $term->getLanguageCode(), $this->terms )
174149
&& $this->terms[$term->getLanguageCode()]->equals( $term );
175150
}
@@ -179,7 +154,7 @@ public function hasTerm( Term $term ) {
179154
*
180155
* @since 7.0
181156
*/
182-
public function clear() {
157+
public function clear(): void {
183158
$this->terms = [];
184159
}
185160

@@ -189,11 +164,7 @@ public function clear() {
189164
* @param iterable|Term[] $terms
190165
* @throws InvalidArgumentException
191166
*/
192-
public function addAll( /* iterable */ $terms ) {
193-
if ( !is_array( $terms ) && !( $terms instanceof \Traversable ) ) {
194-
throw new InvalidArgumentException( '$terms must be iterable' );
195-
}
196-
167+
public function addAll( iterable $terms ): void {
197168
foreach ( $terms as $term ) {
198169
if ( !( $term instanceof Term ) ) {
199170
throw new InvalidArgumentException( 'Every element in $terms must be an instance of Term' );

tests/unit/Term/AliasGroupListTest.php

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace Wikibase\DataModel\Tests\Term;
46

57
use InvalidArgumentException;
@@ -105,13 +107,10 @@ public function testGivenSetLanguageCode_getByLanguageReturnsGroup() {
105107
$this->assertEquals( $enGroup, $list->getByLanguage( 'en' ) );
106108
}
107109

108-
/**
109-
* @dataProvider invalidLanguageCodeProvider
110-
*/
111-
public function testGivenInvalidLanguageCode_getByLanguageThrowsException( $languageCode ) {
110+
public function testGivenInvalidLanguageCode_getByLanguageThrowsException() {
112111
$list = new AliasGroupList();
113112
$this->expectException( OutOfBoundsException::class );
114-
$list->getByLanguage( $languageCode );
113+
$list->getByLanguage( '' );
115114
}
116115

117116
public function testGivenNonSetLanguageCode_getByLanguageThrowsException() {
@@ -161,12 +160,9 @@ public function testGivenSetLanguage_removeByLanguageRemovesIt() {
161160
$this->assertTrue( $list->isEmpty() );
162161
}
163162

164-
/**
165-
* @dataProvider invalidLanguageCodeProvider
166-
*/
167-
public function testGivenInvalidLanguageCode_removeByLanguageIsNoOp( $languageCode ) {
163+
public function testGivenInvalidLanguageCode_removeByLanguageIsNoOp() {
168164
$list = new AliasGroupList( [ new AliasGroup( 'en', [ 'foo' ] ) ] );
169-
$list->removeByLanguage( $languageCode );
165+
$list->removeByLanguage( 'invalidLanguageCodeProvider' );
170166
$this->assertFalse( $list->isEmpty() );
171167
}
172168

@@ -263,20 +259,9 @@ public function testGivenNonSetLanguageGroup_hasGroupForLanguageReturnsFalse() {
263259
$this->assertFalse( $list->hasGroupForLanguage( 'en' ) );
264260
}
265261

266-
/**
267-
* @dataProvider invalidLanguageCodeProvider
268-
*/
269-
public function testGivenInvalidLanguageCode_hasGroupForLanguageReturnsFalse( $languageCode ) {
262+
public function testGivenInvalidLanguageCode_hasGroupForLanguageReturnsFalse() {
270263
$list = new AliasGroupList();
271-
$this->assertFalse( $list->hasGroupForLanguage( $languageCode ) );
272-
}
273-
274-
public static function invalidLanguageCodeProvider() {
275-
return [
276-
[ null ],
277-
[ 21 ],
278-
[ '' ],
279-
];
264+
$this->assertFalse( $list->hasGroupForLanguage( '' ) );
280265
}
281266

282267
public function testGivenMismatchingGroup_hasGroupForLanguageReturnsFalse() {
@@ -304,7 +289,7 @@ public function testGivenInvalidLanguageCode_setGroupTextsThrowsException() {
304289
$list = new AliasGroupList();
305290

306291
$this->expectException( InvalidArgumentException::class );
307-
$list->setAliasesForLanguage( null, [ 'foo', 'bar' ] );
292+
$list->setAliasesForLanguage( '', [ 'foo', 'bar' ] );
308293
}
309294

310295
public function testGivenInvalidAliases_setGroupTextsThrowsException() {

0 commit comments

Comments
 (0)