SentenceNodeSimplifierTest::simplifiableProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace PPP\Wikidata\TreeSimplifier;
4
5
use Mediawiki\Api\MediawikiApi;
6
use PPP\DataModel\MissingNode;
7
use PPP\DataModel\ResourceListNode;
8
use PPP\DataModel\SentenceNode;
9
use PPP\Wikidata\ValueParsers\ResourceListNodeParser;
10
use PPP\Wikidata\ValueParsers\WikibaseValueParserFactory;
11
use PPP\Wikidata\WikibaseResourceNode;
12
use Wikibase\DataModel\Entity\EntityIdValue;
13
use Wikibase\DataModel\Entity\ItemId;
14
use Wikibase\EntityStore\Api\ApiEntityStore;
15
16
/**
17
 * @covers PPP\Wikidata\TreeSimplifier\SentenceNodeSimplifier
18
 *
19
 * @licence AGPLv3+
20
 * @author Thomas Pellissier Tanon
21
 */
22
class SentenceNodeSimplifierTest extends NodeSimplifierBaseTest {
23
24
	protected function buildSimplifier() {
25
		$valueParserFactory = new WikibaseValueParserFactory(
26
			'en',
27
			new ApiEntityStore(new MediawikiApi('http://www‡.wikidata.org/w/api.php'))
28
		);
29
30
		return new SentenceNodeSimplifier(new ResourceListNodeParser($valueParserFactory->newWikibaseValueParser()));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \PPP\Wikidata...ikibaseValueParser())); (PPP\Wikidata\TreeSimplifier\SentenceNodeSimplifier) is incompatible with the return type declared by the abstract method PPP\Wikidata\TreeSimplif...seTest::buildSimplifier of type PPP\Wikidata\TreeSimplifier\NodeSimplifier.

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...
31
	}
32
33
	public function simplifiableProvider() {
34
		return array(
35
			array(
36
				new SentenceNode('')
37
			)
38
		);
39
	}
40
41
	public function nonSimplifiableProvider() {
42
		return array(
43
			array(
44
				new MissingNode()
45
			)
46
		);
47
	}
48
49
	public function simplificationProvider() {
50
		return array(
51
			array(
52
				new ResourceListNode(array(new WikibaseResourceNode('Douglas Adams', new EntityIdValue(new ItemId('Q42'))))),
53
				new SentenceNode('Douglas Adams')
54
			),
55
			array(
56
				new ResourceListNode(),
57
				new SentenceNode('Who are you?')
58
			),
59
		);
60
	}
61
}
62