Completed
Push — master ( 274d0e...9fc64f )
by mw
34:08
created

SMWPropertyValue::highlightText()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3
Metric Value
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.4285
cc 3
eloc 10
nc 2
nop 2
crap 3
1
<?php
2
3
use SMW\DataValueFactory;
4
use SMW\ApplicationFactory;
5
use SMW\Highlighter;
6
7
/**
8
 * Objects of this class represent properties in SMW.
9
 *
10
 * This class represents both normal (user-defined) properties and
11
 * predefined ("special") properties. Predefined properties may still
12
 * have a standard label (and associated wiki article) and they will
13
 * behave just like user-defined properties in most cases (e.g. when
14
 * asking for a printout text, a link to the according page is produced).
15
 * It is possible that predefined properties have no visible label at all,
16
 * if they are used only internally and never specified by or shown to
17
 * the user. Those will use their internal ID as DB key, and
18
 * empty texts for most printouts. All other proeprties use their
19
 * canonical DB key (even if they are predefined and have an id).
20
 * Functions are provided to check whether a property is visible or
21
 * user-defined, and to get the internal ID, if any.
22
 *
23
 * @note This datavalue is used only for representing properties and,
24
 * possibly objects/values, but never for subjects (pages as such). Hence
25
 * it does not provide a complete Title-like interface, or support for
26
 * things like sortkey.
27
 *
28
 * @author Markus Krötzsch
29
 * @ingroup SMWDataValues
30
 */
31
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...
32
33
	/**
34
	 * Cache for wiki page value object associated to this property, or
35
	 * null if no such page exists. Use getWikiPageValue() to get the data.
36
	 * @var SMWWikiPageValue
37
	 */
38
	protected $m_wikipage = null;
39
40
	/**
41
	 * Cache for type value of this property, or null if not calculated yet.
42
	 * @var SMWTypesValue
43
	 */
44
	private $mPropTypeValue;
45
46
	/**
47
	 * @var DIProperty
48
	 */
49
	private $inceptiveProperty = null;
50
51
	/**
52
	 * @since 2.4
53
	 *
54 169
	 * @param string $typeid
55 169
	 */
56 169
	public function __construct( $typeid ) {
57
		parent::__construct( $typeid );
58
	}
59
60
	/**
61
	 * @since 2.4
62
	 *
63 151
	 * @return boolean
64 151
	 */
65
	public function isToFindPropertyRedirect() {
66
		return ( $this->getOptionValueFor( 'smwgDVFeatures' ) & SMW_DV_PROV_REDI ) != 0;
67
	}
68
69
	/**
70
	 * Static function for creating a new property object from a
71
	 * propertyname (string) as a user might enter it.
72
	 * @note The resulting property object might be invalid if
73
	 * the provided name is not allowed. An object is returned
74
	 * in any case.
75
	 *
76
	 * @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...
77
	 *
78 90
	 * @return SMWPropertyValue
79 90
	 */
80
	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...
81
		return DataValueFactory::getInstance()->newPropertyValueByLabel( $propertyLabel );
82
	}
83
84
	/**
85
	 * Static function for creating a new property object from a property
86
	 * identifier (string) as it might be used internally. This might be
87
	 * the DB key version of some property title text or the id of a
88
	 * predefined property (such as '_TYPE').
89
	 * @note This function strictly requires an internal identifier, i.e.
90
	 * predefined properties must be referred to by their ID, and '-' is
91
	 * not supported for indicating inverses.
92
	 * @note The resulting property object might be invalid if
93
	 * the provided name is not allowed. An object is returned
94
	 * in any case.
95
	 */
96
	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...
97
		$diProperty = new SMWDIProperty( $propertyid );
98
		$dvProperty = new SMWPropertyValue( '__pro' );
99
		$dvProperty->setDataItem( $diProperty );
100
		return $dvProperty;
101
	}
102
103
	/**
104
	 * We use the internal wikipage object to store some of this objects data.
105
	 * Clone it to make sure that data can be modified independently from the
106
	 * original object's content.
107
	 */
108
	public function __clone() {
109
		if ( !is_null( $this->m_wikipage ) ) {
110
			$this->m_wikipage = clone $this->m_wikipage;
111
		}
112
	}
113
114
	/**
115
	 * @note If the inceptive property and the property referenced in dataItem
116
	 * are not equal then the dataItem represents the end target to which the
117
	 * inceptive property has been redirected.
118
	 *
119
	 * @since 2.4
120
	 *
121
	 * @return DIProperty
122
	 */
123
	public function getInceptiveProperty() {
124
		return $this->inceptiveProperty;
125
	}
126
127
	/**
128
	 * Extended parsing function to first check whether value refers to pre-defined
129
	 * property, resolve aliases, and set internal property id accordingly.
130 147
	 * @todo Accept/enforce property namespace.
131 147
	 */
132 147
	protected function parseUserValue( $value ) {
133
		$this->mPropTypeValue = null;
134 147
		$this->m_wikipage = null;
135 147
136 147
		if ( $this->m_caption === false ) { // always use this as caption
137 147
			$this->m_caption = $value;
138 147
		}
139 147
		$propertyName = smwfNormalTitleText( ltrim( rtrim( $value, ' ]' ), ' [' ) ); // slightly normalise label
140 8
		$inverse = false;
141
		if ( ( $propertyName !== '' ) && ( $propertyName { 0 } == '-' ) ) { // property refers to an inverse
142
			$propertyName = smwfNormalTitleText( (string)substr( $value, 1 ) );
143 8
			/// NOTE The cast is necessary at least in PHP 5.3.3 to get string '' instead of boolean false.
144 8
			/// NOTE It is necessary to normalize again here, since normalization may uppercase the first letter.
145
			$inverse = true;
146
		}
147 147
148 147
		try {
149
			$this->m_dataitem = SMWDIProperty::newFromUserLabel( $propertyName, $inverse, $this->m_typeid );
0 ignored issues
show
Unused Code introduced by
The call to SMWDIProperty::newFromUserLabel() has too many arguments starting with $this->m_typeid.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
150
		} catch ( SMWDataItemException $e ) { // happens, e.g., when trying to sort queries by property "-"
151
			$this->addError( wfMessage( 'smw_noproperty', $value )->inContentLanguage()->text() );
152
			$this->m_dataitem = new SMWDIProperty( 'ERROR', false ); // just to have something
153 147
		}
154
155 147
		$this->inceptiveProperty = $this->m_dataitem;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->m_dataitem of type object<SMW\DIProperty> is incompatible with the declared type object<DIProperty> of property $inceptiveProperty.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
156 147
157 147
		if ( $this->isToFindPropertyRedirect() ) {
158 147
			$this->m_dataitem = $this->m_dataitem->getRedirectTarget();
159
		}
160
	}
161
162
	/**
163
	 * @see SMWDataValue::loadDataItem()
164
	 * @param $dataitem SMWDataItem
165 26
	 * @return boolean
166
	 */
167 26
	protected function loadDataItem( SMWDataItem $dataItem ) {
168
169
		if ( $dataItem->getDIType() !== SMWDataItem::TYPE_PROPERTY ) {
170
			return false;
171 26
		}
172 26
173
		$this->inceptiveProperty = $dataItem;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dataItem of type object<SMWDataItem> is incompatible with the declared type object<DIProperty> of property $inceptiveProperty.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
174 26
		$this->m_dataitem = $dataItem;
175 26
176 26
		$this->mPropTypeValue = null;
177
		unset( $this->m_wikipage );
178 26
		$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...
179
180
		return true;
181 69
	}
182 69
183 69
	public function setCaption( $caption ) {
184 69
		parent::setCaption( $caption );
185 69
		if ( $this->getWikiPageValue() instanceof SMWDataValue ) { // pass caption to embedded datavalue (used for printout)
186 69
			$this->m_wikipage->setCaption( $caption );
187
		}
188
	}
189
190
	public function setOutputFormat( $formatstring ) {
191
		$this->m_outformat = $formatstring;
192
		if ( $this->m_wikipage instanceof SMWDataValue ) {
193
			$this->m_wikipage->setOutputFormat( $formatstring );
194
		}
195
	}
196
197
	public function setInverse( $isinverse ) {
198
		return $this->m_dataitem = new SMWDIProperty( $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...
199
	}
200
201
	/**
202
	 * Return a wiki page value that can be used for displaying this
203
	 * property, or null if no such wiki page exists (for predefined
204
	 * properties without any label).
205 72
	 * @return SMWWikiPageValue or null
206 72
	 */
207
	public function getWikiPageValue() {
208 72
		if ( !isset( $this->m_wikipage ) ) {
209
210 72
			$diWikiPage = $this->m_dataitem->getDiWikiPage();
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 getDiWikiPage() 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...
211 72
212 72
			if ( $diWikiPage !== null ) {
213 72
				$this->m_wikipage = \SMW\DataValueFactory::getInstance()->newDataItemValue( $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...
214 72
				$this->m_wikipage->setOutputFormat( $this->m_outformat );
215
				$this->addError( $this->m_wikipage->getErrors() );
216
			} else { // should rarely happen ($value is only changed if the input $value really was a label for a predefined prop)
217 72
				$this->m_wikipage = null;
218 72
			}
219
		}
220
		return $this->m_wikipage;
221
	}
222
223
	/**
224
	 * Return TRUE if this is a property that can be displayed, and not a pre-defined
225
	 * property that is used only internally and does not even have a user-readable name.
226 39
	 * @note Every user defined property is necessarily visible.
227 39
	 */
228
	public function isVisible() {
229
		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...
230
	}
231
232
	/**
233
	 * @since 2.2
234
	 *
235 131
	 * @return boolean
236 131
	 */
237
	public function canUse() {
238
		return $this->isValid() && $this->m_dataitem->isUnrestrictedForUse();
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 isUnrestrictedForUse() 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...
239 16
	}
240
241 16
	public function getShortWikiText( $linked = null ) {
242 16
243 16
		if ( $this->isVisible() ) {
244
			$wikiPageValue = $this->getWikiPageValue();
245
			return is_null( $wikiPageValue ) ? '' : $this->highlightText( $wikiPageValue->getShortWikiText( $linked ) );
246
		}
247
248
		return '';
249 1
	}
250
251 1
	public function getShortHTMLText( $linked = null ) {
252 1
253 1
		if ( $this->isVisible() ) {
254
			$wikiPageValue = $this->getWikiPageValue();
255
			return is_null( $wikiPageValue ) ? '' : $this->highlightText( $wikiPageValue->getShortHTMLText( $linked ), $linked );
256
		}
257
258
		return '';
259 8
	}
260
261 8
	public function getLongWikiText( $linked = null ) {
262 8
263 8
		if ( $this->isVisible() ) {
264
			$wikiPageValue = $this->getWikiPageValue();
265
			return is_null( $wikiPageValue ) ? '' : $this->highlightText( $wikiPageValue->getLongWikiText( $linked ) );
266
		}
267
268
		return '';
269
	}
270
271
	public function getLongHTMLText( $linked = null ) {
272
273
		if ( $this->isVisible() ) {
274
			$wikiPageValue = $this->getWikiPageValue();
275
			return is_null( $wikiPageValue ) ? '' : $this->highlightText( $wikiPageValue->getLongHTMLText( $linked ), $linked );
276
		}
277
278
		return '';
279 34
	}
280 34
281
	public function getWikiValue() {
282
		return $this->isVisible() ? $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...
283
	}
284
285
	/**
286
	 * If this property was not user defined, return the internal ID string referring to
287
	 * that property. Otherwise return FALSE;
288
	 */
289
	public function getPropertyID() {
290
		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...
291
	}
292
293
	/**
294
	 * Return an SMWTypesValue object representing the datatype of this
295
	 * property.
296
	 * @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.
297
	 */
298
	public function getTypesValue() {
299
		$result = SMWTypesValue::newFromTypeId( $this->getPropertyTypeID() );
300
		if ( !$this->isValid() ) {
301
			$result->addError( $this->getErrors() );
302
		}
303
		return $result;
304
	}
305
306
	/**
307
	 * Convenience method to find the type id of this property. Most callers
308
	 * should rather use SMWDIProperty::findPropertyTypeId() directly. Note
309
	 * that this is not the same as getTypeID(), which returns the id of
310
	 * this property datavalue.
311
	 *
312 4
	 * @return string
313 4
	 */
314 4
	public function getPropertyTypeID() {
315
		if ( $this->isValid() ) {
316
			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...
317
		} else {
318
			return '__err';
319
		}
320
	}
321
322
	/**
323 22
	 * Create special highlighting for hinting at special properties.
324
	 */
325 22
	protected function highlightText( $text, $linker = null ) {
326
327 4
		$propertySpecificationLookup = ApplicationFactory::getInstance()->getPropertySpecificationLookup();
328 4
329
		if ( ( $content = $propertySpecificationLookup->getPropertyDescriptionFor( $this->m_dataitem, $linker ) ) !== '' ) {
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...
330 4
331 4
			$highlighter = SMW\Highlighter::factory( SMW\Highlighter::TYPE_PROPERTY );
332 4
			$highlighter->setContent( array (
333
				'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...
334 4
				'caption' => $text,
335 4
				'content' => $content !== '' ? $content : wfMessage( 'smw_isspecprop' )->text()
336 4
			) );
337 4
338 4
			return $highlighter->getHtml();
339
		}
340 4
341
		return $text;
342
	}
343 19
344
	/**
345
	 * A function for registering/overwriting predefined properties for SMW. Should be called from
346
	 * within the hook 'smwInitProperties'. Ids should start with three underscores "___" to avoid
347
	 * current and future confusion with SMW built-ins.
348
	 *
349
	 * @deprecated Use SMWDIProperty::registerProperty(). Will vanish before SMW 1.7.
350
	 */
351
	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...
352
		SMWDIProperty::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...
353
	}
354
355
	/**
356
	 * Add a new alias label to an existing datatype id. Note that every ID should have a primary
357
	 * label, either provided by SMW or registered with registerDatatype. This function should be
358
	 * called from within the hook 'smwInitDatatypes'.
359
	 *
360
	 * @deprecated Use SMWDIProperty::registerPropertyAlias(). Will vanish before SMW 1.7.
361
	 */
362
	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...
363
		SMWDIProperty::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...
364
	}
365
366
	/**
367
	 * @see SMWDIProperty::isUserDefined()
368
	 *
369
	 * @deprecated since 1.6
370
	 */
371
	public function isUserDefined() {
372
		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...
373
	}
374
375
	/**
376
	 * @see SMWDIProperty::isShown()
377
	 *
378
	 * @deprecated since 1.6
379
	 */
380
	public function isShown() {
381
		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...
382
	}
383
384
	/**
385
	 * @see SMWDIProperty::isInverse()
386
	 *
387
	 * @deprecated since 1.6
388
	 */
389
	public function isInverse() {
390
		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...
391 79
	}
392 79
393
	/**
394
	 * Return a DB-key-like string: for visible properties, it is the actual DB key,
395
	 * for internal (invisible) properties, it is the property ID. The value agrees
396
	 * with the first component of getDBkeys() and it can be used in its place.
397
	 * @see SMWDIProperty::getKey()
398
	 *
399
	 * @deprecated since 1.6
400
	 */
401
	public function getDBkey() {
402
		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...
403
	}
404
405
	/**
406
	 * @see SMWDIProperty::getLabel()
407
	 *
408
	 * @deprecated since 1.6
409
	 */
410
	public function getText() {
411
		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...
412 4
	}
413 4
414
}
415