SiteLinkListPatcher   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 71
ccs 36
cts 36
cp 1
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A getBadgesFromSiteLinkData() 0 10 3
A getSiteLinksInDiffFormat() 0 16 2
A getPatchedSiteLinkList() 0 17 3
1
<?php
2
3
namespace Wikibase\DataModel\Services\Diff\Internal;
4
5
use Diff\DiffOp\Diff\Diff;
6
use Diff\Patcher\ListPatcher;
7
use Diff\Patcher\MapPatcher;
8
use InvalidArgumentException;
9
use Wikibase\DataModel\Entity\ItemId;
10
use Wikibase\DataModel\SiteLinkList;
11
12
/**
13
 * Package private.
14
 *
15
 * @license GPL-2.0-or-later
16
 * @author Jeroen De Dauw < [email protected] >
17
 * @author Thiemo Kreuz
18
 */
19
class SiteLinkListPatcher {
20
21
	/**
22
	 * @var MapPatcher
23
	 */
24
	private $patcher;
25
26
	public function __construct() {
27 3
		$this->patcher = new MapPatcher( false, new ListPatcher() );
28 3
	}
29 3
30
	/**
31
	 * @param SiteLinkList $siteLinks
32
	 * @param Diff $patch
33
	 *
34
	 * @return SiteLinkList
35
	 * @throws InvalidArgumentException
36
	 */
37
	public function getPatchedSiteLinkList( SiteLinkList $siteLinks, Diff $patch ) {
38 3
		$baseData = $this->getSiteLinksInDiffFormat( $siteLinks );
39 3
		$patchedData = $this->patcher->patch( $baseData, $patch );
40 3
41
		$patchedSiteLinks = new SiteLinkList();
42 3
43
		foreach ( $patchedData as $siteId => $siteLinkData ) {
44 3
			if ( array_key_exists( 'name', $siteLinkData ) ) {
45 3
				$patchedSiteLinks->addNewSiteLink(
46 3
					$siteId,
47 3
					$siteLinkData['name'],
48 3
					$this->getBadgesFromSiteLinkData( $siteLinkData )
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getBadgesFromSiteLinkData($siteLinkData) targeting Wikibase\DataModel\Servi...adgesFromSiteLinkData() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
49 3
				);
50 3
			}
51 3
		}
52 3
53
		return $patchedSiteLinks;
54 3
	}
55
56
	/**
57
	 * @param string[] $siteLinkData
58
	 *
59
	 * @return ItemId[]|null
60
	 */
61
	private function getBadgesFromSiteLinkData( array $siteLinkData ) {
62 3
		if ( !array_key_exists( 'badges', $siteLinkData ) || !is_array( $siteLinkData['badges'] ) ) {
0 ignored issues
show
introduced by
The condition is_array($siteLinkData['badges']) is always false.
Loading history...
63 3
			return null;
64 1
		}
65
66
		return array_map(
67 2
			static function( $idSerialization ) {
68
				return new ItemId( $idSerialization );
69 1
			},
70 2
			$siteLinkData['badges']
71 2
		);
72 2
	}
73
74
	private function getSiteLinksInDiffFormat( SiteLinkList $siteLinks ): array {
75 3
		$linksInDiffFormat = [];
76 3
77
		foreach ( $siteLinks->toArray() as $siteLink ) {
78
			$linksInDiffFormat[$siteLink->getSiteId()] = [
79
				'name' => $siteLink->getPageName(),
80
				'badges' => array_map(
81 3
					static function( ItemId $id ) {
82 2
						return $id->getSerialization();
83 2
					},
84 2
					$siteLink->getBadges()
85 2
				),
86 1
			];
87 2
		}
88 2
89 2
		return $linksInDiffFormat;
90 2
	}
91 3
92
}
93