modifyTargetLink()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 3
nc 4
nop 1
crap 3
1
<?php
2
3
namespace SIL;
4
5
use Title;
6
use Language;
7
8
use SMW\Localizer;
9
10
/**
11
 * @license GNU GPL v2+
12
 * @since 1.0
13
 *
14
 * @author mwjames
15
 */
16
class InterlanguageListParserFunction {
17
18
	/**
19
	 * @var InterlanguageLinksLookup
20
	 */
21
	private $interlanguageLinksLookup;
22
23
	/**
24
	 * @since 1.0
25
	 *
26
	 * @param InterlanguageLinksLookup $interlanguageLinksLookup
27
	 */
28 8
	public function __construct( InterlanguageLinksLookup $interlanguageLinksLookup ) {
29 8
		$this->interlanguageLinksLookup = $interlanguageLinksLookup;
30 8
	}
31
32
	/**
33
	 * @since 1.0
34
	 *
35
	 * @param string $linkReference
36
	 * @param string $template
37
	 *
38
	 * @return string
39
	 */
40 7
	public function parse( $linkReference, $template ) {
41
42 7
		if ( $linkReference === '' ) {
43 1
			return $this->createErrorMessageFor( 'sil-interlanguagelist-missing-linkreference' );
44
		}
45
46 7
		if ( $template === '' ) {
47 1
			return $this->createErrorMessageFor( 'sil-interlanguagelist-missing-template' );
48
		}
49
50 7
		$title = Title::newFromText( $linkReference );
51
52 7
		if ( $title === null ) {
53 1
			return $this->createErrorMessageFor( 'sil-interlanguageparser-linkreference-error', $linkReference );
54
		}
55
56
		// `null` indicates to the lookup interface to return all matches regardless
57
		// of any language code
58 6
		$interlanguageLink = new InterlanguageLink(
59 6
			null,
60 6
			$this->interlanguageLinksLookup->getRedirectTargetFor( $title )
61
		);
62
63 6
		$languageTargetLinks = $this->getLanguageTargetLinks( $interlanguageLink );
64
65 6
		$templateText = $this->createTemplateInclusionCode(
66 6
			$languageTargetLinks,
67
			$template
68
		);
69
70 6
		return [ $templateText, 'noparse' => $templateText === '', 'isHTML' => false ];
71
	}
72
73 6
	private function getLanguageTargetLinks( InterlanguageLink $interlanguageLink ) {
74
75 6
		$languageTargetLinks = $this->interlanguageLinksLookup->queryLanguageTargetLinks(
76 6
			$interlanguageLink
77
		);
78
79 6
		ksort( $languageTargetLinks );
80
81 6
		return $languageTargetLinks;
82
	}
83
84 6
	private function createTemplateInclusionCode( array $languageTargetLinks, $template ) {
85
86 6
		$result = '';
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
87 6
		$templateText = '';
88 6
		$i = 0;
89
90 6
		foreach ( $languageTargetLinks as $languageCode => $targetLink ) {
91
92 5
			$wikitext = '';
93
94 5
			$wikitext .= "|#=" . $i++;
95 5
			$wikitext .= "|target-link=" . $this->modifyTargetLink( $targetLink );
96 5
			$wikitext .= "|lang-code=" . Localizer::asBCP47FormattedLanguageCode( $languageCode );
97 5
			$wikitext .= "|lang-name=" . Language::fetchLanguageName( $languageCode );
98
99 5
			$templateText .= '{{' . $template . $wikitext . '}}';
100
		}
101
102 6
		return $templateText;
103
	}
104
105 5
	private function modifyTargetLink( $targetLink ) {
106
107 5
		if ( !$targetLink instanceOf Title ) {
108 2
			$targetLink = Title::newFromText( $targetLink );
109
		}
110
111 5
		return ( $targetLink->getNamespace() === NS_CATEGORY ? ':' : '' ) . $targetLink->getPrefixedText();
112
	}
113
114 1
	private function createErrorMessageFor( $messageKey, $arg1 = '' ) {
115 1
		return '<div class="smw-callout smw-callout-error">' . wfMessage( $messageKey, $arg1 )->inContentLanguage()->text() . '</div>';
116
	}
117
118
}
119