SentenceNodeSimplifier   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 61.53%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 39
ccs 8
cts 13
cp 0.6153
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isSimplifierFor() 0 3 1
A simplify() 0 7 2
A doSimplification() 0 6 1
1
<?php
2
3
namespace PPP\Wikidata\TreeSimplifier;
4
5
use InvalidArgumentException;
6
use PPP\DataModel\AbstractNode;
7
use PPP\DataModel\ResourceListNode;
8
use PPP\DataModel\SentenceNode;
9
use PPP\DataModel\StringResourceNode;
10
use PPP\Module\TreeSimplifier\NodeSimplifier;
11
use PPP\Wikidata\ValueParsers\ResourceListNodeParser;
12
13
/**
14
 * Tries to cast the input sentence to a Wikibase item
15
 *
16
 * @licence AGPLv3+
17
 * @author Thomas Pellissier Tanon
18
 */
19
class SentenceNodeSimplifier implements NodeSimplifier {
20
21
	/**
22
	 * @var ResourceListNodeParser
23
	 */
24
	private $resourceListNodeParser;
25
26
	/**
27
	 * @param ResourceListNodeParser $resourceListNodeParser
28
	 */
29 4
	public function __construct(ResourceListNodeParser $resourceListNodeParser) {
30 4
		$this->resourceListNodeParser = $resourceListNodeParser;
31 4
	}
32
33
	/**
34
	 * @see NodeSimplifier::isSimplifierFor
35
	 */
36 3
	public function isSimplifierFor(AbstractNode $node) {
37 3
		return $node instanceof SentenceNode;
38
	}
39
40
	/**
41
	 * @see NodeSimplifier::doSimplification
42
	 */
43 1
	public function simplify(AbstractNode $node) {
44 1
		if(!$this->isSimplifierFor($node)) {
45 1
			throw new InvalidArgumentException('SentenceNodeSimplifier can only simplify SentenceNode objects');
46
		}
47
48
		return $this->doSimplification($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PPP\DataModel\AbstractNode> is not a sub-type of object<PPP\DataModel\SentenceNode>. It seems like you assume a child class of the class PPP\DataModel\AbstractNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
49
	}
50
51
	public function doSimplification(SentenceNode $node) {
52
		return $this->resourceListNodeParser->parse(
53
			new ResourceListNode(array(new StringResourceNode($node->getValue()))),
54
			'wikibase-item'
55
		);
56
	}
57
}
58