ResourceListForEntityProperty   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 51
ccs 19
cts 21
cp 0.9048
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getForEntityProperty() 0 10 2
A getSnaksForProperty() 0 7 2
A snaksToNodes() 0 12 3
1
<?php
2
3
namespace PPP\Wikidata\TreeSimplifier;
4
5
use PPP\DataModel\ResourceListNode;
6
use PPP\Wikidata\WikibaseResourceNode;
7
use Wikibase\DataModel\Entity\EntityDocument;
8
use Wikibase\DataModel\Entity\EntityId;
9
use Wikibase\DataModel\Entity\PropertyId;
10
use Wikibase\DataModel\Snak\PropertyValueSnak;
11
use Wikibase\DataModel\Statement\StatementListProvider;
12
use Wikibase\EntityStore\EntityStore;
13
14
/**
15
 * Simplifies a triple node when the object is missing.
16
 *
17
 * @licence AGPLv3+
18
 * @author Thomas Pellissier Tanon
19
 */
20
class ResourceListForEntityProperty {
21
22
	/**
23
	 * @var EntityStore
24
	 */
25
	private $entityStore;
26
27
	/**
28
	 * @param EntityStore $entityStore
29
	 */
30 4
	public function __construct(EntityStore $entityStore) {
31 4
		$this->entityStore = $entityStore;
32 4
	}
33
34
	/**
35
	 * @param EntityId $entityId
36
	 * @param PropertyId $propertyId
37
	 * @return ResourceListNode
38
	 */
39 4
	public function getForEntityProperty(EntityId $entityId, PropertyId $propertyId) {
40 4
		$entity = $this->entityStore->getEntityDocumentLookup()->getEntityDocumentForId($entityId);
41
42 4
		if($entity === null) {
43
			return array();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type documented by PPP\Wikidata\TreeSimplif...y::getForEntityProperty of type PPP\DataModel\ResourceListNode.

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...
44
		}
45
46 4
		$snaks = $this->getSnaksForProperty($entity, $propertyId);
47 4
		return new ResourceListNode($this->snaksToNodes($snaks, $entityId, $propertyId));
48
	}
49
50 4
	private function getSnaksForProperty(EntityDocument $entity, PropertyId $propertyId) {
51 4
		if(!$entity instanceof StatementListProvider) {
52
			return array();
53
		}
54
55 4
		return $entity->getStatements()->getByPropertyId($propertyId)->getBestStatements()->getMainSnaks();
56
	}
57
58 4
	private function snaksToNodes(array $snaks, EntityId $fromSubject, PropertyId $fromProperty) {
59 4
		$nodes = array();
60
61 4
		foreach($snaks as $snak) {
62 3
			if($snak instanceof PropertyValueSnak) {
63 2
				$nodes[] = new WikibaseResourceNode('', $snak->getDataValue(), $fromSubject, $fromProperty);
64 2
			}
65
			//TODO case of PropertySomeValueSnak (MissingNode) and PropertyNoValueSnak (return the negation of the triple?)
66 4
		}
67
68 4
		return $nodes;
69
	}
70
}
71