Passed
Push — sub-entities ( 5e526c )
by Daniel
03:27
created

HierarchicalEntityId::getRootId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Entity;
4
5
use InvalidArgumentException;
6
use Serializable;
7
8
/**
9
 * Base class for hierarchical entity IDs. Hierarchical entity IDs are addresses that can be
10
 * used to refer to entities that are parts of other entities, logically or physically. They
11
 * consist of the base (parent) EntityId and a relative part. The base entity may itself be
12
 * hierarchical. Only the first (root) entity ID in a chain may be a foreign ID.
13
 *
14
 * Hierarchical entity IDs are serialized as the serialization of the base entity IDs, followed
15
 * by a dash, followed by the child part of the ID.
16
 *
17
 * @license GPL-2.0+
18
 * @author Daniel Kinzler
19
 */
20
abstract class HierarchicalEntityId extends EntityId {
21
22
	/**
23
	 * @var EntityId
24
	 */
25
	private $base;
26
27
	/**
28
	 * @since 6.2
29
	 *
30
	 * @param EntityId $base
31
	 * @param string $relativePart
32
	 */
33
	public function __construct( EntityId $base, $relativePart ) {
34
		$this->base = $base;
35
		parent::__construct( $relativePart );
36
37
		if ( parent::isForeign() ) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (isForeign() instead of __construct()). Are you sure this is correct? If so, you might want to change this to $this->isForeign().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
38
			throw new InvalidArgumentException(
39
				'The relative part of a hierarchical entity cannot contain a repo prefix!'
40
			);
41
		}
42
	}
43
44
	/**
45
	 * Splits an ID serialization into the relative part, and the base ID.
46
	 *
47
	 * @param $serialization
48
	 * @return array A two element array of strings, [ $basePart, $relativePart ]
49
	 */
50
	public static function splitHierarchicalSerialization( $serialization ) {
51
		if ( !preg_match( '/^(.+)[-#]([^-#]+)/', $serialization, $m ) ) {
52
			throw new InvalidArgumentException(
53
				'Serialization is not a valid heirarchical ID: ' . $serialization
54
			);
55
		}
56
57
		$basePart = $m[1];
58
		$relativePart = $m[2];
59
60
		return [
61
			$basePart,
62
			$relativePart
63
		];
64
	}
65
66
	/**
67
	 * Returns the parent EntityId, that is, the ID of the immediate parent of this ID.
68
	 *
69
	 * @return EntityId
70
	 */
71
	public function getBaseId() {
72
		return $this->base;
73
	}
74
75
	/**
76
	 * Returns the root EntityId, that is, the beginning of the chain of base IDs.
77
	 *
78
	 * @return EntityId
79
	 */
80
	public function getRootId() {
81
		$id = $this->getBaseId();
82
		if ( $id instanceof HierarchicalEntityId ) {
83
			$id = $id->getRootId();
84
		};
85
86
		return $id;
87
	}
88
89
	/**
90
	 * @return string
91
	 */
92
	public function getRelativePart() {
93
		return parent::getSerialization();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getSerialization() instead of getRelativePart()). Are you sure this is correct? If so, you might want to change this to $this->getSerialization().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
94
	}
95
96
	/**
97
	 * @return string
98
	 */
99
	public function getSerialization() {
100
		return $this->getBaseId()->getSerialization() . '-' . $this->getRelativePart();
101
	}
102
103
	/**
104
	 * @return string
105
	 */
106
	public function getRepositoryName() {
107
		return $this->getBaseId()->getRepositoryName();
108
	}
109
110
	/**
111
	 * @return string
112
	 */
113
	public function getLocalPart() {
114
		return $this->getBaseId()->getLocalPart() . '-' . $this->getRelativePart();
115
	}
116
117
	/**
118
	 * @return bool
119
	 */
120
	public function isForeign() {
121
		return $this->getBaseId()->isForeign();
122
	}
123
124
}
125