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