Skip to content

Commit 02dee43

Browse files
committed
Minor code styling bits
1 parent c481062 commit 02dee43

5 files changed

Lines changed: 40 additions & 43 deletions

File tree

src/ByPropertyIdArray.php

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ public function getFlatArrayIndexOfObject( $object ) {
138138
$this->assertIndexIsBuild();
139139

140140
$i = 0;
141-
foreach( $this as $o ) {
142-
if( $o === $object ) {
141+
foreach ( $this as $o ) {
142+
if ( $o === $object ) {
143143
return $i;
144144
}
145145
$i++;
@@ -159,7 +159,7 @@ public function toFlatArray() {
159159
$this->assertIndexIsBuild();
160160

161161
$array = array();
162-
foreach( $this->byId as $objects ) {
162+
foreach ( $this->byId as $objects ) {
163163
$array = array_merge( $array, $objects );
164164
}
165165
return $array;
@@ -179,8 +179,8 @@ private function getFlatArrayIndices( PropertyId $propertyId ) {
179179
$propertyIndices = array();
180180
$i = 0;
181181

182-
foreach( $this->byId as $serializedPropertyId => $objects ) {
183-
if( $serializedPropertyId === $propertyId->getSerialization() ) {
182+
foreach ( $this->byId as $serializedPropertyId => $objects ) {
183+
if ( $serializedPropertyId === $propertyId->getSerialization() ) {
184184
$propertyIndices = range( $i, $i + count( $objects ) - 1 );
185185
break;
186186
} else {
@@ -202,7 +202,7 @@ private function getFlatArrayIndices( PropertyId $propertyId ) {
202202
private function moveObjectInPropertyGroup( $object, $toIndex ) {
203203
$currentIndex = $this->getFlatArrayIndexOfObject( $object );
204204

205-
if( $toIndex === $currentIndex ) {
205+
if ( $toIndex === $currentIndex ) {
206206
return;
207207
}
208208

@@ -211,11 +211,11 @@ private function moveObjectInPropertyGroup( $object, $toIndex ) {
211211
$numericIndices = $this->getFlatArrayIndices( $propertyId );
212212
$lastIndex = $numericIndices[count( $numericIndices ) - 1];
213213

214-
if( $toIndex > $lastIndex + 1 || $toIndex < $numericIndices[0] ) {
214+
if ( $toIndex > $lastIndex + 1 || $toIndex < $numericIndices[0] ) {
215215
throw new OutOfBoundsException( 'Object cannot be moved to ' . $toIndex );
216216
}
217217

218-
if( $toIndex >= $lastIndex ) {
218+
if ( $toIndex >= $lastIndex ) {
219219
$this->moveObjectToEndOfPropertyGroup( $object );
220220
} else {
221221
$this->removeObject( $object );
@@ -287,13 +287,10 @@ private function insertObjectAtIndex( $object, $index ) {
287287
* @param int $toIndex
288288
*/
289289
private function movePropertyGroup( PropertyId $propertyId, $toIndex ) {
290-
if( $this->getPropertyGroupIndex( $propertyId ) === $toIndex ) {
290+
if ( $this->getPropertyGroupIndex( $propertyId ) === $toIndex ) {
291291
return;
292292
}
293293

294-
/**
295-
* @var PropertyId
296-
*/
297294
$insertBefore = null;
298295

299296
$oldIndex = $this->getPropertyGroupIndex( $propertyId );
@@ -302,16 +299,16 @@ private function movePropertyGroup( PropertyId $propertyId, $toIndex ) {
302299
// Remove "property group" to calculate the groups new index:
303300
unset( $this->byId[$propertyId->getSerialization()] );
304301

305-
if( $toIndex > $oldIndex ) {
302+
if ( $toIndex > $oldIndex ) {
306303
// If the group shall be moved towards the bottom, the number of objects within the
307304
// group needs to be subtracted from the absolute toIndex:
308305
$toIndex -= count( $byIdClone[$propertyId->getSerialization()] );
309306
}
310307

311-
foreach( $this->getPropertyIds() as $pId ) {
308+
foreach ( $this->getPropertyIds() as $pId ) {
312309
// Accepting other than the exact index by using <= letting the "property group" "latch"
313310
// in the next slot.
314-
if( $toIndex <= $this->getPropertyGroupIndex( $pId ) ) {
311+
if ( $toIndex <= $this->getPropertyGroupIndex( $pId ) ) {
315312
$insertBefore = $pId;
316313
break;
317314
}
@@ -320,17 +317,17 @@ private function movePropertyGroup( PropertyId $propertyId, $toIndex ) {
320317
$serializedPropertyId = $propertyId->getSerialization();
321318
$this->byId = array();
322319

323-
foreach( $byIdClone as $serializedPId => $objects ) {
320+
foreach ( $byIdClone as $serializedPId => $objects ) {
324321
$pId = new PropertyId( $serializedPId );
325-
if( $pId->equals( $propertyId ) ) {
322+
if ( $pId->equals( $propertyId ) ) {
326323
continue;
327-
} elseif( $pId->equals( $insertBefore ) ) {
324+
} elseif ( $pId->equals( $insertBefore ) ) {
328325
$this->byId[$serializedPropertyId] = $byIdClone[$serializedPropertyId];
329326
}
330327
$this->byId[$serializedPId] = $objects;
331328
}
332329

333-
if( is_null( $insertBefore ) ) {
330+
if ( $insertBefore === null ) {
334331
$this->byId[$serializedPropertyId] = $byIdClone[$serializedPropertyId];
335332
}
336333

@@ -348,9 +345,9 @@ private function movePropertyGroup( PropertyId $propertyId, $toIndex ) {
348345
private function getPropertyGroupIndex( PropertyId $propertyId ) {
349346
$i = 0;
350347

351-
foreach( $this->byId as $serializedPropertyId => $objects ) {
348+
foreach ( $this->byId as $serializedPropertyId => $objects ) {
352349
$pId = new PropertyId( $serializedPropertyId );
353-
if( $pId->equals( $propertyId ) ) {
350+
if ( $pId->equals( $propertyId ) ) {
354351
return $i;
355352
}
356353
$i += count( $objects );
@@ -373,18 +370,18 @@ private function getPropertyGroupIndex( PropertyId $propertyId ) {
373370
public function moveObjectToIndex( $object, $toIndex ) {
374371
$this->assertIndexIsBuild();
375372

376-
if( !in_array( $object, $this->toFlatArray() ) ) {
373+
if ( !in_array( $object, $this->toFlatArray() ) ) {
377374
throw new OutOfBoundsException( 'Object not present in array' );
378-
} elseif( $toIndex < 0 || $toIndex > count( $this ) ) {
375+
} elseif ( $toIndex < 0 || $toIndex > count( $this ) ) {
379376
throw new OutOfBoundsException( 'Specified index is out of bounds' );
380-
} elseif( $this->getFlatArrayIndexOfObject( $object ) === $toIndex ) {
377+
} elseif ( $this->getFlatArrayIndexOfObject( $object ) === $toIndex ) {
381378
return;
382379
}
383380

384381
// Determine whether to simply reindex the object within its "property group":
385382
$propertyIndices = $this->getFlatArrayIndices( $object->getPropertyId() );
386383

387-
if( in_array( $toIndex, $propertyIndices ) ) {
384+
if ( in_array( $toIndex, $propertyIndices ) ) {
388385
$this->moveObjectInPropertyGroup( $object, $toIndex );
389386
} else {
390387
$edgeIndex = ( $toIndex <= $propertyIndices[0] )
@@ -417,14 +414,14 @@ public function addObjectAtIndex( $object, $index = null ) {
417414
$propertyId = $object->getPropertyId();
418415
$validIndices = $this->getFlatArrayIndices( $propertyId );
419416

420-
if( count( $this ) === 0 ) {
417+
if ( count( $this ) === 0 ) {
421418
// Array is empty, just append object.
422419
$this->append( $object );
423-
} elseif( empty( $validIndices ) ) {
420+
} elseif ( empty( $validIndices ) ) {
424421
// No objects featuring that property exist. The object may be inserted at a place
425422
// between existing "property groups".
426423
$this->append( $object );
427-
if( !is_null( $index ) ) {
424+
if ( $index !== null ) {
428425
$this->buildIndex();
429426
$this->moveObjectToIndex( $object, $index );
430427
}
@@ -450,19 +447,19 @@ private function addObjectToPropertyGroup( $object, $index = null ) {
450447
$propertyId = $object->getPropertyId();
451448
$validIndices = $this->getFlatArrayIndices( $propertyId );
452449

453-
if( empty( $validIndices ) ) {
450+
if ( empty( $validIndices ) ) {
454451
throw new OutOfBoundsException( 'No objects featuring the object\'s property exist' );
455452
}
456453

457454
// Add index to allow placing object after the last object of the "property group":
458455
$validIndices[] = $validIndices[count( $validIndices ) - 1] + 1;
459456

460-
if( is_null( $index ) ) {
457+
if ( $index === null ) {
461458
// If index is null, append object to "property group".
462459
$index = $validIndices[count( $validIndices ) - 1];
463460
}
464461

465-
if( in_array( $index, $validIndices ) ) {
462+
if ( in_array( $index, $validIndices ) ) {
466463
// Add object at index within "property group".
467464
$this->byId[$propertyId->getSerialization()][] = $object;
468465
$this->exchangeArray( $this->toFlatArray() );
@@ -474,7 +471,7 @@ private function addObjectToPropertyGroup( $object, $index = null ) {
474471

475472
// Move new object to the edge of the "property group" to receive its designated
476473
// index:
477-
if( $index < $validIndices[0] ) {
474+
if ( $index < $validIndices[0] ) {
478475
array_unshift( $this->byId[$propertyId->getSerialization()], $object );
479476
} else {
480477
$this->byId[$propertyId->getSerialization()][] = $object;

src/Entity/Entity.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,11 @@ public function getLabel( $languageCode ) {
268268
private function getMultilangTexts( $fieldKey, array $languageCodes = null ) {
269269
if ( $fieldKey === 'label' ) {
270270
$textList = $this->fingerprint->getLabels()->toTextArray();
271-
}
272-
else {
271+
} else {
273272
$textList = $this->fingerprint->getDescriptions()->toTextArray();
274273
}
275274

276-
if ( !is_null( $languageCodes ) ) {
275+
if ( $languageCodes !== null ) {
277276
$textList = array_intersect_key( $textList, array_flip( $languageCodes ) );
278277
}
279278

src/ReferenceList.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ public function __construct( $references = array() ) {
5555
* @throws InvalidArgumentException
5656
*/
5757
public function addReference( Reference $reference, $index = null ) {
58-
if( !is_null( $index ) && !is_integer( $index ) ) {
59-
throw new InvalidArgumentException( '$index must be an integer or null; got ' . gettype( $index ) );
60-
} elseif ( is_null( $index ) || $index >= count( $this ) ) {
58+
if ( !is_int( $index ) && $index !== null ) {
59+
throw new InvalidArgumentException( '$index must be an integer or null' );
60+
}
61+
62+
if ( $index === null || $index >= count( $this ) ) {
6163
// Append object to the end of the reference list.
6264
$this->attach( $reference );
6365
} else {

src/Statement/StatementList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public function isEmpty() {
291291
* @return int|bool Zero-based index or false if not found.
292292
*/
293293
public function getIndexByGuid( $statementGuid ) {
294-
if ( !is_string( $statementGuid ) && !is_null( $statementGuid ) ) {
294+
if ( !is_string( $statementGuid ) && $statementGuid !== null ) {
295295
throw new InvalidArgumentException( '$statementGuid needs to be string or null' );
296296
}
297297

tests/unit/Entity/ItemTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,9 @@ protected function getNewEmpty() {
5050
}
5151

5252
public function testGetId() {
53-
/**
54-
* @var Item $item
55-
*/
5653
foreach ( TestItems::getItems() as $item ) {
57-
$this->assertTrue( is_null( $item->getId() ) || $item->getId() instanceof ItemId );
54+
$id = $item->getId();
55+
$this->assertTrue( $id === null || $id instanceof ItemId );
5856
}
5957
}
6058

@@ -81,6 +79,7 @@ public function itemProvider() {
8179
$item->setAliases( 'de', array( 'bar', 'baz' ) );
8280
$items[] = $item;
8381

82+
/** @var Item $item */
8483
$item = $item->copy();
8584
$item->getStatements()->addNewStatement(
8685
new PropertyNoValueSnak( new PropertyId( 'P42' ) )

0 commit comments

Comments
 (0)