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

PlaceholderEmittingEntityTermsView.php (3 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\Services\Lookup\LabelDescriptionLookup;
8
use Wikibase\DataModel\Term\AliasGroupList;
9
use Wikibase\DataModel\Term\TermList;
10
use Wikibase\View\CacheableEntityTermsView;
11
use Wikibase\View\EditSectionGenerator;
12
use Wikibase\View\HtmlTermRenderer;
13
use Wikibase\View\LocalizedTextProvider;
14
use Wikibase\View\SimpleEntityTermsView;
15
use Wikibase\View\Template\TemplateFactory;
16
use Wikibase\View\TermsListView;
17
18
/**
19
 * An EntityTermsView that returns placeholders for some parts of the HTML
20
 *
21
 * @license GPL-2.0-or-later
22
 * @author Adrian Heine <[email protected]>
23
 */
24
class PlaceholderEmittingEntityTermsView extends SimpleEntityTermsView implements CacheableEntityTermsView {
25
26
	/**
27
	 * @var TemplateFactory
28
	 */
29
	private $templateFactory;
30
31
	/**
32
	 * @var TermsListView
33
	 */
34
	private $termsListView;
35
36
	/**
37
	 * @var TextInjector
38
	 */
39
	private $textInjector;
40
41
	public const TERMBOX_VERSION = 1;
42
43
	/**
44
	 * This is used as a value in the ParserCache `termboxVersion` entry, prefixed with TERMBOX_VERSION.
45
	 *
46
	 * Note: It's currently set to '' to avoid unnecessarily purging caches. Starting from the next time this is
47
	 * changed it should be a number, incremented every time the caches need to be purged.
48
	 */
49
	public const CACHE_VERSION = '';
50
51
	public function __construct(
52
		HtmlTermRenderer $htmlTermRenderer,
53
		LabelDescriptionLookup $labelDescriptionLookup,
54
		TemplateFactory $templateFactory,
55
		EditSectionGenerator $sectionEditLinkGenerator,
56
		LocalizedTextProvider $textProvider,
57
		TermsListView $termsListView,
58
		TextInjector $textInjector
59
	) {
60
		parent::__construct(
61
			$htmlTermRenderer,
62
			$labelDescriptionLookup,
63
			$templateFactory,
64
			$sectionEditLinkGenerator,
65
			$termsListView,
66
			$textProvider
67
		);
68
		$this->templateFactory = $templateFactory;
69
		$this->termsListView = $termsListView;
70
		$this->textInjector = $textInjector;
71
	}
72
73
	/**
74
	 * @param string $mainLanguageCode Desired language of the label, description and aliases in the
75
	 *  title and header section. Not necessarily identical to the interface language.
76
	 * @param TermList $labels
77
	 * @param TermList $descriptions
78
	 * @param AliasGroupList|null $aliasGroups
79
	 * @param EntityId|null $entityId the id of the entity
80
	 *
81
	 * @return string HTML
82
	 */
83
	public function getHtml(
84
		$mainLanguageCode,
85
		TermList $labels,
86
		TermList $descriptions,
87
		AliasGroupList $aliasGroups = null,
88
		EntityId $entityId = null
89
	) {
90
		$cssClasses = $this->textInjector->newMarker(
91
			'entityViewPlaceholder-entitytermsview-entitytermsforlanguagelistview-class'
92
		);
93
94
		return $this->templateFactory->render(
95
			'wikibase-entitytermsview',
96
			$this->getHeadingHtml( $mainLanguageCode, $entityId, $aliasGroups ),
97
			$this->textInjector->newMarker( 'termbox' ),
98
			$cssClasses,
99
			$this->getHtmlForLabelDescriptionAliasesEditSection( $mainLanguageCode, $entityId )
100
		);
101
	}
102
103
	/**
104
	 * @param string $mainLanguageCode Desired language of the label, description and aliases in the
105
	 *  title and header section. Not necessarily identical to the interface language.
106
	 * @param TermList $labels
107
	 * @param TermList $descriptions
108
	 * @param AliasGroupList|null $aliasGroups
109
	 *
110
	 * @return string[] HTML snippets
111
	 */
112
	public function getTermsListItems(
113
		$mainLanguageCode,
114
		TermList $labels,
115
		TermList $descriptions,
116
		AliasGroupList $aliasGroups = null
117
	) {
118
		$termsListItems = [];
119
120
		$termsListLanguages = $this->getTermsLanguageCodes(
121
			$mainLanguageCode,
122
			$labels,
123
			$descriptions,
124
			$aliasGroups ?: new AliasGroupList()
125
		);
126
127
		foreach ( $termsListLanguages as $languageCode ) {
128
			$termsListItems[ $languageCode ] = $this->termsListView->getListItemHtml(
129
				$labels,
130
				$descriptions,
131
				$aliasGroups,
132
				$languageCode
133
			);
134
		}
135
136
		return $termsListItems;
137
	}
138
139
	/**
140
	 * @inheritDoc
141
	 * @suppress PhanUndeclaredMethod
142
	 */
143
	public function getPlaceholders(
144
		EntityDocument $entity,
145
		$revision,
146
		$languageCode
147
	) {
148
		return [
149
			'wikibase-view-chunks' =>
150
				$this->textInjector->getMarkers(),
151
			'wikibase-terms-list-items' =>
152
				$this->getTermsListItems(
153
					$languageCode,
154
					$entity->getLabels(),
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Wikibase\DataModel\Entity\EntityDocument as the method getLabels() does only exist in the following implementations of said interface: Wikibase\DataModel\Entity\Item, Wikibase\DataModel\Entity\Property.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
155
					$entity->getDescriptions(),
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Wikibase\DataModel\Entity\EntityDocument as the method getDescriptions() does only exist in the following implementations of said interface: Wikibase\DataModel\Entity\Item, Wikibase\DataModel\Entity\Property.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
156
					$entity->getAliasGroups()
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Wikibase\DataModel\Entity\EntityDocument as the method getAliasGroups() does only exist in the following implementations of said interface: Wikibase\DataModel\Entity\Item, Wikibase\DataModel\Entity\Property.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
157
				)
158
		];
159
	}
160
161
}
162