OtherProjectsSitesGenerator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 107
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getOtherProjectsSiteIds() 0 25 5
A getSiteForGroup() 0 15 4
A expandSpecialGroups() 0 8 2
A getLocalSite() 0 3 1
1
<?php
2
3
namespace Wikibase\Client;
4
5
use Site;
6
use SiteLookup;
7
8
/**
9
 * Generates a list of sites that should be displayed in the "Other projects" sidebar.
10
 *
11
 * @license GPL-2.0-or-later
12
 * @author Thomas Pellissier Tanon
13
 * @author Marius Hoch < [email protected] >
14
 */
15
class OtherProjectsSitesGenerator implements OtherProjectsSitesProvider {
16
17
	/**
18
	 * @var SiteLookup
19
	 */
20
	private $siteLookup;
21
22
	/**
23
	 * @var string
24
	 */
25
	private $localSiteId;
26
27
	/**
28
	 * @var string[]
29
	 */
30
	private $specialSiteGroups;
31
32
	/**
33
	 * @param SiteLookup $siteLookup
34
	 * @param string $localSiteId
35
	 * @param string[] $specialSiteGroups
36
	 */
37
	public function __construct( SiteLookup $siteLookup, $localSiteId, array $specialSiteGroups ) {
38
		$this->siteLookup = $siteLookup;
39
		$this->localSiteId = $localSiteId;
40
		$this->specialSiteGroups = $specialSiteGroups;
41
	}
42
43
	/**
44
	 * Get the site ids of other projects to use.
45
	 *
46
	 * @param array $siteLinkGroups
47
	 * @return string[]
48
	 */
49
	public function getOtherProjectsSiteIds( array $siteLinkGroups ) {
50
		$localSite = $this->getLocalSite();
51
52
		if ( $localSite === null ) {
53
			wfWarn( 'Site not found for ' . $this->localSiteId );
54
			return [];
55
		}
56
57
		$currentGroupId = $localSite->getGroup();
58
		$otherProjectsSiteIds = [];
59
60
		$this->expandSpecialGroups( $siteLinkGroups );
61
		foreach ( $siteLinkGroups as $groupId ) {
62
			if ( $groupId === $currentGroupId ) {
63
				continue;
64
			}
65
66
			$siteToAdd = $this->getSiteForGroup( $groupId, $localSite->getLanguageCode() );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $siteToAdd is correct as $this->getSiteForGroup($...ite->getLanguageCode()) (which targets Wikibase\Client\OtherPro...ator::getSiteForGroup()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

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

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
67
			if ( $siteToAdd ) {
68
				$otherProjectsSiteIds[] = $siteToAdd->getGlobalId();
69
			}
70
		}
71
72
		return $otherProjectsSiteIds;
73
	}
74
75
	/**
76
	 * Returns the site to link to for a given group or null
77
	 *
78
	 * If there is only one site in this group (like for commons) this site is returned else the site in the same language
79
	 * as the current site is returned
80
	 *
81
	 * @param string $groupId
82
	 * @param string $currentLanguageCode
83
	 *
84
	 * @return Site|null
85
	 */
86
	private function getSiteForGroup( $groupId, $currentLanguageCode ) {
87
		$siteGroupList = $this->siteLookup->getSites()->getGroup( $groupId );
88
		if ( $siteGroupList->count() === 1 ) {
89
			return $siteGroupList[0];
90
		}
91
92
		/** @var Site $site */
93
		foreach ( $siteGroupList as $site ) {
94
			if ( $site->getLanguageCode() === $currentLanguageCode ) {
95
				return $site;
96
			}
97
		}
98
99
		return null;
100
	}
101
102
	/**
103
	 * @param array &$groups
104
	 */
105
	private function expandSpecialGroups( &$groups ) {
106
		if ( !in_array( 'special', $groups ) ) {
107
			return;
108
		}
109
110
		$groups = array_diff( $groups, [ 'special' ] );
111
		$groups = array_merge( $groups, $this->specialSiteGroups );
112
	}
113
114
	/**
115
	 * @return Site
116
	 */
117
	private function getLocalSite() {
118
		return $this->siteLookup->getSite( $this->localSiteId );
119
	}
120
121
}
122