Skip to content

Commit 3f4cbbc

Browse files
committed
[RevisionSaver] Check for bugs and null pointers
1 parent 85cb54c commit 3f4cbbc

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/Service/RevisionSaver.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,49 +33,49 @@ public function __construct( WikibaseApi $api, Deserializer $entityDeserializer,
3333
/**
3434
* @param EditInfo|null $editInfo
3535
*
36-
* @throws RuntimeException
37-
* @throws InvalidArgumentException
36+
* @throws RuntimeException if Content is not an EntityDocument
37+
* @throws InvalidArgumentException if $editInfo is null and the revision does not have an EditInfo
3838
* @return Item|Property new version of the entity
3939
*/
4040
public function save( Revision $revision, EditInfo $editInfo = null ): object {
41-
if ( !$revision->getContent()->getData() instanceof EntityDocument ) {
42-
throw new RuntimeException( 'Can only save Content of EntityDocuments' );
41+
$content = $revision->getContent();
42+
$data = $content->getData();
43+
if (!$data instanceof EntityDocument) {
44+
throw new RuntimeException('Can only save Content of EntityDocuments');
4345
}
4446

45-
/** @var Item|Property $entity */
46-
$entity = $revision->getContent()->getData();
47-
$serialized = $this->entitySerializer->serialize( $entity );
47+
$entity = $data;
48+
$serialized = $this->entitySerializer->serialize($entity);
4849

4950
$params = [
50-
'data' => json_encode( $serialized )
51+
'data' => json_encode($serialized)
5152
];
5253

5354
$revId = $revision->getId();
54-
if ( $revId !== null ) {
55+
if ($revId !== null) {
5556
$params['baserevid'] = $revId;
5657
}
5758

5859
$entityId = $entity->getId();
59-
if ( $entityId !== null ) {
60+
if ($entityId !== null) {
6061
$params['id'] = $entityId->getSerialization();
6162

6263
// If we are provided an empty entity, then set the clear flag
63-
if ( $entity->isEmpty() ) {
64+
if ($entity->isEmpty()) {
6465
$params['clear'] = true;
6566
}
66-
67-
6867
} else {
6968
$params['new'] = $entity->getType();
7069
}
7170

7271
// If no editInfo is explicitly passed call back to the one in the revision?
73-
if ( $editInfo === null ) {
74-
$editInfo = $revision->getEditInfo();
72+
if ($editInfo === null && $revision->getEditInfo() === null) {
73+
throw new InvalidArgumentException('No EditInfo provided and the revision does not have one');
7574
}
7675

77-
$result = $this->api->postRequest( 'wbeditentity', $params, $editInfo );
78-
return $this->entityDeserializer->deserialize( $result['entity'] );
76+
$editInfo = $editInfo ?? $revision->getEditInfo();
77+
$result = $this->api->postRequest('wbeditentity', $params, $editInfo);
78+
return $this->entityDeserializer->deserialize($result['entity']);
7979
}
8080

8181
}

0 commit comments

Comments
 (0)