SentenceNodeSimplifier::simplify()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
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