Completed
Push — master ( 9b2528...666c25 )
by mw
60:20 queued 25:29
created

PropertyValueFormatter::format()   C

Complexity

Conditions 11
Paths 21

Size

Total Lines 46
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 11.0113

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 24
c 2
b 0
f 0
nc 21
nop 2
dl 0
loc 46
ccs 21
cts 22
cp 0.9545
crap 11.0113
rs 5.2653

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SMW\DataValues\ValueFormatters;
4
5
use SMW\ApplicationFactory;
6
use SMW\Highlighter;
7
use SMW\Localizer;
8
use SMW\Message;
9
use SMW\DIWikiPage;
10
use SMWDataValue as DataValue;
11
use SMWPropertyValue as PropertyValue;
12
use RuntimeException;
13
14
/**
15
 * @license GNU GPL v2+
16
 * @since 2.5
17
 *
18
 * @author mwjames
19
 */
20
class PropertyValueFormatter extends DataValueFormatter {
21
22
	/**
23
	 * @since 2.5
24
	 *
25
	 * {@inheritDoc}
26
	 */
27 166
	public function isFormatterFor( DataValue $dataValue ) {
28 166
		return $dataValue instanceof PropertyValue;
29
	}
30
31
	/**
32
	 * @since 2.5
33
	 *
34
	 * {@inheritDoc}
35
	 */
36 108
	public function format( $type, $linker = null ) {
37
38 108
		if ( !$this->dataValue instanceof PropertyValue ) {
39 1
			throw new RuntimeException( "The formatter is missing a valid PropertyValue object" );
40
		}
41
42 107
		if ( !$this->dataValue->isVisible() ) {
43 1
			return '';
44
		}
45
46 106
		if ( $type === self::VALUE ) {
47 88
			return $this->getWikiValue();
48
		}
49
50 76
		if ( $type === PropertyValue::FORMAT_LABEL ) {
51 15
			return $this->getFormattedLabel( $linker );
52
		}
53
54 75
		$wikiPageValue = $this->prepareWikiPageValue( $linker );
55 75
		$text = '';
56
57 75
		if ( $wikiPageValue === null ) {
58
			return '';
59
		}
60
61 75
		if ( $type === self::WIKI_SHORT ) {
62 53
			$text = $this->doHighlightText(
63
				$wikiPageValue->getShortWikiText( $linker ),
64
				$this->dataValue->getOptionBy( PropertyValue::OPT_HIGHLIGHT_LINKER ) ? $linker : null
65 75
			);
66 14
		}
67
68
		if ( $type === self::HTML_SHORT ) {
69 75
			$text = $this->doHighlightText( $wikiPageValue->getShortHTMLText( $linker ), $linker );
70 15
		}
71
72
		if ( $type === self::WIKI_LONG ) {
73 75
			$text =  $this->doHighlightText( $wikiPageValue->getLongWikiText( $linker ) );
74 2
		}
75
76
		if ( $type === self::HTML_LONG ) {
77 75
			$text = $this->doHighlightText( $wikiPageValue->getLongHTMLText( $linker ), $linker );
78
		}
79
80
		return $text . $this->hintPreferredLabelUse();
81
	}
82
83
	/**
84
	 * Formatting rule set:
85
	 * - preferred goes before translation
86 15
	 * - displayTitle goes before translation
87
	 * - translation goes before "normal" label
88 15
	 */
89 15
	private function getFormattedLabel( $linker = null ) {
90 15
91
		$property = $this->dataValue->getDataItem();
92 15
		$output = '';
93 15
		$displayTitle = '';
94
95
		$preferredLabel = $property->getPreferredLabel(
96 15
			$this->dataValue->getOptionBy( PropertyValue::OPT_USER_LANGUAGE )
97
		);
98 15
99 1
		$label = $preferredLabel;
100
101
		if ( $preferredLabel === '' && ( $label = $this->findTranslatedPropertyLabel( $property ) ) === '' ) {
102 15
			$label = $property->getLabel();
103 14
		}
104
105
		if ( $this->dataValue->getWikiPageValue() !== null ) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SMWDataValue as the method getWikiPageValue() does only exist in the following sub-classes of SMWDataValue: SMWPropertyValue. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
106 15
			$displayTitle = $this->dataValue->getWikiPageValue()->getDisplayTitle();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SMWDataValue as the method getWikiPageValue() does only exist in the following sub-classes of SMWDataValue: SMWPropertyValue. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
107
		}
108
109 15
		$canonicalLabel = $property->getCanonicalLabel();
110 1
111 1
		// Display title goes before a translated label (but not preferred)
112
		if ( $displayTitle !== '' && !$property->isUserDefined() ) {
113
			$label = $displayTitle;
114
			$canonicalLabel = $displayTitle;
115 15
		}
116 15
117
		// Internal format only used by PropertyValue
118 15
		$format = $this->getOptionBy( PropertyValue::FORMAT_LABEL );
119 14
		$this->dataValue->setCaption( $label );
120
121
		if ( $format === self::VALUE ) {
122 15
			$output = $this->dataValue->getWikiValue();
123
		}
124 15
125
		if ( $format === self::WIKI_LONG && $linker !== null ) {
126 15
			$output = $this->dataValue->getLongWikiText( $linker );
127
		} elseif ( $format === self::WIKI_LONG && $preferredLabel === '' && $displayTitle !== '' ) {
128
			$output = $displayTitle;
129
		} elseif ( $format === self::WIKI_LONG ) {
130
			// Avoid Title::getPrefixedText as it transforms the text to have a
131
			// leading capital letter in some configurations
132 15
			$output = Localizer::getInstance()->createTextWithNamespacePrefix( SMW_NS_PROPERTY, $label );
133 1
		}
134
135
		if ( $format === self::HTML_SHORT && $linker !== null ) {
136
			$output = $this->dataValue->getShortHTMLText( $linker );
137 15
		}
138 3
139
		// Output both according to the formatting rule set forth by
140
		if ( $canonicalLabel !== $label ) {
141 15
			$output = Message::get( array( 'smw-property-preferred-title-format', $output, $canonicalLabel ) );
142
		}
143
144 88
		return $output;
145
	}
146 88
147 88
	private function getWikiValue() {
148
149 88
		$property = $this->dataValue->getDataItem();
150 5
		$languageCode = $this->dataValue->getOptionBy( PropertyValue::OPT_USER_LANGUAGE );
151
152
		if ( ( $preferredLabel = $property->getPreferredLabel( $languageCode ) ) !== '' ) {
153 84
			return $preferredLabel;
154 2
		}
155
156
		if ( $this->dataValue->getWikiPageValue() !== null && $this->dataValue->getWikiPageValue()->getDisplayTitle() !== '' ) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SMWDataValue as the method getWikiPageValue() does only exist in the following sub-classes of SMWDataValue: SMWPropertyValue. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
157 83
			return $this->dataValue->getWikiPageValue()->getDisplayTitle();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SMWDataValue as the method getWikiPageValue() does only exist in the following sub-classes of SMWDataValue: SMWPropertyValue. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
158 25
		}
159
160
		if ( ( $translatedPropertyLabel = $this->findTranslatedPropertyLabel( $property ) ) !== '' ) {
161 69
			return $translatedPropertyLabel;
162
		}
163
164 75
		return $this->dataValue->getDataItem()->getLabel();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SMWDataItem as the method getLabel() does only exist in the following sub-classes of SMWDataItem: SMWDIProperty, SMW\DIProperty. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
165
	}
166 75
167
	private function prepareWikiPageValue( $linker = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $linker is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
168 75
169
		$wikiPageValue = $this->dataValue->getWikiPageValue();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SMWDataValue as the method getWikiPageValue() does only exist in the following sub-classes of SMWDataValue: SMWPropertyValue. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
170
171
		if ( $wikiPageValue === null ) {
172 75
			return null;
173 75
		}
174
175 75
		$property = $this->dataValue->getDataItem();
176 63
		$caption = $this->dataValue->getCaption();
177 19
178
		if ( $caption !== false && $caption !== '' ) {
179 19
			$wikiPageValue->setCaption( $caption );
180 4
		} elseif ( ( $preferredLabel = $this->dataValue->getPreferredLabel() ) !== '' ) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SMWDataValue as the method getPreferredLabel() does only exist in the following sub-classes of SMWDataValue: SMWPropertyValue. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
181
			$wikiPageValue->setCaption( $preferredLabel );
182 15
		} elseif ( ( $translatedPropertyLabel = $this->findTranslatedPropertyLabel( $property ) ) !== '' ) {
183
			$wikiPageValue->setCaption( $translatedPropertyLabel );
184
		} else {
185 75
			$wikiPageValue->setCaption( $property->getLabel() );
186
		}
187
188 74
		return $wikiPageValue;
189
	}
190 74
191
	private function doHighlightText( $text, $linker = null ) {
192 74
193 67
		$content = '';
194
195
		if ( !$this->canHighlight( $content, $linker ) ) {
196 19
			return $text;
197 19
		}
198 19
199
		$highlighter = Highlighter::factory(
200
			Highlighter::TYPE_PROPERTY,
201 19
			$this->dataValue->getOptionBy( PropertyValue::OPT_USER_LANGUAGE )
0 ignored issues
show
Security Bug introduced by
It seems like $this->dataValue->getOpt...lue::OPT_USER_LANGUAGE) targeting SMWDataValue::getOptionBy() can also be of type false; however, SMW\Highlighter::factory() does only seem to accept string|null, did you maybe forget to handle an error condition?
Loading history...
202 19
		);
203 19
204 19
		$highlighter->setContent( array (
205
			'userDefined' => $this->dataValue->getDataItem()->isUserDefined(),
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SMWDataItem as the method isUserDefined() does only exist in the following sub-classes of SMWDataItem: SMWDIProperty, SMW\DIProperty. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
206
			'caption' => $text,
207 19
			'content' => $content !== '' ? $content : Message::get( 'smw_isspecprop' )
208
		) );
209
210 74
		return $highlighter->getHtml();
211
	}
212 74
213 2
	private function canHighlight( &$propertyDescription, $linker ) {
214
215
		if ( $this->dataValue->getOptionBy( PropertyValue::OPT_NO_HIGHLIGHT ) === true ) {
216 74
			return false;
217
		}
218 74
219
		$dataItem = $this->dataValue->getDataItem();
220 74
221
		$propertyDescription = ApplicationFactory::getInstance()->getPropertySpecificationLookup()->getPropertyDescriptionBy(
222
			$dataItem,
0 ignored issues
show
Compatibility introduced by
$dataItem of type object<SMWDataItem> is not a sub-type of object<SMW\DIProperty>. It seems like you assume a child class of the class SMWDataItem to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
223
			$this->dataValue->getOptionBy( PropertyValue::OPT_USER_LANGUAGE ),
0 ignored issues
show
Security Bug introduced by
It seems like $this->dataValue->getOpt...lue::OPT_USER_LANGUAGE) targeting SMWDataValue::getOptionBy() can also be of type false; however, SMW\PropertySpecificatio...PropertyDescriptionBy() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
224 74
			$linker
225
		);
226
227 75
		return !$dataItem->isUserDefined() || $propertyDescription !== '';
228
	}
229 75
230 75
	private function hintPreferredLabelUse() {
231 10
232
		if ( !$this->dataValue->isEnabledFeature( SMW_DV_PROV_LHNT ) ||
233
			$this->dataValue->getOptionBy( PropertyValue::OPT_NO_PREF_LHNT ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->dataValue->getOpt...alue::OPT_NO_PREF_LHNT) of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
234 65
			return '';
235
		}
236 65
237 65
		$property = $this->dataValue->getDataItem();
238
239
		$preferredLabel = $property->getPreferredLabel(
240 65
			$this->dataValue->getOptionBy( PropertyValue::OPT_USER_LANGUAGE )
241 60
		);
242
243
		if ( $preferredLabel === '' || $this->dataValue->getCaption() !== $preferredLabel ) {
244 8
			return '';
245 8
		}
246
247 8
		$label = $property->getLabel();
248 8
		$preferredLabelMarker = '';
249
250
		if ( $preferredLabel !== $label ) {
251 8
			$preferredLabelMarker = '&nbsp;' . \Html::rawElement( 'span', array( 'title' => $property->getCanonicalLabel() ), '<sup>ᵖ</sup>' );
252
		}
253
254 94
		return $preferredLabelMarker;
255
	}
256
257
	private function findTranslatedPropertyLabel( $property ) {
258 94
259 80
		// User-defined properties don't have any translatable label (this is
260
		// what the preferred label is for)
261
		if ( $property->isUserDefined() ) {
262 26
			return '';
263 26
		}
264 26
265
		return ApplicationFactory::getInstance()->getPropertyLabelFinder()->findPropertyLabelByLanguageCode(
266
			$property->getKey(),
267
			$this->dataValue->getOptionBy( PropertyValue::OPT_USER_LANGUAGE )
0 ignored issues
show
Security Bug introduced by
It seems like $this->dataValue->getOpt...lue::OPT_USER_LANGUAGE) targeting SMWDataValue::getOptionBy() can also be of type false; however, SMW\PropertyLabelFinder:...tyLabelByLanguageCode() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
268
		);
269
	}
270
271
}
272