Completed
Push — master ( 9eae06...a41cb5 )
by mw
16s
created

MonolingualTextValueDescriptionDeserializer.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SMW\Deserializers\DVDescriptionDeserializer;
4
5
use InvalidArgumentException;
6
use SMW\DataValueFactory;
7
use SMW\DataValues\MonolingualTextValue;
8
use SMW\Query\Language\Conjunction;
9
use SMW\Query\Language\SomeProperty;
10
use SMW\Query\Language\ThingDescription;
11
use SMW\Query\Language\ValueDescription;
12
use SMWDIBlob as DIBlob;
13
14
/**
15
 * @private
16
 *
17
 * @license GNU GPL v2+
18
 * @since 2.4
19
 *
20
 * @author mwjames
21
 */
22
class MonolingualTextValueDescriptionDeserializer extends DescriptionDeserializer {
23
24
	/**
25
	 * @since 2.4
26
	 *
27
	 * {@inheritDoc}
28
	 */
29 99
	public function isDeserializerFor( $serialization ) {
30 99
		return $serialization instanceof MonolingualTextValue;
31
	}
32
33
	/**
34
	 * @since 2.4
35
	 *
36
	 * @param string $value
37
	 *
38
	 * @return Description
39
	 * @throws InvalidArgumentException
40
	 */
41 13
	public function deserialize( $value ) {
42
43 13
		if ( !is_string( $value ) ) {
44 1
			throw new InvalidArgumentException( 'Value needs to be a string' );
45
		}
46
47 12
		if ( $value === '' ) {
48 1
			$this->addError( wfMessage( 'smw_novalues' )->text() );
49 1
			return new ThingDescription();
50
		}
51
52 11
		$subdescriptions = array();
53 11
		list( $text, $languageCode ) = $this->dataValue->getValuesFromString( $value );
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class SMWDataValue as the method getValuesFromString() does only exist in the following sub-classes of SMWDataValue: SMWRecordValue, SMW\DataValues\MonolingualTextValue. 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...
54
55 11
		foreach ( $this->dataValue->getPropertyDataItems() as $property ) {
56
57
			// If the DVFeature doesn't require a language code to be present then
58
			// allow to skip it as conjunctive condition when it is empty
59
			if (
60 11
				( $languageCode === '' ) &&
61 11
				( $property->getKey() === '_LCODE' ) &&
62 11
				( !$this->dataValue->isEnabledFeature( SMW_DV_MLTV_LCODE ) ) ) {
63 2
				continue;
64
			}
65
66 11
			$value = $property->getKey() === '_LCODE' ? $languageCode : $text;
67 11
			$comparator = SMW_CMP_EQ;
68
69 11
			$this->prepareValue( $value, $comparator );
70
71
			// Directly use the DI instead of going through the DVFactory to
72
			// avoid having ~zh-* being validated when building a DV
73
			// If one of the values is empty use, ? so queries can be arbitrary
74
			// in respect of the query condition
75 11
			$dataValue = DataValueFactory::getInstance()->newDataValueByItem(
76 11
				new DIBlob( $value === '' ? '?' : $value ),
77
				$property,
78 11
				false,
79 11
				$this->dataValue->getContextPage()
80
			);
81
82 11
			if ( !$dataValue->isValid() ) {
83
				$this->addError( $dataValue->getErrors() );
84
				continue;
85
			}
86
87 11
			$subdescriptions[] = $this->newSubdescription( $dataValue, $comparator );
88
		}
89
90 11
		return $this->getFinalDescriptionFor( $subdescriptions );
91
	}
92
93 11
	private function getFinalDescriptionFor( $subdescriptions ) {
94
95 11
		$count = count( $subdescriptions );
96
97 11
		if ( $count == 0 ) {
98
			return new ThingDescription();
99
		}
100
101 11
		if ( $count == 1 ) {
102 2
			return reset( $subdescriptions );
103
		}
104
105 9
		return new Conjunction( $subdescriptions );
106
	}
107
108 11
	private function newSubdescription( $dataValue, $comparator ) {
109
110 11
		$description = new ValueDescription(
111 11
			$dataValue->getDataItem(),
112 11
			$dataValue->getProperty(),
113
			$comparator
114
		);
115
116 11
		if ( $dataValue->getWikiValue() === '+' || $dataValue->getWikiValue() === '?' ) {
117 7
			$description = new ThingDescription();
118
		}
119
120 11
		return new SomeProperty(
121 11
			$dataValue->getProperty(),
122
			$description
123
		);
124
	}
125
126
}
127