Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.08% covered (success)
98.08%
51 / 52
88.89% covered (warning)
88.89%
8 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
SummaryAggregator
98.08% covered (success)
98.08%
51 / 52
88.89% covered (warning)
88.89%
8 / 9
14
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 overrideSummary
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 aggregate
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
 hasNothingToMerge
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isAlreadyAggregate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasNoExistingSummary
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 haveDifferentActions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createDifferentActionAggregation
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 createSameActionAggregation
95.24% covered (success)
95.24%
20 / 21
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Wikibase\Lexeme\MediaWiki\Api\Summary;
4
5use Wikibase\Lib\Summary;
6
7/**
8 * @license GPL-2.0-or-later
9 */
10class SummaryAggregator {
11
12    /**
13     * @var string
14     */
15    private $aggregateAction;
16
17    /**
18     * @param string $aggregateAction The name to use to describe the aggregation action in Summary
19     */
20    public function __construct( $aggregateAction ) {
21        $this->aggregateAction = $aggregateAction;
22    }
23
24    /**
25     * Change the $summary to reflect the aggregation result of $summary and $subSummary
26     *
27     * Helps if you have a reference to an existing object
28     * http://php.net/manual/en/language.oop5.references.php
29     *
30     * @param Summary $summary
31     * @param Summary $subSummary
32     */
33    public function overrideSummary( Summary $summary, Summary $subSummary ) {
34        $aggregateSummary = $this->aggregate( $summary, $subSummary );
35
36        $summary->setAction( $aggregateSummary->getMessageKey() );
37        $summary->setLanguage( $aggregateSummary->getLanguageCode() );
38        $summary->setAutoSummaryArgs( $aggregateSummary->getAutoSummaryArgs() );
39        $summary->setAutoCommentArgs( $aggregateSummary->getCommentArgs() );
40    }
41
42    /**
43     * Get a Summary that contains the aggregation result of $summary and $subSummary
44     *
45     * @param Summary $summary
46     * @param Summary $subSummary
47     *
48     * @return Summary
49     */
50    public function aggregate( Summary $summary, Summary $subSummary ) {
51        if ( $this->hasNothingToMerge( $subSummary ) ) {
52            return $summary;
53        }
54
55        if ( $this->isAlreadyAggregate( $summary ) ) {
56            return $summary;
57        }
58
59        if ( $this->hasNoExistingSummary( $summary ) ) {
60            return $subSummary;
61        }
62
63        if ( $this->haveDifferentActions( $summary, $subSummary ) ) {
64            return $this->createDifferentActionAggregation( $summary, $subSummary );
65        }
66
67        return $this->createSameActionAggregation( $summary, $subSummary );
68    }
69
70    private function hasNothingToMerge( Summary $summary ) {
71        return $summary->getMessageKey() === null;
72    }
73
74    private function isAlreadyAggregate( Summary $summary ) {
75        return $summary->getMessageKey() === $this->aggregateAction;
76    }
77
78    private function hasNoExistingSummary( Summary $summary ) {
79        return $summary->getMessageKey() === null;
80    }
81
82    private function haveDifferentActions( Summary $summary, Summary $subSummary ) {
83        return $summary->getMessageKey() !== $subSummary->getMessageKey();
84    }
85
86    private function createDifferentActionAggregation( Summary $summary, Summary $subSummary ) {
87        $newSummary = new Summary();
88        $newSummary->setAction( $this->aggregateAction );
89        $newSummary->setLanguage( null );
90        $newSummary->setAutoCommentArgs(
91            array_unique(
92                array_merge(
93                    $summary->getCommentArgs(),
94                    $subSummary->getCommentArgs()
95                )
96            )
97        );
98        // discard auto summary arguments, they do not make sense with the aggregate action
99        return $newSummary;
100    }
101
102    private function createSameActionAggregation( Summary $summary, Summary $subSummary ) {
103        if ( $summary->getLanguageCode() === $subSummary->getLanguageCode() ) {
104            // TODO bubble the language into the aggregation here?
105            // $language = $summary->getLanguageCode();
106            $language = null;
107        } else { // @phan-suppress-current-line PhanPluginDuplicateIfStatements
108            $language = null;
109        }
110        $newSummary = new Summary();
111        $newSummary->setAction( $summary->getMessageKey() );
112        $newSummary->setLanguage( $language );
113        $newSummary->setAutoSummaryArgs(
114            array_merge(
115                $summary->getAutoSummaryArgs(),
116                $subSummary->getAutoSummaryArgs()
117            )
118        );
119        $newSummary->setAutoCommentArgs(
120            array_unique(
121                array_merge(
122                    $summary->getCommentArgs(),
123                    $subSummary->getCommentArgs()
124                )
125            )
126        );
127        return $newSummary;
128    }
129
130}