Completed
Push — master ( 951284...b5c57e )
by
unknown
06:36 queued 11s
created

repo/includes/ParserOutput/TermboxView.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Wikibase\Repo\ParserOutput;
4
5
use Wikibase\DataModel\Entity\EntityDocument;
6
use Wikibase\DataModel\Entity\EntityId;
7
use Wikibase\DataModel\Term\AliasGroupList;
8
use Wikibase\DataModel\Term\TermList;
9
use Wikibase\Lib\LanguageFallbackChainFactory;
10
use Wikibase\Lib\Store\EntityRevision;
11
use Wikibase\View\CacheableEntityTermsView;
12
use Wikibase\View\EntityTermsView;
13
use Wikibase\View\LocalizedTextProvider;
14
use Wikibase\View\SpecialPageLinker;
15
use Wikibase\View\Termbox\Renderer\TermboxRenderer;
16
use Wikibase\View\Termbox\Renderer\TermboxRenderingException;
17
use Wikibase\View\ViewPlaceHolderEmitter;
18
19
/**
20
 * @license GPL-2.0-or-later
21
 */
22
class TermboxView implements CacheableEntityTermsView {
23
24
	public const TERMBOX_PLACEHOLDER = 'wb-ui';
25
26
	public const TERMBOX_MARKUP = 'termbox-markup';
27
28
	public const TERMBOX_VERSION = 2;
29
30
	public const CACHE_VERSION = 2;
31
32
	private $fallbackChainFactory;
33
	private $renderer;
34
	private $specialPageLinker;
35
	private $textInjector;
36
37
	/**
38
	 * @var LocalizedTextProvider
39
	 */
40
	private $textProvider;
41
42
	public function __construct(
43
		LanguageFallbackChainFactory $fallbackChainFactory,
44
		TermboxRenderer $renderer,
45
		LocalizedTextProvider $textProvider,
46
		SpecialPageLinker $specialPageLinker,
47
		TextInjector $textInjector
48
	) {
49
		$this->fallbackChainFactory = $fallbackChainFactory;
50
		$this->renderer = $renderer;
51
		$this->textProvider = $textProvider;
52
		$this->specialPageLinker = $specialPageLinker;
53
		$this->textInjector = $textInjector;
54
	}
55
56
	public function getHtml(
57
		$mainLanguageCode,
58
		TermList $labels,
59
		TermList $descriptions,
60
		AliasGroupList $aliasGroups = null,
61
		EntityId $entityId = null
62
	) {
63
		return $this->textInjector->newMarker( self::TERMBOX_PLACEHOLDER );
64
	}
65
66
	public function getTitleHtml( EntityId $entityId = null ) {
67
		return htmlspecialchars(
68
			$this->textProvider->get( 'parentheses', [ $entityId->getSerialization() ] )
0 ignored issues
show
It seems like $entityId is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
69
		);
70
	}
71
72
	/**
73
	 * @see \Wikibase\View\ViewPlaceHolderEmitter
74
	 */
75
	public function getPlaceholders(
76
		EntityDocument $entity,
77
		$revision,
78
		$languageCode
79
	) {
80
		return [
81
			'wikibase-view-chunks' => $this->textInjector->getMarkers(),
82
			self::TERMBOX_MARKUP => $this->renderTermbox(
83
				$entity->getId(),
0 ignored issues
show
It seems like $entity->getId() can be null; however, renderTermbox() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
84
				$revision,
85
				$languageCode
86
			),
87
		];
88
	}
89
90
	/**
91
	 * @param EntityId $entityId
92
	 * @param int $revision
93
	 * @param string $mainLanguageCode
94
	 *
95
	 * @return string|null
96
	 */
97
	private function renderTermbox( EntityId $entityId, $revision, $mainLanguageCode ) {
98
		if ( $revision === EntityRevision::UNSAVED_REVISION ) {
99
			return ViewPlaceHolderEmitter::ERRONEOUS_PLACEHOLDER_VALUE;
100
		}
101
102
		try {
103
			return $this->renderer->getContent(
104
				$entityId,
105
				$revision,
106
				$mainLanguageCode,
107
				$this->specialPageLinker->getLink(
108
					EntityTermsView::TERMS_EDIT_SPECIAL_PAGE,
109
					[ $entityId->getSerialization() ]
110
				),
111
				$this->fallbackChainFactory->newFromLanguageCode( $mainLanguageCode )
112
			);
113
		} catch ( TermboxRenderingException $exception ) {
114
			return ViewPlaceHolderEmitter::ERRONEOUS_PLACEHOLDER_VALUE;
115
		}
116
	}
117
118
}
119