Skip to content

Commit 70a0eea

Browse files
tobanaddshore
authored andcommitted
WikibaseDataModel: PHP 7.4 CI fails
PHP 7.4 exposes two new magic functions __serialize and __unserialize for custom object serialization. These are required to be implemented to support migration from older PHP versions. see https://wiki.php.net/rfc/custom_object_serialization for more detail. Bug: T243590
1 parent e62317d commit 70a0eea

3 files changed

Lines changed: 73 additions & 9 deletions

File tree

src/ReferenceList.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
* @author H. Snater < mediawiki@snater.com >
2525
* @author Thiemo Kreuz
2626
* @author Bene* < benestar.wikimedia@gmail.com >
27+
*
28+
* @phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
29+
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2730
*/
2831
class ReferenceList implements Comparable, Countable, IteratorAggregate, Serializable {
2932

@@ -232,6 +235,26 @@ public function serialize() {
232235
return serialize( array_values( $this->references ) );
233236
}
234237

238+
/**
239+
* @see https://wiki.php.net/rfc/custom_object_serialization
240+
*
241+
* @return array
242+
*/
243+
public function __serialize() {
244+
return [
245+
'references' => array_values( $this->references )
246+
];
247+
}
248+
249+
/**
250+
* @see https://wiki.php.net/rfc/custom_object_serialization
251+
*
252+
* @param array $data
253+
*/
254+
public function __unserialize( array $data ) : void {
255+
$this->references = $data['references'];
256+
}
257+
235258
/**
236259
* @see Serializable::unserialize
237260
*

src/Snak/SnakList.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
* @license GPL-2.0-or-later
1919
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
2020
* @author Addshore
21+
*
22+
* @phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
23+
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2124
*/
2225
class SnakList extends ArrayObject implements Comparable, Hashable {
2326

@@ -275,10 +278,7 @@ private function setElement( $index, $value ) {
275278
* @return string
276279
*/
277280
public function serialize() {
278-
return serialize( [
279-
'data' => $this->getArrayCopy(),
280-
'index' => $this->indexOffset,
281-
] );
281+
return serialize( $this->__serialize() );
282282
}
283283

284284
/**
@@ -288,14 +288,34 @@ public function serialize() {
288288
*/
289289
public function unserialize( $serialized ) {
290290
$serializationData = unserialize( $serialized );
291+
$this->__unserialize( $serializationData );
292+
}
293+
294+
/**
295+
* @see https://wiki.php.net/rfc/custom_object_serialization
296+
*
297+
* @return array
298+
*/
299+
public function __serialize(): array {
300+
return [
301+
'data' => $this->getArrayCopy(),
302+
'index' => $this->indexOffset,
303+
];
304+
}
291305

292-
foreach ( $serializationData['data'] as $offset => $value ) {
306+
/**
307+
* @see https://wiki.php.net/rfc/custom_object_serialization
308+
*
309+
* @param array $data
310+
*/
311+
public function __unserialize( $data ) : void {
312+
foreach ( $data['data'] as $offset => $value ) {
293313
// Just set the element, bypassing checks and offset resolving,
294314
// as these elements have already gone through this.
295315
parent::offsetSet( $offset, $value );
296316
}
297317

298-
$this->indexOffset = $serializationData['index'];
318+
$this->indexOffset = $data['index'];
299319
}
300320

301321
/**

tests/unit/ReferenceListTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,21 @@ public function testRemoveReferenceHash( ReferenceList $references ) {
412412
$this->assertTrue( $references->isEmpty() );
413413
}
414414

415+
/**
416+
* This integration test (relies on ReferenceList::getValueHash) is supposed to break whenever the hash
417+
* calculation changes.
418+
*/
419+
public function testGetValueHashStability() {
420+
$array = new ReferenceList();
421+
$snak1 = new PropertyNoValueSnak( 1 );
422+
$snak2 = new PropertyNoValueSnak( 3 );
423+
$snak3 = new PropertyNoValueSnak( 2 );
424+
425+
$array->addNewReference( $snak1, $snak2, $snak3 );
426+
$expectedHash = '92244e1a91f60b7fa922d42441995442bf50adb5';
427+
$this->assertSame( $expectedHash, $array->getValueHash() );
428+
}
429+
415430
public function testRemoveReferenceHashRemovesIdenticalObjects() {
416431
$reference = new Reference( [ new PropertyNoValueSnak( 1 ) ] );
417432
$references = new ReferenceList( [ $reference, $reference ] );
@@ -505,11 +520,17 @@ public function testEmptySerializationStability() {
505520
public function testSerializationStability() {
506521
$list = new ReferenceList();
507522
$list->addNewReference( new PropertyNoValueSnak( 1 ) );
508-
$this->assertSame(
509-
"a:1:{i:0;O:28:\"Wikibase\\DataModel\\Reference\":1:{s:35:\"\x00Wikibase\\DataModel\\"
523+
524+
$testString = "a:1:{i:0;O:28:\"Wikibase\\DataModel\\Reference\":1:{s:35:\"\x00Wikibase\\DataModel\\"
510525
. "Reference\x00snaks\";C:32:\"Wikibase\\DataModel\\Snak\\SnakList\":100:{a:2:{s:4:\""
511526
. 'data";a:1:{i:0;C:43:"Wikibase\\DataModel\\Snak\\PropertyNoValueSnak":2:{P1}}s:5'
512-
. ':"index";i:0;}}}}',
527+
. ':"index";i:0;}}}}';
528+
529+
$secondList = new ReferenceList();
530+
$secondList->unserialize( $testString );
531+
532+
$this->assertSame(
533+
$testString,
513534
$list->serialize()
514535
);
515536
}

0 commit comments

Comments
 (0)