Completed
Push — master ( 59c431...219a45 )
by mw
36:38
created

SMWPropertyValue::parseUserValue()   D

Complexity

Conditions 9
Paths 72

Size

Total Lines 45
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 9.3752

Importance

Changes 0
Metric Value
cc 9
eloc 24
nc 72
nop 1
dl 0
loc 45
ccs 15
cts 18
cp 0.8333
crap 9.3752
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
use SMW\ApplicationFactory;
4
use SMW\DataValues\ValueFormatters\DataValueFormatter;
5
use SMW\DataValueFactory;
6
use SMW\DIProperty;
7
use SMW\Highlighter;
8
use SMW\Message;
9
10
/**
11
 * Objects of this class represent properties in SMW.
12
 *
13
 * This class represents both normal (user-defined) properties and
14
 * predefined ("special") properties. Predefined properties may still
15
 * have a standard label (and associated wiki article) and they will
16
 * behave just like user-defined properties in most cases (e.g. when
17
 * asking for a printout text, a link to the according page is produced).
18
 * It is possible that predefined properties have no visible label at all,
19
 * if they are used only internally and never specified by or shown to
20
 * the user. Those will use their internal ID as DB key, and
21
 * empty texts for most printouts. All other proeprties use their
22
 * canonical DB key (even if they are predefined and have an id).
23
 * Functions are provided to check whether a property is visible or
24
 * user-defined, and to get the internal ID, if any.
25
 *
26
 * @note This datavalue is used only for representing properties and,
27
 * possibly objects/values, but never for subjects (pages as such). Hence
28
 * it does not provide a complete Title-like interface, or support for
29
 * things like sortkey.
30
 *
31
 * @author Markus Krötzsch
32
 * @ingroup SMWDataValues
33
 */
34
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...
35
36
	/**
37
	 * Avoid the display of a tooltip
38
	 */
39
	const OPT_NO_HIGHLIGHT = 'no.highlight';
40
41
	/**
42
	 * Cache for wiki page value object associated to this property, or
43
	 * null if no such page exists. Use getWikiPageValue() to get the data.
44
	 * @var SMWWikiPageValue
45
	 */
46
	protected $m_wikipage = null;
47
48
	/**
49
	 * @var array
50
	 */
51
	protected $linkAttributes = array();
52
53
	/**
54
	 * @var string
55
	 */
56
	private $preferredLabel = '';
57
58
	/**
59
	 * Cache for type value of this property, or null if not calculated yet.
60
	 * @var SMWTypesValue
61
	 */
62
	private $mPropTypeValue;
63
64
	/**
65
	 * @var DIProperty
66
	 */
67
	private $inceptiveProperty = null;
68 222
69 222
	/**
70 222
	 * @since 2.4
71
	 *
72
	 * @param string $typeid
73
	 */
74
	public function __construct( $typeid = '__pro' ) {
75
		parent::__construct( $typeid );
76
	}
77
78
	/**
79
	 * Static function for creating a new property object from a
80
	 * propertyname (string) as a user might enter it.
81
	 * @note The resulting property object might be invalid if
82
	 * the provided name is not allowed. An object is returned
83 132
	 * in any case.
84 132
	 *
85
	 * @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...
86
	 *
87
	 * @return SMWPropertyValue
88
	 */
89
	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...
90
		return DataValueFactory::getInstance()->newPropertyValueByLabel( $propertyLabel );
91
	}
92
93
	/**
94
	 * Static function for creating a new property object from a property
95
	 * identifier (string) as it might be used internally. This might be
96
	 * the DB key version of some property title text or the id of a
97
	 * predefined property (such as '_TYPE').
98
	 * @note This function strictly requires an internal identifier, i.e.
99
	 * predefined properties must be referred to by their ID, and '-' is
100
	 * not supported for indicating inverses.
101
	 * @note The resulting property object might be invalid if
102
	 * the provided name is not allowed. An object is returned
103
	 * in any case.
104
	 */
105
	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...
106
		$diProperty = new DIProperty( $propertyid );
107
		$dvProperty = new SMWPropertyValue( '__pro' );
108
		$dvProperty->setDataItem( $diProperty );
109
		return $dvProperty;
110
	}
111 1
112 1
	/**
113 1
	 * We use the internal wikipage object to store some of this objects data.
114
	 * Clone it to make sure that data can be modified independently from the
115 1
	 * original object's content.
116
	 */
117
	public function __clone() {
118
		if ( !is_null( $this->m_wikipage ) ) {
119
			$this->m_wikipage = clone $this->m_wikipage;
120
		}
121
	}
122
123
	/**
124
	 * @note If the inceptive property and the property referenced in dataItem
125
	 * are not equal then the dataItem represents the end target to which the
126 1
	 * inceptive property has been redirected.
127 1
	 *
128
	 * @since 2.4
129
	 *
130
	 * @return DIProperty
131
	 */
132
	public function getInceptiveProperty() {
133
		return $this->inceptiveProperty;
134
	}
135 203
136 203
	/**
137 203
	 * Extended parsing function to first check whether value refers to pre-defined
138
	 * property, resolve aliases, and set internal property id accordingly.
139 203
	 * @todo Accept/enforce property namespace.
140 203
	 */
141
	protected function parseUserValue( $value ) {
142
		$this->mPropTypeValue = null;
143 203
		$this->m_wikipage = null;
144
145
		list( $propertyName, $inverse ) = $this->doNormalizeUserValue(
146
			$value
147 203
		);
148
149
		$contentLanguage = $this->getOptionBy( self::OPT_CONTENT_LANGUAGE );
150 203
151 3
		try {
152 3
			$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 149 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...
153 3
		} catch ( SMWDataItemException $e ) { // happens, e.g., when trying to sort queries by property "-"
154
			$this->addErrorMsg( array( 'smw_noproperty', $value ) );
155
			$this->m_dataitem = new DIProperty( 'ERROR', false ); // just to have something
156
		}
157 203
158
		// @see the SMW_DV_PROV_DTITLE explanation
159
		if ( $this->isEnabledFeature( SMW_DV_PROV_DTITLE ) ) {
160
			$dataItem = ApplicationFactory::getInstance()->getPropertySpecificationLookup()->getPropertyFromDisplayTitle(
161
				$value
162
			);
163
164
			$this->m_dataitem = $dataItem ? $dataItem : $this->m_dataitem;
165 203
		}
166
167 203
		// Copy the original DI to ensure we can compare it against a possible redirect
168 195
		$this->inceptiveProperty = $this->m_dataitem;
169
170 203
		if ( $this->isEnabledFeature( SMW_DV_PROV_REDI ) ) {
171
			$this->m_dataitem = $this->m_dataitem->getRedirectTarget();
172
		}
173
174
		// If no external caption has been invoked then fetch a preferred label
175
		if ( $this->m_caption === false || $this->m_caption === '' ) {
176
			$this->preferredLabel = $this->m_dataitem->getPreferredLabel( $this->getOptionBy( self::OPT_USER_LANGUAGE ) );
0 ignored issues
show
Security Bug introduced by
It seems like $this->getOptionBy(self::OPT_USER_LANGUAGE) targeting SMWDataValue::getOptionBy() can also be of type false; however, SMW\DIProperty::getPreferredLabel() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
177 32
		}
178
179 32
		// Use the preferred label as first choice for a caption, if available
180
		if ( $this->preferredLabel !== '' ) {
181
			$this->m_caption = $this->preferredLabel;
182
		} elseif ( $this->m_caption === false ) {
183 32
			$this->m_caption = $value;
184 32
		}
185
	}
186 32
187 32
	/**
188 32
	 * @see SMWDataValue::loadDataItem()
189 32
	 * @param $dataitem SMWDataItem
190
	 * @return boolean
191 32
	 */
192
	protected function loadDataItem( SMWDataItem $dataItem ) {
193
194
		if ( $dataItem->getDIType() !== SMWDataItem::TYPE_PROPERTY ) {
195
			return false;
196
		}
197
198
		$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...
199 1
		$this->m_dataitem = $dataItem;
200 1
		$this->preferredLabel = $this->m_dataitem->getPreferredLabel();
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 getPreferredLabel() 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...
201
202 1
		$this->mPropTypeValue = null;
203 1
		unset( $this->m_wikipage );
204
		$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...
205 1
		$this->linkAttributes = array();
206
207 105
		if ( $this->preferredLabel !== '' ) {
208 105
			$this->m_caption = $this->preferredLabel;
209 105
		}
210 105
211
		return true;
212 105
	}
213
214
	/**
215
	 * @since 2.5
216
	 *
217
	 * @return string
218
	 */
219
	public function getPreferredLabel() {
220
		return $this->preferredLabel;
221
	}
222
223
	/**
224
	 * @since 2.4
225
	 *
226
	 * @param array $linkAttributes
227
	 */
228
	public function setLinkAttributes( array $linkAttributes ) {
229
		$this->linkAttributes = $linkAttributes;
230
231 110
		if ( $this->getWikiPageValue() instanceof SMWDataValue ) {
232
			$this->m_wikipage->setLinkAttributes( $linkAttributes );
233 110
		}
234 69
	}
235
236
	public function setCaption( $caption ) {
237 110
		parent::setCaption( $caption );
238
		if ( $this->getWikiPageValue() instanceof SMWDataValue ) { // pass caption to embedded datavalue (used for printout)
239 110
			$this->m_wikipage->setCaption( $caption );
240 110
		}
241 110
	}
242 110
243 110
	public function setOutputFormat( $formatstring ) {
244 110
		$this->m_outformat = $formatstring;
245
		if ( $this->getWikiPageValue() instanceof SMWDataValue ) {
246
			$this->m_wikipage->setOutputFormat( $formatstring );
247
		}
248
	}
249 110
250
	public function setInverse( $isinverse ) {
251
		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...
252
	}
253
254
	/**
255
	 * Return a wiki page value that can be used for displaying this
256
	 * property, or null if no such wiki page exists (for predefined
257 95
	 * properties without any label).
258 95
	 * @return SMWWikiPageValue or null
259
	 */
260
	public function getWikiPageValue() {
261
262
		if ( isset( $this->m_wikipage ) ) {
263
			return $this->m_wikipage;
264
		}
265
266 181
		$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...
267 181
268
		if ( $diWikiPage !== null ) {
269
			$this->m_wikipage = 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...
270 41
			$this->m_wikipage->setOutputFormat( $this->m_outformat );
271
			$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...
272 41
			$this->m_wikipage->setOptions( $this->getOptions() );
273 40
			$this->addError( $this->m_wikipage->getErrors() );
274 40
		} else { // should rarely happen ($value is only changed if the input $value really was a label for a predefined prop)
275
			$this->m_wikipage = null;
276
		}
277 1
278
		return $this->m_wikipage;
279
	}
280 8
281
	/**
282 8
	 * Return TRUE if this is a property that can be displayed, and not a pre-defined
283 8
	 * property that is used only internally and does not even have a user-readable name.
284 8
	 * @note Every user defined property is necessarily visible.
285
	 */
286
	public function isVisible() {
287
		return $this->isValid() && ( $this->m_dataitem->isUserDefined() || $this->m_dataitem->getCanonicalLabel() !== '' );
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 getCanonicalLabel() 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...
288
	}
289
290 12
	/**
291
	 * @since 2.2
292 12
	 *
293 12
	 * @return boolean
294 12
	 */
295
	public function canUse() {
296
		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...
297
	}
298
299
	/**
300
	 * @see DataValue::getShortWikiText
301
	 *
302
	 * @return string
303
	 */
304
	public function getShortWikiText( $linker = null ) {
305
		return $this->getDataValueFormatter()->format( DataValueFormatter::WIKI_SHORT, $linker );
306
	}
307
308
	/**
309
	 * @see DataValue::getShortHTMLText
310 68
	 *
311
	 * @return string
312 68
	 */
313
	public function getShortHTMLText( $linker = null ) {
314
		return $this->getDataValueFormatter()->format( DataValueFormatter::HTML_SHORT, $linker );
315
	}
316 68
317 1
	/**
318
	 * @see DataValue::getLongWikiText
319
	 *
320 68
	 * @return string
321
	 */
322
	public function getLongWikiText( $linker = null ) {
323
		return $this->getDataValueFormatter()->format( DataValueFormatter::WIKI_LONG, $linker );
324
	}
325
326
	/**
327
	 * @see DataValue::getLongHTMLText
328
	 *
329
	 * @return string
330
	 */
331
	public function getLongHTMLText( $linker = null ) {
332
		return $this->getDataValueFormatter()->format( DataValueFormatter::HTML_LONG, $linker );
333
	}
334
335
	/**
336
	 * @see DataValue::getWikiValue
337
	 *
338
	 * @return string
339
	 */
340
	public function getWikiValue() {
341
		return $this->getDataValueFormatter()->format( DataValueFormatter::VALUE );
342
	}
343
344
	/**
345
	 * If this property was not user defined, return the internal ID string referring to
346
	 * that property. Otherwise return FALSE;
347
	 */
348
	public function getPropertyID() {
349
		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...
350
	}
351
352 4
	/**
353 4
	 * Return an SMWTypesValue object representing the datatype of this
354 4
	 * property.
355
	 * @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.
356
	 */
357
	public function getTypesValue() {
358
		$result = SMWTypesValue::newFromTypeId( $this->getPropertyTypeID() );
359
		if ( !$this->isValid() ) {
360
			$result->addError( $this->getErrors() );
361
		}
362
		return $result;
363 52
	}
364
365 52
	/**
366 2
	 * Convenience method to find the type id of this property. Most callers
367
	 * should rather use DIProperty::findPropertyTypeId() directly. Note
368
	 * that this is not the same as getTypeID(), which returns the id of
369 52
	 * this property datavalue.
370
	 *
371 52
	 * @return string
372
	 */
373 15
	public function getPropertyTypeID() {
374 15
		if ( $this->isValid() ) {
375 15
			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...
376 15
		} else {
377 15
			return '__err';
378
		}
379
	}
380 15
381
	/**
382
	 * A function for registering/overwriting predefined properties for SMW. Should be called from
383 46
	 * within the hook 'smwInitProperties'. Ids should start with three underscores "___" to avoid
384
	 * current and future confusion with SMW built-ins.
385
	 *
386
	 * @deprecated Use DIProperty::registerProperty(). Will vanish before SMW 1.7.
387
	 */
388
	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...
389
		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...
390
	}
391
392
	/**
393
	 * Add a new alias label to an existing datatype id. Note that every ID should have a primary
394
	 * label, either provided by SMW or registered with registerDatatype. This function should be
395
	 * called from within the hook 'smwInitDatatypes'.
396
	 *
397
	 * @deprecated Use DIProperty::registerPropertyAlias(). Will vanish before SMW 1.7.
398
	 */
399
	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...
400
		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...
401
	}
402
403
	/**
404
	 * @see DIProperty::isUserDefined()
405
	 *
406
	 * @deprecated since 1.6
407
	 */
408
	public function isUserDefined() {
409
		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...
410
	}
411
412
	/**
413
	 * @see DIProperty::isShown()
414
	 *
415
	 * @deprecated since 1.6
416
	 */
417
	public function isShown() {
418
		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...
419
	}
420
421
	/**
422
	 * @see DIProperty::isInverse()
423
	 *
424
	 * @deprecated since 1.6
425
	 */
426
	public function isInverse() {
427
		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...
428
	}
429
430
	/**
431
	 * Return a DB-key-like string: for visible properties, it is the actual DB key,
432
	 * for internal (invisible) properties, it is the property ID. The value agrees
433
	 * with the first component of getDBkeys() and it can be used in its place.
434
	 * @see DIProperty::getKey()
435
	 *
436
	 * @deprecated since 1.6
437
	 */
438
	public function getDBkey() {
439
		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...
440
	}
441
442
	/**
443
	 * @see DIProperty::getLabel()
444
	 *
445
	 * @deprecated since 1.6
446
	 */
447
	public function getText() {
448
		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...
449
	}
450
451
	private function doNormalizeUserValue( $value ) {
452
453
		if (
454
			( $pos = strpos( $value, '#' ) ) !== false && strlen( $value ) > 1 || /* #1567 */
455
			( $pos = strpos( $value, '[' ) ) !== false ) /* #1638 */ {
456 203
			$this->addErrorMsg( array( 'smw-datavalue-property-invalid-name', $value, substr(  $value, $pos, 1 ) ) );
457
			$this->m_dataitem = new DIProperty( 'ERROR', false );
458
		}
459 203
460 203
		// #1727 <Foo> or <Foo-<Bar> are not permitted but
461 2
		// Foo-<Bar will be converted to Foo-
462 2
		$value = strip_tags( htmlspecialchars_decode( $value ) );
463
		$inverse = false;
464
465
		// Enforce upper case for the first character on annotations that are used
466
		// within the property namespace in order to avoid confusion when
467 203
		// $wgCapitalLinks setting is disabled
468 203
		if ( $this->getContextPage() !== null && $this->getContextPage()->getNamespace() === SMW_NS_PROPERTY ) {
469
			// ucfirst is not utf-8 safe hence the reliance on mb_strtoupper
470
			$value = mb_strtoupper( mb_substr( $value, 0, 1 ) ) . mb_substr( $value, 1 );
471
		}
472
473 203
		// slightly normalise label
474
		$propertyName = smwfNormalTitleText( ltrim( rtrim( $value, ' ]' ), ' [' ) );
475 145
476
		if ( ( $propertyName !== '' ) && ( $propertyName { 0 } == '-' ) ) { // property refers to an inverse
477
			$propertyName = smwfNormalTitleText( (string)substr( $value, 1 ) );
478
			/// NOTE The cast is necessary at least in PHP 5.3.3 to get string '' instead of boolean false.
479 203
			/// NOTE It is necessary to normalize again here, since normalization may uppercase the first letter.
480
			$inverse = true;
481 203
		}
482 11
483
		return array( $propertyName, $inverse );
484
	}
485 11
486
}
487