Completed
Pull Request — master (#6982)
by Blizzz
14:42
created

CommentersSorter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 86
Duplicated Lines 26.74 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 23
loc 86
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getId() 0 3 1
B sort() 23 30 5
B retrieveCommentsInformation() 0 19 5
A compare() 0 9 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Comments\Collaboration;
25
26
27
use OCP\Collaboration\AutoComplete\ISorter;
28
use OCP\Comments\ICommentsManager;
29
30
class CommentersSorter implements ISorter {
31
32
	/** @var ICommentsManager */
33
	private $commentsManager;
34
35
	public function __construct(ICommentsManager $commentsManager) {
36
		$this->commentsManager = $commentsManager;
37
	}
38
39
	public function getId() {
40
		return 'commenters';
41
	}
42
43
	/**
44
	 * Sorts people who commented on the given item atop (descelating) of the
45
	 * others
46
	 *
47
	 * @param array $sortArray
48
	 * @param array $context
49
	 */
50
	public function sort(array &$sortArray, array $context) {
51
		$commenters = $this->retrieveCommentsInformation($context['itemType'], $context['itemId']);
52
		if(count($commenters) === 0) {
53
			return;
54
		}
55
56 View Code Duplication
		foreach ($sortArray as $type => &$byType) {
57
			if(!isset($commenters[$type])) {
58
				continue;
59
			}
60
61
			// at least on PHP 5.6 usort turned out to be not stable. So we add
62
			// the current index to the value and compare it on a draw
63
			$i = 0;
64
			$workArray = array_map(function($element) use (&$i) {
65
				return [$i++, $element];
66
			}, $byType);
67
68
			usort($workArray, function ($a, $b) use ($commenters, $type) {
69
				$r = $this->compare($a[1], $b[1], $commenters[$type]);
70
				if($r === 0) {
71
					$r = $a[0] - $b[0];
72
				}
73
				return $r;
74
			});
75
76
			// and remove the index values again
77
			$byType = array_column($workArray, 1);
78
		}
79
	}
80
81
	/**
82
	 * @param $type
83
	 * @param $id
84
	 * @return array
85
	 */
86
	protected function retrieveCommentsInformation($type, $id) {
87
		$comments = $this->commentsManager->getForObject($type, $id);
88
		if(count($comments) === 0) {
89
			return [];
90
		}
91
92
		$actors = [];
93
		foreach ($comments as $comment) {
94
			if(!isset($actors[$comment->getActorType()])) {
95
				$actors[$comment->getActorType()] = [];
96
			}
97
			if(!isset($actors[$comment->getActorType()][$comment->getActorId()])) {
98
				$actors[$comment->getActorType()][$comment->getActorId()] = 1;
99
			} else {
100
				$actors[$comment->getActorType()][$comment->getActorId()]++;
101
			}
102
		}
103
		return $actors;
104
	}
105
106
	protected function compare(array $a, array $b, array $commenters) {
107
		$a = $a['value']['shareWith'];
108
		$b = $b['value']['shareWith'];
109
110
		$valueA = isset($commenters[$a]) ? $commenters[$a] : 0;
111
		$valueB = isset($commenters[$b]) ? $commenters[$b] : 0;
112
113
		return $valueB - $valueA;
114
	}
115
}
116