Completed
Push — master ( 9cb597...d2d2ad )
by mw
13s
created

SMWPropertyValue::highlightText()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 3
nop 2
dl 0
loc 22
ccs 12
cts 12
cp 1
crap 5
rs 8.6737
c 0
b 0
f 0
1
<?php
2
3
use SMW\ApplicationFactory;
4
use SMW\DataValueFactory;
5
use SMW\DIProperty;
6
use SMW\Highlighter;
7
use SMW\Message;
8
9
/**
10
 * Objects of this class represent properties in SMW.
11
 *
12
 * This class represents both normal (user-defined) properties and
13
 * predefined ("special") properties. Predefined properties may still
14
 * have a standard label (and associated wiki article) and they will
15
 * behave just like user-defined properties in most cases (e.g. when
16
 * asking for a printout text, a link to the according page is produced).
17
 * It is possible that predefined properties have no visible label at all,
18
 * if they are used only internally and never specified by or shown to
19
 * the user. Those will use their internal ID as DB key, and
20
 * empty texts for most printouts. All other proeprties use their
21
 * canonical DB key (even if they are predefined and have an id).
22
 * Functions are provided to check whether a property is visible or
23
 * user-defined, and to get the internal ID, if any.
24
 *
25
 * @note This datavalue is used only for representing properties and,
26
 * possibly objects/values, but never for subjects (pages as such). Hence
27
 * it does not provide a complete Title-like interface, or support for
28
 * things like sortkey.
29
 *
30
 * @author Markus Krötzsch
31
 * @ingroup SMWDataValues
32
 */
33
class SMWPropertyValue extends SMWDataValue {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
34
35
	/**
36
	 * Avoid the display of a tooltip
37
	 */
38
	const OPT_NO_HIGHLIGHT = 'no.highlight';
39
40
	/**
41
	 * Cache for wiki page value object associated to this property, or
42
	 * null if no such page exists. Use getWikiPageValue() to get the data.
43
	 * @var SMWWikiPageValue
44
	 */
45
	protected $m_wikipage = null;
46
47
	/**
48
	 * @var array
49
	 */
50
	protected $linkAttributes = array();
51
52
	/**
53
	 * Cache for type value of this property, or null if not calculated yet.
54
	 * @var SMWTypesValue
55
	 */
56
	private $mPropTypeValue;
57
58
	/**
59
	 * @var DIProperty
60
	 */
61
	private $inceptiveProperty = null;
62
63
	/**
64
	 * @since 2.4
65
	 *
66
	 * @param string $typeid
67
	 */
68 218
	public function __construct( $typeid ) {
69 218
		parent::__construct( $typeid );
70 218
	}
71
72
	/**
73
	 * Static function for creating a new property object from a
74
	 * propertyname (string) as a user might enter it.
75
	 * @note The resulting property object might be invalid if
76
	 * the provided name is not allowed. An object is returned
77
	 * in any case.
78
	 *
79
	 * @param string $propertyName
0 ignored issues
show
Bug introduced by
There is no parameter named $propertyName. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
80
	 *
81
	 * @return SMWPropertyValue
82
	 */
83 131
	static public function makeUserProperty( $propertyLabel ) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
84 131
		return DataValueFactory::getInstance()->newPropertyValueByLabel( $propertyLabel );
85
	}
86
87
	/**
88
	 * Static function for creating a new property object from a property
89
	 * identifier (string) as it might be used internally. This might be
90
	 * the DB key version of some property title text or the id of a
91
	 * predefined property (such as '_TYPE').
92
	 * @note This function strictly requires an internal identifier, i.e.
93
	 * predefined properties must be referred to by their ID, and '-' is
94
	 * not supported for indicating inverses.
95
	 * @note The resulting property object might be invalid if
96
	 * the provided name is not allowed. An object is returned
97
	 * in any case.
98
	 */
99
	static public function makeProperty( $propertyid ) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
100
		$diProperty = new DIProperty( $propertyid );
101
		$dvProperty = new SMWPropertyValue( '__pro' );
102
		$dvProperty->setDataItem( $diProperty );
103
		return $dvProperty;
104
	}
105
106
	/**
107
	 * We use the internal wikipage object to store some of this objects data.
108
	 * Clone it to make sure that data can be modified independently from the
109
	 * original object's content.
110
	 */
111 1
	public function __clone() {
112 1
		if ( !is_null( $this->m_wikipage ) ) {
113 1
			$this->m_wikipage = clone $this->m_wikipage;
114
		}
115 1
	}
116
117
	/**
118
	 * @note If the inceptive property and the property referenced in dataItem
119
	 * are not equal then the dataItem represents the end target to which the
120
	 * inceptive property has been redirected.
121
	 *
122
	 * @since 2.4
123
	 *
124
	 * @return DIProperty
125
	 */
126 1
	public function getInceptiveProperty() {
127 1
		return $this->inceptiveProperty;
128
	}
129
130
	/**
131
	 * Extended parsing function to first check whether value refers to pre-defined
132
	 * property, resolve aliases, and set internal property id accordingly.
133
	 * @todo Accept/enforce property namespace.
134
	 */
135 199
	protected function parseUserValue( $value ) {
136 199
		$this->mPropTypeValue = null;
137 199
		$this->m_wikipage = null;
138
139 199
		if ( $this->m_caption === false ) { // always use this as caption
140 199
			$this->m_caption = $value;
141
		}
142
143 199
		list( $propertyName, $inverse ) = $this->doNormalizeUserValue(
144
			$value
145
		);
146
147 199
		$contentLanguage = $this->getOptionBy( self::OPT_CONTENT_LANGUAGE );
148
149
		try {
150 199
			$this->m_dataitem = DIProperty::newFromUserLabel( $propertyName, $inverse, $contentLanguage );
0 ignored issues
show
Bug introduced by
It seems like $contentLanguage defined by $this->getOptionBy(self::OPT_CONTENT_LANGUAGE) on line 147 can also be of type string; however, SMW\DIProperty::newFromUserLabel() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
151 2
		} catch ( SMWDataItemException $e ) { // happens, e.g., when trying to sort queries by property "-"
152 2
			$this->addErrorMsg( array( 'smw_noproperty', $value ) );
153 2
			$this->m_dataitem = new DIProperty( 'ERROR', false ); // just to have something
154
		}
155
156
		// @see the SMW_DV_PROV_DTITLE explanation
157 199
		if ( $this->isEnabledFeature( SMW_DV_PROV_DTITLE ) ) {
158
			$dataItem = ApplicationFactory::getInstance()->getPropertySpecificationLookup()->getPropertyFromDisplayTitle(
159
				$value
160
			);
161
162
			$this->m_dataitem = $dataItem ? $dataItem : $this->m_dataitem;
163
		}
164
165 199
		$this->inceptiveProperty = $this->m_dataitem;
166
167 199
		if ( $this->isEnabledFeature( SMW_DV_PROV_REDI ) ) {
168 191
			$this->m_dataitem = $this->m_dataitem->getRedirectTarget();
169
		}
170 199
	}
171
172
	/**
173
	 * @see SMWDataValue::loadDataItem()
174
	 * @param $dataitem SMWDataItem
175
	 * @return boolean
176
	 */
177 32
	protected function loadDataItem( SMWDataItem $dataItem ) {
178
179 32
		if ( $dataItem->getDIType() !== SMWDataItem::TYPE_PROPERTY ) {
180
			return false;
181
		}
182
183 32
		$this->inceptiveProperty = $dataItem;
0 ignored issues
show
Documentation Bug introduced by
$dataItem is of type object<SMWDataItem>, but the property $inceptiveProperty was declared to be of type object<SMW\DIProperty>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
184 32
		$this->m_dataitem = $dataItem;
185
186 32
		$this->mPropTypeValue = null;
187 32
		unset( $this->m_wikipage );
188 32
		$this->m_caption = false;
0 ignored issues
show
Documentation Bug introduced by
The property $m_caption was declared of type string, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
189 32
		$this->linkAttributes = array();
190
191 32
		return true;
192
	}
193
194
	/**
195
	 * @since 2.4
196
	 *
197
	 * @param array $linkAttributes
198
	 */
199 1
	public function setLinkAttributes( array $linkAttributes ) {
200 1
		$this->linkAttributes = $linkAttributes;
201
202 1
		if ( $this->getWikiPageValue() instanceof SMWDataValue ) {
203 1
			$this->m_wikipage->setLinkAttributes( $linkAttributes );
204
		}
205 1
	}
206
207 105
	public function setCaption( $caption ) {
208 105
		parent::setCaption( $caption );
209 105
		if ( $this->getWikiPageValue() instanceof SMWDataValue ) { // pass caption to embedded datavalue (used for printout)
210 105
			$this->m_wikipage->setCaption( $caption );
211
		}
212 105
	}
213
214
	public function setOutputFormat( $formatstring ) {
215
		$this->m_outformat = $formatstring;
216
		if ( $this->getWikiPageValue() instanceof SMWDataValue ) {
217
			$this->m_wikipage->setOutputFormat( $formatstring );
218
		}
219
	}
220
221
	public function setInverse( $isinverse ) {
222
		return $this->m_dataitem = new DIProperty( $this->m_dataitem->getKey(), ( $isinverse == true ) );
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 getKey() 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...
223
	}
224
225
	/**
226
	 * Return a wiki page value that can be used for displaying this
227
	 * property, or null if no such wiki page exists (for predefined
228
	 * properties without any label).
229
	 * @return SMWWikiPageValue or null
230
	 */
231 109
	public function getWikiPageValue() {
232
233 109
		if ( isset( $this->m_wikipage ) ) {
234 69
			return $this->m_wikipage;
235
		}
236
237 109
		$diWikiPage = $this->m_dataitem->getCanonicalDiWikiPage();
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 getCanonicalDiWikiPage() 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...
238
239 109
		if ( $diWikiPage !== null ) {
240 109
			$this->m_wikipage = \SMW\DataValueFactory::getInstance()->newDataValueByItem( $diWikiPage, null, $this->m_caption );
0 ignored issues
show
Documentation introduced by
$this->m_caption is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
241 109
			$this->m_wikipage->setOutputFormat( $this->m_outformat );
242 109
			$this->m_wikipage->setLinkAttributes( $this->linkAttributes );
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 setLinkAttributes() does only exist in the following sub-classes of SMWDataValue: SMWPropertyValue, SMWWikiPageValue. 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...
243 109
			$this->m_wikipage->setOptions( $this->getOptions() );
244 109
			$this->addError( $this->m_wikipage->getErrors() );
245
		} else { // should rarely happen ($value is only changed if the input $value really was a label for a predefined prop)
246
			$this->m_wikipage = null;
247
		}
248
249 109
		return $this->m_wikipage;
250
	}
251
252
	/**
253
	 * Return TRUE if this is a property that can be displayed, and not a pre-defined
254
	 * property that is used only internally and does not even have a user-readable name.
255
	 * @note Every user defined property is necessarily visible.
256
	 */
257 94
	public function isVisible() {
258 94
		return $this->isValid() && ( $this->m_dataitem->isUserDefined() || $this->m_dataitem->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 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...
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...
259
	}
260
261
	/**
262
	 * @since 2.2
263
	 *
264
	 * @return boolean
265
	 */
266 177
	public function canUse() {
267 177
		return $this->isValid() && $this->m_dataitem->isUnrestricted();
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 isUnrestricted() 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...
268
	}
269
270 40
	public function getShortWikiText( $linked = null ) {
271
272 40
		if ( $this->isVisible() ) {
273 39
			$wikiPageValue = $this->getWikiPageValue();
274 39
			return is_null( $wikiPageValue ) ? '' : $this->highlightText( $wikiPageValue->getShortWikiText( $linked ) );
275
		}
276
277 1
		return '';
278
	}
279
280 8
	public function getShortHTMLText( $linked = null ) {
281
282 8
		if ( $this->isVisible() ) {
283 8
			$wikiPageValue = $this->getWikiPageValue();
284 8
			return is_null( $wikiPageValue ) ? '' : $this->highlightText( $wikiPageValue->getShortHTMLText( $linked ), $linked );
285
		}
286
287
		return '';
288
	}
289
290 12
	public function getLongWikiText( $linked = null ) {
291
292 12
		if ( $this->isVisible() ) {
293 12
			$wikiPageValue = $this->getWikiPageValue();
294 12
			return is_null( $wikiPageValue ) ? '' : $this->highlightText( $wikiPageValue->getLongWikiText( $linked ) );
295
		}
296
297
		return '';
298
	}
299
300
	public function getLongHTMLText( $linked = null ) {
301
302
		if ( $this->isVisible() ) {
303
			$wikiPageValue = $this->getWikiPageValue();
304
			return is_null( $wikiPageValue ) ? '' : $this->highlightText( $wikiPageValue->getLongHTMLText( $linked ), $linked );
305
		}
306
307
		return '';
308
	}
309
310 68
	public function getWikiValue() {
311
312 68
		if ( !$this->isVisible() ) {
313
			return '';
314
		}
315
316 68
		if ( $this->getWikiPageValue() !== null && $this->getWikiPageValue()->getDisplayTitle() !== '' ) {
317 1
			return $this->getWikiPageValue()->getDisplayTitle();
318
		}
319
320 68
		return $this->m_dataitem->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...
321
	}
322
323
	/**
324
	 * If this property was not user defined, return the internal ID string referring to
325
	 * that property. Otherwise return FALSE;
326
	 */
327
	public function getPropertyID() {
328
		return $this->m_dataitem->isUserDefined() ? false : $this->m_dataitem->getKey();
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...
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SMWDataItem as the method getKey() 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...
329
	}
330
331
	/**
332
	 * Return an SMWTypesValue object representing the datatype of this
333
	 * property.
334
	 * @deprecated Types values are not a good way to exchange SMW type information. They are for input only. Use getPropertyTypeID() if you want the type id. This method will vanish in SMW 1.7.
335
	 */
336
	public function getTypesValue() {
337
		$result = SMWTypesValue::newFromTypeId( $this->getPropertyTypeID() );
338
		if ( !$this->isValid() ) {
339
			$result->addError( $this->getErrors() );
340
		}
341
		return $result;
342
	}
343
344
	/**
345
	 * Convenience method to find the type id of this property. Most callers
346
	 * should rather use DIProperty::findPropertyTypeId() directly. Note
347
	 * that this is not the same as getTypeID(), which returns the id of
348
	 * this property datavalue.
349
	 *
350
	 * @return string
351
	 */
352 4
	public function getPropertyTypeID() {
353 4
		if ( $this->isValid() ) {
354 4
			return $this->m_dataitem->findPropertyTypeId();
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 findPropertyTypeId() 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...
355
		} else {
356
			return '__err';
357
		}
358
	}
359
360
	/**
361
	 * Create special highlighting for hinting at special properties.
362
	 */
363 51
	protected function highlightText( $text, $linker = null ) {
364
365 51
		if ( $this->getOptionBy( self::OPT_NO_HIGHLIGHT ) === true ) {
366 2
			return $text;
367
		}
368
369 51
		$propertySpecificationLookup = ApplicationFactory::getInstance()->getPropertySpecificationLookup();
370
371 51
		if ( ( $content = $propertySpecificationLookup->getPropertyDescriptionBy( $this->m_dataitem, $linker ) ) !== '' || !$this->m_dataitem->isUserDefined() ) {
0 ignored issues
show
Compatibility introduced by
$this->m_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...
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...
372
373 14
			$highlighter = Highlighter::factory( Highlighter::TYPE_PROPERTY );
374 14
			$highlighter->setContent( array (
375 14
				'userDefined' => $this->m_dataitem->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...
376 14
				'caption' => $text,
377 14
				'content' => $content !== '' ? $content : wfMessage( 'smw_isspecprop' )->text()
378
			) );
379
380 14
			return $highlighter->getHtml();
381
		}
382
383 46
		return $text;
384
	}
385
386
	/**
387
	 * A function for registering/overwriting predefined properties for SMW. Should be called from
388
	 * within the hook 'smwInitProperties'. Ids should start with three underscores "___" to avoid
389
	 * current and future confusion with SMW built-ins.
390
	 *
391
	 * @deprecated Use DIProperty::registerProperty(). Will vanish before SMW 1.7.
392
	 */
393
	static public function registerProperty( $id, $typeid, $label = false, $show = false ) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
394
		DIProperty::registerProperty( $id, $typeid, $label, $show );
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DIProperty::registerProperty() has been deprecated with message: since 2.1, use PropertyRegistry::registerProperty

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
395
	}
396
397
	/**
398
	 * Add a new alias label to an existing datatype id. Note that every ID should have a primary
399
	 * label, either provided by SMW or registered with registerDatatype. This function should be
400
	 * called from within the hook 'smwInitDatatypes'.
401
	 *
402
	 * @deprecated Use DIProperty::registerPropertyAlias(). Will vanish before SMW 1.7.
403
	 */
404
	static public function registerPropertyAlias( $id, $label ) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
405
		DIProperty::registerPropertyAlias( $id, $label );
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DIProperty::registerPropertyAlias() has been deprecated with message: since 2.1, use PropertyRegistry::registerPropertyAlias

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
406
	}
407
408
	/**
409
	 * @see DIProperty::isUserDefined()
410
	 *
411
	 * @deprecated since 1.6
412
	 */
413
	public function isUserDefined() {
414
		return $this->m_dataitem->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...
415
	}
416
417
	/**
418
	 * @see DIProperty::isShown()
419
	 *
420
	 * @deprecated since 1.6
421
	 */
422
	public function isShown() {
423
		return $this->m_dataitem->isShown();
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 isShown() 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...
424
	}
425
426
	/**
427
	 * @see DIProperty::isInverse()
428
	 *
429
	 * @deprecated since 1.6
430
	 */
431
	public function isInverse() {
432
		return $this->m_dataitem->isInverse();
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 isInverse() 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...
433
	}
434
435
	/**
436
	 * Return a DB-key-like string: for visible properties, it is the actual DB key,
437
	 * for internal (invisible) properties, it is the property ID. The value agrees
438
	 * with the first component of getDBkeys() and it can be used in its place.
439
	 * @see DIProperty::getKey()
440
	 *
441
	 * @deprecated since 1.6
442
	 */
443
	public function getDBkey() {
444
		return $this->m_dataitem->getKey();
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 getKey() 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...
445
	}
446
447
	/**
448
	 * @see DIProperty::getLabel()
449
	 *
450
	 * @deprecated since 1.6
451
	 */
452
	public function getText() {
453
		return $this->m_dataitem->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...
454
	}
455
456 199
	private function doNormalizeUserValue( $value ) {
457
458
		if (
459 199
			( $pos = strpos( $value, '#' ) ) !== false && strlen( $value ) > 1 || /* #1567 */
460 199
			( $pos = strpos( $value, '[' ) ) !== false ) /* #1638 */ {
461 2
			$this->addErrorMsg( array( 'smw-datavalue-property-invalid-name', $value, substr(  $value, $pos, 1 ) ) );
462 2
			$this->m_dataitem = new DIProperty( 'ERROR', false );
463
		}
464
465
		// #1727 <Foo> or <Foo-<Bar> are not permitted but
466
		// Foo-<Bar will be converted to Foo-
467 199
		$value = strip_tags( htmlspecialchars_decode( $value ) );
468 199
		$inverse = false;
469
470
		// Enforce upper case for the first character on annotations that are used
471
		// within the property namespace in order to avoid confusion when
472
		// $wgCapitalLinks setting is disabled
473 199
		if ( $this->getContextPage() !== null && $this->getContextPage()->getNamespace() === SMW_NS_PROPERTY ) {
474
			// ucfirst is not utf-8 safe hence the reliance on mb_strtoupper
475 141
			$value = mb_strtoupper( mb_substr( $value, 0, 1 ) ) . mb_substr( $value, 1 );
476
		}
477
478
		// slightly normalise label
479 199
		$propertyName = smwfNormalTitleText( ltrim( rtrim( $value, ' ]' ), ' [' ) );
480
481 199
		if ( ( $propertyName !== '' ) && ( $propertyName { 0 } == '-' ) ) { // property refers to an inverse
482 11
			$propertyName = smwfNormalTitleText( (string)substr( $value, 1 ) );
483
			/// NOTE The cast is necessary at least in PHP 5.3.3 to get string '' instead of boolean false.
484
			/// NOTE It is necessary to normalize again here, since normalization may uppercase the first letter.
485 11
			$inverse = true;
486
		}
487
488 199
		return array( $propertyName, $inverse );
489
	}
490
491
}
492