Completed
Push — master ( 7880aa...3f359c )
by Thomas
05:14
created

WikibaseValueParser::parse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4285
ccs 2
cts 2
cp 1
cc 3
eloc 10
nc 3
nop 2
crap 3
1
<?php
2
3
namespace PPP\Wikidata\ValueParsers;
4
5
use DataValues\DataValue;
6
use ValueParsers\ParseException;
7
use ValueParsers\ValueParser;
8
9
/**
10
 * Choose the right parser for the given type and return the parsed value.
11
 *
12
 * @licence GPLv2+
13
 * @author Thomas Pellissier Tanon
14
 */
15
class WikibaseValueParser {
16
17
	/**
18
	 * @var ValueParser[]
19
	 */
20
	public $parsers;
21
22
	/**
23
	 * @param ValueParser[] $parsers
24
	 */
25 2
	public function __construct(array $parsers) {
26 2
		$this->parsers = $parsers;
27
	}
28
29
	/**
30
	 * @param string $value
31
	 * @param string $type
32
	 * @return DataValue[]
33
	 */
34 1
	public function parse($value, $type) {
35
		if(!array_key_exists($type, $this->parsers)) {
36
			throw new ParseException(
37
				$type . ' is not one of the type supported by the value parser (' . implode(', ', array_keys($this->parsers)) . ')',
38
				$value,
39
				$type
40
			);
41
		}
42
43
		$result = $this->parsers[$type]->parse($value);
44
45
		if(!is_array($result)) {
46 1
			return array($result);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array($result); (array<object|integer|double|string|null|boolean>) is incompatible with the return type documented by PPP\Wikidata\ValueParser...ibaseValueParser::parse of type DataValues\DataValue[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
47
		}
48
49
		return $result;
50
	}
51
}
52