JsonLdEntityIdFormatter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 70%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 8
dl 0
loc 85
ccs 21
cts 30
cp 0.7
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A format() 0 7 2
A toJsonLd() 0 11 3
A itemIdToJsonLd() 0 8 2
A propertyIdToJsonLd() 0 8 2
1
<?php
2
3
namespace PPP\Wikidata\ValueFormatters\JsonLd\Entity;
4
5
use InvalidArgumentException;
6
use PPP\Wikidata\ValueFormatters\JsonLd\JsonLdDataValueFormatter;
7
use ValueFormatters\FormatterOptions;
8
use ValueFormatters\ValueFormatter;
9
use ValueFormatters\ValueFormatterBase;
10
use Wikibase\DataModel\Entity\EntityIdValue;
11
use Wikibase\DataModel\Entity\Item;
12
use Wikibase\DataModel\Entity\ItemId;
13
use Wikibase\DataModel\Entity\Property;
14
use Wikibase\DataModel\Entity\PropertyId;
15
use Wikibase\DataModel\Services\Lookup\ItemLookup;
16
use Wikibase\DataModel\Services\Lookup\PropertyLookup;
17
18
/**
19
 * @licence AGPLv3+
20
 * @author Thomas Pellissier Tanon
21
 */
22
class JsonLdEntityIdFormatter extends ValueFormatterBase implements JsonLdDataValueFormatter {
23
24
	/**
25
	 * @var ItemLookup
26
	 */
27
	private $itemLookup;
28
29
	/**
30
	 * @var JsonLdItemFormatter
31
	 */
32
	private $itemFormatter;
33
34
	/**
35
	 * @var PropertyLookup
36
	 */
37
	private $propertyLookup;
38
39
	/**
40
	 * @var JsonLdPropertyFormatter
41
	 */
42
	private $propertyFormatter;
43
44
	/**
45
	 * @param ItemLookup $itemLookup
46
	 * @param ValueFormatter $itemFormatter
47
	 * @param PropertyLookup $propertyLookup
48
	 * @param ValueFormatter $propertyFormatter
49
	 * @param FormatterOptions $options
50
	 */
51
	public function __construct(
52
		ItemLookup $itemLookup,
53
		ValueFormatter $itemFormatter,
54
		PropertyLookup $propertyLookup,
55
		ValueFormatter $propertyFormatter,
56
		FormatterOptions $options
57
	) {
58
		$this->itemLookup = $itemLookup;
59
		$this->itemFormatter = $itemFormatter;
0 ignored issues
show
Documentation Bug introduced by
$itemFormatter is of type object<ValueFormatters\ValueFormatter>, but the property $itemFormatter was declared to be of type object<PPP\Wikidata\Valu...ty\JsonLdItemFormatter>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
60
		$this->propertyLookup = $propertyLookup;
61
		$this->propertyFormatter = $propertyFormatter;
0 ignored issues
show
Documentation Bug introduced by
$propertyFormatter is of type object<ValueFormatters\ValueFormatter>, but the property $propertyFormatter was declared to be of type object<PPP\Wikidata\Valu...sonLdPropertyFormatter>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
62
63
		parent::__construct($options);
64
	}
65
66
	/**
67
	 * @see ValueFormatter::format
68
	 */
69 4
	public function format($value) {
70 4
		if(!($value instanceof EntityIdValue)) {
71
			throw new InvalidArgumentException('$value is not a EntityIdValue.');
72
		}
73
74 4
		return $this->toJsonLd($value);
75
	}
76
77 4
	private function toJsonLd(EntityIdValue $value) {
78 4
		$entityId = $value->getEntityId();
79
80 4
		if($entityId instanceof ItemId) {
81 2
			return $this->itemIdToJsonLd($entityId);
82 2
		} elseif($entityId instanceof PropertyId) {
83 2
			return $this->propertyIdToJsonLd($entityId);
84
		}
85
86
		throw new InvalidArgumentException('Unsupported entity type: ' . $entityId->getEntityType());
87
	}
88
89 2
	private function itemIdToJsonLd(ItemId $itemId) {
90 2
		$item = $this->itemLookup->getItemForId($itemId);
91 2
		if($item === null) {
92 1
			$item = new Item($itemId);
93 1
		}
94
95 2
		return $this->itemFormatter->format($item);
96
	}
97
98 2
	private function propertyIdToJsonLd(PropertyId $propertyId) {
99 2
		$property = $this->propertyLookup->getPropertyForId($propertyId);
100 2
		if($property === null) {
101 1
			$property = new Property($propertyId, null, '');
102 1
		}
103
104 2
		return $this->propertyFormatter->format($property);
105
	}
106
}
107