Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.30% covered (success)
91.30%
21 / 23
81.82% covered (warning)
81.82%
9 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Sense
91.30% covered (success)
91.30%
21 / 23
81.82% covered (warning)
81.82%
9 / 11
18.21
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getGlosses
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStatements
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 copy
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __clone
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setId
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 isEmpty
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 equals
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 clear
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare( strict_types = 1 );
4
5namespace Wikibase\Lexeme\Domain\Model;
6
7use LogicException;
8use Wikibase\DataModel\Entity\ClearableEntity;
9use Wikibase\DataModel\Entity\StatementListProvidingEntity;
10use Wikibase\DataModel\Statement\StatementList;
11use Wikibase\DataModel\Term\TermList;
12use Wikibase\Lexeme\Domain\DummyObjects\DummySenseId;
13use Wikibase\Lexeme\Domain\DummyObjects\NullSenseId;
14use Wikimedia\Assert\Assert;
15
16/**
17 * Mutable (e.g. the provided StatementList can be changed) implementation of a Lexeme's sense in
18 * the lexicographical data model.
19 *
20 * @see https://www.mediawiki.org/wiki/Extension:WikibaseLexeme/Data_Model#Sense
21 *
22 * @license GPL-2.0-or-later
23 */
24class Sense implements StatementListProvidingEntity, ClearableEntity {
25
26    public const ENTITY_TYPE = 'sense';
27
28    protected SenseId $id;
29
30    protected TermList $glossList;
31
32    protected StatementList $statementList;
33
34    public function __construct(
35        SenseId $id,
36        TermList $glossList,
37        ?StatementList $statementList = null
38    ) {
39        $this->id = $id;
40        $this->glossList = $glossList; // TODO: check there is at least gloss in one language provided
41        $this->statementList = $statementList ?: new StatementList();
42    }
43
44    public function getId(): SenseId {
45        return $this->id;
46    }
47
48    public function getGlosses(): TermList {
49        return $this->glossList;
50    }
51
52    public function getStatements(): StatementList {
53        return $this->statementList;
54    }
55
56    public function copy(): self {
57        return clone $this;
58    }
59
60    /**
61     * @see http://php.net/manual/en/language.oop5.cloning.php
62     *
63     * @since 5.1
64     */
65    public function __clone() {
66        // TermList is mutable, but Term is not. No deeper cloning necessary.
67        $this->glossList = clone $this->glossList;
68        $this->statementList = clone $this->statementList;
69    }
70
71    public function getType(): string {
72        return 'sense';
73    }
74
75    /**
76     * @param SenseId $id
77     */
78    public function setId( $id ): void {
79        Assert::parameterType( SenseId::class, $id, '$id' );
80
81        if ( !( $this->id instanceof NullSenseId || $this->id instanceof DummySenseId ) ) {
82            throw new LogicException( 'Cannot override a real SenseId' );
83        }
84
85        $this->id = $id;
86    }
87
88    public function isEmpty(): bool {
89        return $this->glossList->isEmpty()
90            && $this->statementList->isEmpty();
91    }
92
93    /**
94     * @see EntityDocument::equals
95     *
96     * @param mixed $target
97     *
98     * @return bool True if the sense's contents are equal. Does not consider the ID.
99     */
100    public function equals( $target ): bool {
101        if ( $this === $target ) {
102            return true;
103        }
104
105        return $target instanceof self
106            && $this->glossList->equals( $target->glossList )
107            && $this->statementList->equals( $target->statementList );
108    }
109
110    /**
111     * Clears the glosses and statements of a sense.
112     * Note that this leaves the sense in an insufficiently initialized state.
113     */
114    public function clear(): void {
115        $this->glossList = new TermList();
116        $this->statementList = new StatementList();
117    }
118
119}