UserCollection   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getTopUsers() 0 18 4
A sortUsers() 0 3 1
1
<?php
2
3
namespace PhpEarth\Stats\Collection;
4
5
use PhpEarth\Stats\Collection;
6
7
/**
8
 * Class UserCollection.
9
 */
10
class UserCollection extends Collection
11
{
12
    /**
13
     * @var array
14
     */
15
    private $topUsers = [];
16
17
    /**
18
     * Returns top active members of the given period.
19
     *
20
     * @param string|null $limit
21
     * @param array $ignoredUsers
22
     *
23
     * @return mixed
24
     */
25
    public function getTopUsers($limit = null, array $ignoredUsers = [])
26
    {
27
        if (sizeof($this->topUsers) == 0) {
28
            $users = $this->data;
29
            usort($users, [$this, 'sortUsers']);
30
            $users = array_reverse($users, true);
31
32
            $this->topUsers = $users;
33
        }
34
35
        $topUsers = [];
36
        foreach ($this->topUsers as $user) {
37
            if (!in_array($user->getName(), $ignoredUsers)) {
38
                $topUsers[] = $user;
39
            }
40
        }
41
42
        return array_slice($topUsers, 0, $limit);
0 ignored issues
show
Bug introduced by
It seems like $limit can also be of type string; however, parameter $length of array_slice() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        return array_slice($topUsers, 0, /** @scrutinizer ignore-type */ $limit);
Loading history...
43
    }
44
45
    /**
46
     * For calling with usort($myArray, [$this, 'sortUsers']);.
47
     *
48
     * @param $a
49
     * @param $b
50
     *
51
     * @return mixed
52
     */
53
    private function sortUsers($a, $b)
54
    {
55
        return $a->getPointsCount() - $b->getPointsCount();
56
    }
57
}
58