Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
NoCrossReferencingLexemeStatements
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
4 / 4
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validate
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 collectViolations
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
6
 getViolations
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikibase\Lexeme\Domain\Merge;
4
5use Wikibase\DataModel\Entity\EntityId;
6use Wikibase\Lexeme\Domain\EntityReferenceExtractors\LexemeStatementEntityReferenceExtractor;
7use Wikibase\Lexeme\Domain\Model\Lexeme;
8use Wikibase\Lexeme\Domain\Model\LexemeId;
9use Wikibase\Lexeme\Domain\Model\LexemeSubEntityId;
10use Wikimedia\Assert\Assert;
11
12/**
13 * @license GPL-2.0-or-later
14 */
15class NoCrossReferencingLexemeStatements {
16
17    /**
18     * @var array[] All violations from all validations of this objects life.
19     */
20    private $violations = [];
21
22    /**
23     * @var LexemeStatementEntityReferenceExtractor
24     */
25    private $refExtractor;
26
27    public function __construct( LexemeStatementEntityReferenceExtractor $refExtractor ) {
28        $this->refExtractor = $refExtractor;
29    }
30
31    /**
32     * Validate the two Lexemes and collect the violations along with any violations from
33     * previous calls.
34     * @param Lexeme $one
35     * @param Lexeme $two
36     * @return bool true if valid
37     */
38    public function validate( Lexeme $one, Lexeme $two ) {
39        $oneId = $one->getId();
40        $twoId = $two->getId();
41        Assert::parameter( $oneId !== null, '$one', 'must have a lexeme ID' );
42        Assert::parameter( $twoId !== null, '$two', 'must have a lexeme ID' );
43
44        $oneRefIds = $this->refExtractor->extractEntityIds( $one );
45        $twoRefIds = $this->refExtractor->extractEntityIds( $two );
46
47        $this->collectViolations( $twoId, $twoRefIds, $oneId );
48        $this->collectViolations( $oneId, $oneRefIds, $twoId );
49
50        return $this->violations === [];
51    }
52
53    /**
54     * @param LexemeId $entityIdsFrom The LexemeId that the $entityIds references are from
55     * @param EntityId[] $entityIds The list of EntityIds that we are checking
56     * @param LexemeId $notToReference The LexemeId that when referenced will cause violations
57     */
58    private function collectViolations(
59        LexemeId $entityIdsFrom,
60        array $entityIds,
61        LexemeId $notToReference
62    ) {
63        foreach ( $entityIds as $entityId ) {
64            if (
65                ( $entityId instanceof LexemeId && $entityId->equals( $notToReference ) ) ||
66                (
67                    $entityId instanceof LexemeSubEntityId &&
68                    $entityId->getLexemeId()->equals( $notToReference )
69                )
70            ) {
71                $this->violations[] = [ $entityIdsFrom, $entityId, $notToReference ];
72            }
73        }
74    }
75
76    /**
77     * Get violations of all validate() calls.
78     *
79     * @return array[] with each element containing:
80     * [ LexemeId $source, EntityId $reference, LexemeId $target ]
81     * Where:
82     *  - $source is the source Lexeme of the reference
83     *  - $reference is the reference to $target
84     *  - $target is the LexemeId being referenced
85     */
86    public function getViolations() {
87        return $this->violations;
88    }
89
90}